4
#include<iostream>
#include<vector>
#include<string.h>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int MaxN=109;
const int INF=1000000000;
int N,M;
vector<int> e[MaxN];
vector<pair<int,int>> Go[MaxN];
int A[MaxN],B[MaxN],F[MaxN],G[MaxN];
void init()
{
for(int i=1;i<=N;i++)
e[i].clear();
for(int i=1;i<=N;i++)
{
int x;
scanf("%d%d%d",&A[i],&B[i],&x);
B[i]+=x;
if(A[i]>=B[i]);
else
A[i]=B[i];
}
for(int i=1;i<N;i++)
{
int u,v;
scanf("%d%d",&u,&v);
e[u].push_back(v);
e[v].push_back(u);
}
}
void Dfs(int fa,int x)
{
F[x]=A[x];
G[x]=B[x];
Go[x].clear();
for(int i=0;i<e[x].size();i++)
if(e[x][i]!=fa)
{
Dfs(x,e[x][i]);
Go[x].push_back(make_pair(G[e[x][i]],F[e[x][i]]));
}
sort(Go[x].begin(),Go[x].end());
for(int i=Go[x].size()-1;i>=0;i--)
{
if(F[x]-G[x]<Go[x][i].second)
F[x]=G[x]+Go[x][i].second;
G[x]+=Go[x][i].first;
}
}
void solve()
{
int ans=INF;
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
F[j]=G[j]=-1;
Dfs(0,i);
if(F[i]<ans)
ans=F[i];
}
printf("%d\n",ans);
}
int main()
{
int T=0;
while(1==scanf("%d",&N)&&N!=0)
{
T++;
init();
printf("Case %d: ",T);
solve();
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
const int MAXN=1100007;
const int MAXR=20;
const int MAXC=10;
typedef unsigned char _char;
struct Tstack
{
static const int MAXS=MAXN;
int len;
int data[MAXS];
void clear(void){len=0;}
void push(int k){data[++len]=k;}
int pop(void){return data[len--];}
inline bool empty(){return len==0;}
};
const int exp4[]={0x000003,0x00000C,0x000003,0x000030,0x0000C0,0x000300,0x000C00,
0x003000,0x00C000,0x030000,0x0C0000,0x30000,0xC00000};
const int div4[]={0,2,4,6,8,10,12,14,16,18,20,22};
int r,c;
_char f[MAXR][MAXC][MAXN];
int l[MAXR][MAXC][MAXN];
Tstack s[2];
bool a[MAXR][MAXC];
int ans[MAXR][MAXC];
bool path[MAXR][MAXC];
int maxlen=0;
void init()
{
int i,j;
char str[20];
for(i=0;i<r;++i)
{
scanf("%s",str);
for(j=0;j<c;++j)
if(str[j]=='#')
a[i][j]=0;
else
a[i][j]=1;
}
}
inline _char max(_char a,_char b)
{
return a>b?a:b;
}
inline int getbit(int k,int n)
{
return ((k&exp4[n])>>div4[n]);
}
inline int changebit(int k,int n,int m)
{
return k-(k&exp4[n])+(m<<div4[n]);
}
inline int changeL(int k,int j)
{
k=changebit(changebit(k,j,3),j+1,3);
int t=j+1,s=1,i;
while(s>0)
{
++t;
i=getbit(k,t);
if(i==1)
++s;
else if(i==2)
--s;
}
k=changebit(k,t,1);
return k;
}
inline int changeR(int k,int j)
{
k=changebit(changebit(k,j,3),j+1,3);
int t=j,s=1,i;
while(s>0)
{
--t;
i=getbit(k,t);
if(i==2)
++s;
else if(i==1)
--s;
}
k=changebit(k,t,2);
return k;
}
inline void updata(_char hash[],int path[],Tstack &s,int k,_char v,int last)
{
if(hash[k]==0xff)
{
hash[k]=v;
path[k]=last;
s.push(k);
}
else if(v>hash[k])
{
hash[k]=v;
path[k]=last;
}
}
inline bool ok(int k)
{
int i,j;
for(i=0;i<c;++i)
{
j=getbit(k,i);
if((j==1)||(j==2))
return false;
}
return getbit(k,c)==1;
}
void solve()
{
int now=0;
int last=1;
int i,j,k,t,left,up,d;
memset(f,0xff,sizeof(f));
updata(f[0][0],l[0][0],s[now],1,0,-1);
for(i=0;i<r;++i)
{
for(j=0;j<c;++j)
{
now^=1;
last^=1;
s[now].clear();
if(s[last].len>maxlen)
maxlen=s[last].len;
while(!s[last].empty())
{
k=s[last].pop();
d=f[i][j][k];
left=getbit(k,j);
up=getbit(k,j+1);
if(a[i][j])
{
if((left==2)&&(up==1))
{
updata(f[i][j+1],l[i][j+1],s[now],changebit(changebit(k,j,3),j+1,3),d+1,k);
}
else if((left==0)&&(up==0))
{
updata(f[i][j+1],l[i][j+1],s[now],changebit(changebit(k,j,1),j+1,2),d+1,k);
}
else if((left==1)&&(up==1))
{
updata(f[i][j+1],l[i][j+1],s[now],changeL(k,j),d+1,k);
}
else if((left==2)&&(up==2))
{
updata(f[i][j+1],l[i][j+1],s[now],changeR(k,j),d+1,k);
}
else if((left==0)&&(up<3))
{
updata(f[i][j+1],l[i][j+1],s[now],changebit(changebit(k,j,up),j+1,3),d+1,k);
updata(f[i][j+1],l[i][j+1],s[now],changebit(changebit(k,j,3),j+1,up),d+1,k);
}
else if((left<3)&&(up==0))
{
if((j+2>c)||(getbit(k,j+2)!=3))
updata(f[i][j+1],l[i][j+1],s[now],changebit(changebit(k,j,left),j+1,3),d+1,k);
updata(f[i][j+1],l[i][j+1],s[now],changebit(changebit(k,j,3),j+1,left),d+1,k);
}
}
if(((left==0)||(left==3))&&((up==0)||(up==3)))
{
if((up==3)&&((j+2>c)||(getbit(k,j+2)==0)))
updata(f[i][j+1],l[i][j+1],s[now],changebit(changebit(k,j,0),j+1,3),d,k);
else
updata(f[i][j+1],l[i][j+1],s[now],changebit(changebit(k,j,0),j+1,0),d,k);
}
}
}
if(i==(r-1))
break;
now^=1;
last^=1;
s[now].clear();
while(!s[last].empty())
{
k=s[last].pop();
d=f[i][c][k];
left=getbit(k,c);
if((left==0)||(left==3))
updata(f[i+1][0],l[i+1][0],s[now],changebit(k,c,0)<<2,d,k);
}
}
int anslen=-1;
k=-1;
while(!s[now].empty())
{
k=s[now].pop();
if(ok(k)&&(f[i][c][k]>anslen))
{
anslen=f[i][c][k];
t=k;
}
}
for(i=r-1;i>=0;--i)
for(j=c;j>=0;--j)
{
k=t;
ans[i][j]=k;
t=l[i][j][k];
}
d=0;
memset(path,0,sizeof(path));
for(i=0;i<r;++i)
for(j=0;j<=c;++j)
{
k=f[i][j][ans[i][j]];
if(k>d)
path[i][j]=true;
d=k;
}
for(i=0;i<r;++i)
{
for(j=0;j<c;++j)
{
if(path[i][j+1])
printf("C");
else if(!a[i][j])
printf("#");
else
printf(".");
}
printf("\n");
}
}
int main()
{
int CASE=0;
maxlen=0;
while(scanf("%d%d",&r,&c)&&(r||c))
{
printf("CASE %d:\n",++CASE);
init();
solve();
printf("\n");
}
}
6
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
const int MAXN=110;
const int COSA=0.5;
int s,p,d,h;
int a[MAXN][MAXN];
double ans;
void init()
{
scanf("%d %d %d",&p,&d,&h);
int i,j;
for(i=1;i<=s;++i)
{
for(j=1;j<=p+1-(i&1);++j)
scanf("%d",&a[i][j]);
}
}
void makeswap(int &a,int &b)
{
if(a>b)
{
int t=a;
a=b;
b=t;
}
}
inline double gettri(double a,double b)
{
return sqrt(a*a+b*b-2*a*b*COSA);
}
inline double gethalflong(int a,int b,int c,int l,int r)
{
if(l>r)
return 0;
double L=gettri(double(l-a)/double(b-a)*d,double(l-a)/double(c-a)*d);
double R=gettri(double(r-a)/double(b-a)*d,double(r-a)/double(c-a)*d);
return (L+R)*((r-l)/h+1)/2;
}
double getlong(int a,int b,int c)
{
makeswap(a,b);
makeswap(b,c);
makeswap(a,b);
double ret;
int l,m1,m2,r;
if(a==c)
{
if(a%h==0)
return d*1.5;
}
if(a==b)
{
l=(a/h)*h;
if(l<a)
l+=h;
r=(c/h)*h;
ret=gethalflong(-c,-b,-a,-r,-l);
}
else if(b==c)
{
l=(a/h)*h+h;
r=(c/h)*h;
ret=gethalflong(a,b,c,l,r);
}
else
{
l=(a/h)*h+h;
m2=(b/h)*h+h;
m1=m2-h;
r=(c/h)*h;
if(r==c)
r-=h;
ret=gethalflong(a,b,c,l,m1)+gethalflong(-c,-b,-a,-r,-m2);
}
if((b%h==0)&&((a==b)||(b==c)))
ret-=d*0.5;
return ret;
}
bool equal(int a,int b,int c,int d)
{
return (a==b)&&(b==c)&&(c==d);
}
void solve()
{
ans=0;
int i,j;
for(i=1;i<s;i+=2)
{
for(j=1;j<p;++j)
{
ans+=getlong(a[i][j],a[i+1][j],a[i+1][j+1]);
ans+=getlong(a[i][j],a[i][j+1],a[i+1][j+1]);
}
ans+=getlong(a[i][p],a[i+1][p],a[i+1][p+1]);
if((a[i][1]==a[i+1][1])&&(a[i][1]%h==0))
ans+=d*0.5;
if((a[i][p]==a[i+1][p+1])&&(a[i][p]%h==0))
ans+=d*0.5;
}
for(i=1;i<s;i+=2)
{
for(j=1;j<p;++j)
{
ans+=getlong(a[i][j],a[i+1][j],a[i+1][j+1]);
ans+=getlong(a[i+1][j+1],a[i][j+1],a[i+1][j]);
}
ans+=getlong(a[i][p],a[i+1][p],a[i+1][p+1]);
if((a[i][1]==a[i+1][1])&&(a[i][1]%h==0))
ans+=d*0.5;
if((a[i][p+1]==a[i+1][p])&&(a[i][p+1]%h==0))
ans+=d*0.5;
}
for(i=1;i<p;++i)
if((a[1][i]==a[1][i+1])&&(a[1][i]%h==0))
ans+=d*0.5;
for(i=1;i<p+1-(s&1);++i)
if((a[s][i]==a[s][i+1])&&(a[s][i]%h==0))
ans+=d*0.5;
for(i=1;i<s;i+=2)
{
for(j=1;j<p;++j)
if(a[i][j]%h==0)
{
if(equal(a[i][j],a[i+1][j],a[i][j+1],a[i+1][j+1]))
ans-=d;
if(equal(a[i][j],a[i][j+1],a[i+1][j+1],a[i+1][j+2]))
ans-=d;
}
if(i<s-1)
for(j=1;j<=p;++j)
if(a[i][j]%h==0)
if(equal(a[i][j],a[i+1][j],a[i+2][j],a[i+1][j+1]))
ans-=d;
}
for(i=2;i<s;i+=2)
{
for(j=1;j<p;++j)
if(a[i][j+1]%h==0)
{
if(equal(a[i][j],a[i+1][j],a[i][j+1],a[i+1][j+1]))
ans-=d;
if(equal(a[i+1][j],a[i][j+1],a[i+1][j+1],a[i][j+2]))
ans-=d;
}
if(i<s-1)
for(j=1;j<p;++j)
if(a[i][j+1]%h==0)
if(equal(a[i][j+1],a[i+1][j+1],a[i+2][j+1],a[i+1][j]))
ans-=d;
}
printf("%.0f\n",ans);
}
int main()
{
int CASE=0;
while((scanf("%d",&s)!=EOF)&&s)
{
init();
printf("Case %d: ",++CASE);
solve();
}
return 0;
}
7
#include<stdio.h>
#include<string.h>
#include<math.h>
const long MAXN=110;
const long INF=1e+6;
struct Tpoint
{
long x,y;
};
long n,p1,p2;
Tpoint a[MAXN];
float f[MAXN][MAXN];
long lx[MAXN][MAXN],ly[MAXN][MAXN];
long ans1[MAXN],ans2[MAXN];
inline long sqr(long a)
{
return a*a;
}
inline float dis(Tpoint a,Tpoint b)
{
return sqrt(double(sqr(a.x-b.x)+sqr(a.y-b.y)));
}
inline float min(float a,float b)
{
return a<b?a:b;
}
void init(void)
{
for(long i=0;i<n;++i)
scanf("%ld %ld",&a[i].x,&a[i].y);
}
void update(long x1,long y1,long x2,long y2,float dis)
{
if(f[x1][y1]+dis<f[x2][y2])
{
f[x2][y2]=f[x1][y1]+dis;
lx[x2][y2]=x1;
ly[x2][y2]=y1;
}
}
void solve()
{
long i,j,k;
for(i=0;i<=n;++i)
for(j=0;j<=n;++j)
f[i][j]=1e+8;
f[0][0]=0;
for(k=1;k<n;++k)
{
if(p2!=k)
{
for(i=0;i<k;++i)
{
if(f[k-1][i]<INF)
update(k-1,i,k,i,dis(a[k-1],a[k]));
if(f[i][k-1]<INF)
update(i,k-1,k,k-1,dis(a[i],a[k]));
}
}
if(p1!=k)
{
for(i=0;i<k;++i)
{
if(f[i][k-1]<INF)
update(i,k-1,i,k,dis(a[k-1],a[k]));
if(f[k-1][i]<INF)
update(k-1,i,k-1,k,dis(a[i],a[k]));
}
}
}
float ans=1e+8;
long x,y,x1,y1,l1,l2;
for(i=0;i<n;++i)
{
if(f[i][n-1]+dis(a[i],a[n-1])<ans)
{
ans=f[i][n-1]+dis(a[i],a[n-1]);
x=i;
y=n-1;
}
else if(f[n-1][i]+dis(a[i],a[n-1])<ans)
{
ans=f[n-1][i]+dis(a[i],a[n-1]);
x=n-1;
y=i;
}
l1=0;
l2=0;
ans1[++l1]=x;
ans2[++l2]=y;
while((x!=0)||(y!=0))
{
x1=lx[x][y];
y1=ly[x][y];
x=x1;
y=y1;
ans1[++l1]=x1;
ans2[++l2]=y1;
}
}
printf("%.2f\n",ans);
for(i=l1;i>=1;--i)
if((i==l1)||(ans1[i]!=ans1[i+1]))
printf("%ld",ans1[i]);
for(i=1;i<=l2;++i)
if((i==1)||(ans2[i]!=ans2[i-1]))
printf("%ld",ans2[i]);
printf("\n");
}
int main()
{
long CASE=0;
while((scanf("%ld %ld %ld",&n,&p1,&p2)!=EOF)&&n)
{
printf("Case %ld: ",++CASE);
init();
solve();
}
return 0;
}