UVA 10048 - Audiophobia
floyd算法的变形,输出格式问题要注意。。。
代码如下:
#include<stdio.h> #define MAXNC 100 + 10 #define INF 100000 + 100 int d[MAXNC][MAXNC]; int C, S, Q, num; void init() { num = 0; while(scanf("%d%d%d",&C, &S, &Q)) { if(C <= 1) break; else if(num != 0) printf("\n"); for(int i = 0; i <= C; i ++) for(int j = 0; j <= C; j ++) { if(i != j) d[i][j] = INF; else d[i][j] = 0; } for(int i = 0; i < S; i ++) { int x, y, w; scanf("%d%d%d", &x, &y, &w); if(d[x][y] > w) d[x][y] = w, d[y][x] = w; } for(int k = 1; k <= C; k ++) for(int i = 1; i <= C; i ++) for(int j = 1; j <= C; j ++) { if(d[i][k] != INF && d[k][j] != INF) { if(d[i][k] >= d[k][j]) { if(d[i][k] < d[i][j]) d[i][j] = d[i][k]; } else if(d[k][j] < d[i][j]) d[i][j] = d[k][j]; } } printf("Case #%d\n",++num); for(int i = 0; i < Q; i ++) { int a, b; scanf("%d%d",&a,&b); if(d[b][a] < INF) printf("%d\n",d[a][b]); else printf("no path\n"); } } } int main() { init(); return 0; }