2014-09-10 17:07:20
思路:水水的floyd,只要改关键方程为:d[i][j] = min(d[i][j] , max(d[i][k] , d[k][j]));
1 /************************************************************************* 2 > File Name: 10048.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Wed 10 Sep 2014 04:30:28 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <vector> 13 #include <queue> 14 #include <iostream> 15 #include <algorithm> 16 using namespace std; 17 typedef long long ll; 18 const int INF = 1 << 29; 19 20 int d[105][105]; 21 int C,S,Q,Case; 22 23 void Init(){ 24 for(int i = 1; i <= C; ++i){ 25 for(int j = 1; j <= C; ++j){ 26 d[i][j] = INF; 27 } 28 d[i][i] = 0; 29 } 30 } 31 32 void Floyd(){ 33 for(int k = 1; k <= C; ++k) 34 for(int i = 1; i <= C; ++i) 35 for(int j = 1; j <= C; ++j) if(d[i][k] < INF && d[k][j] < INF){ 36 d[i][j] = min(d[i][j],max(d[i][k],d[k][j])); 37 } 38 } 39 40 int main(){ 41 int x,y,f; 42 while(scanf("%d%d%d",&C,&S,&Q) != EOF && (C || S || Q)){ 43 Init(); 44 for(int i = 1; i <= S; ++i){ 45 scanf("%d%d%d",&x,&y,&f); 46 d[x][y] = d[y][x] = f; 47 } 48 Floyd(); 49 if(Case) puts(""); 50 printf("Case #%d\n",++Case); 51 for(int i = 1; i <= Q; ++i){ 52 scanf("%d%d",&x,&y); 53 if(d[x][y] >= INF) printf("no path\n"); 54 else printf("%d\n",d[x][y]); 55 } 56 } 57 return 0; 58 }