2009年11月7日 星期六

UVa 336 A Node Too Far

佛洛伊德為什麼會WA  QQ                                                                                                                    .


這樣會讓我對佛洛伊德產生陰影QQ


//UVa 336

#include<stdio.h>
int dist[31][31];
int count[31][32];
int convert[31],ccnt=1;
int n,cnt=1,i,j;
int find(int key){
    for(int i=0;i<ccnt;i++)
        if(key==convert[i])
            return i;
    return -1;
}
main(){
    freopen("UVa336.in","r",stdin);
    freopen("UVa336.out","w",stdout);
    while(scanf("%d",&n),n){
        //Initialize
        ccnt=1;
        for(int i=0;i<31;i++)
            for(int j=0;j<31;j++){
                dist[i][j]=i==j?0:20000;
                count[i][j]=0;
            }
        //Input
        while(n--){
            int i,j;
            scanf("%d%d",&i,&j);
            int fi=find(i),fj=find(j);
            if(fi==-1)convert[ccnt++]=i;
            if(fj==-1)convert[ccnt++]=j;
            fi=find(i);
            fj=find(j);
            dist[fi][fj]=dist[fj][fi]=1;
        }
        //Floyd-Warshall
        for(int k=1;k<ccnt;k++)
            for(int i=1;i<ccnt;i++)
                for(int j=1;j<ccnt;j++)
                    if(dist[i][k]+dist[k][j]<dist[i][j])
                        dist[i][j]=dist[i][k]+dist[k][j];
        //Count The Dist
        for(int i=1;i<ccnt;i++){
            for(int j=1;j<ccnt;j++){
                //printf("dist %d %d:%d\n",i,j,dist[i][j]);
                if(dist[i][j]<31)
                    count[i][dist[i][j]]++;
            }
            for(int j=1;j<31;j++)
                count[i][j]+=count[i][j-1];
        }
        //Output
        while(1){
            int i,j,fi;
            scanf("%d%d",&i,&j);
            if(i==j&&!j)break;
            if(j>30)j=30;
            fi=find(i);
            printf("Case %d: %d nodes not reachable from node %d with TTL = %d.\n",cnt++,ccnt-1-count[find(i)][j],i,j);
        }
    }
}


沒有留言:

張貼留言