用floyd求最短路,难得的一次AC
View Code
1 #include <stdio.h> 2 #define MIN(a,b) ((a)<(b)?(a):(b)) 3 #define N 20 4 #define INF 0x7fffff 5 int dist[N][N]; 6 int main() 7 { 8 int i,j,k,x,kase=0; 9 while(1) 10 { 11 for(i=0;i<N;i++) 12 for(j=i+1;j<N;j++) dist[i][j]=dist[j][i]=INF; 13 for(i=0;i<19;i++) 14 { 15 if(scanf("%d",&x)==EOF) return 0; 16 while(x--) 17 { 18 scanf("%d",&j); 19 dist[i][j-1]=dist[j-1][i]=1; 20 } 21 } 22 for(k=0;k<N;k++) 23 for(i=0;i<N;i++) 24 for(j=0;j<N;j++) 25 dist[i][j]=MIN(dist[i][k]+dist[k][j],dist[i][j]); 26 scanf("%d",&x); 27 printf("Test Set #%d\n",++kase); 28 while(x--) 29 { 30 scanf("%d%d",&i,&j); 31 printf("%2d to %2d: %d\n",i,j,dist[i-1][j-1]); 32 } 33 printf("\n"); 34 } 35 return 0; 36 }