这一题不错。。。将floyd变了变形。。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 5 int point,street,que; 6 int g[1010][1010]; 7 int cas; 8 9 bool init() 10 { 11 scanf("%d%d%d",&point,&street,&que); 12 if(point == 0&&street == 0&&que == 0) 13 return false; 14 15 if(cas > 1)//个人觉得这些东西比较恶心。。。 16 printf("\n"); 17 memset(g,-1,sizeof(g)); 18 int a,b,d; 19 for(int i = 0;i < street;i ++) 20 { 21 scanf("%d%d%d",&a,&b,&d); 22 g[a][b] = g[b][a] = d; 23 g[a][a] = g[b][b] = 0; 24 } 25 return true; 26 } 27 28 void floyd() 29 { 30 for(int i = 1;i <= point;i ++) 31 { 32 for(int j = 1;j <= point;j ++) 33 { 34 for(int k = 1;k <= point;k ++) 35 { 36 if(g[j][i] != -1&&g[i][k] != -1) 37 { 38 int t = std::max(g[j][i],g[i][k]); 39 if(g[j][k] == -1) 40 { 41 g[j][k] = t; 42 } 43 else 44 { 45 g[j][k] = std::min(t,g[j][k]); 46 } 47 } 48 } 49 } 50 } 51 } 52 53 int main() 54 { 55 cas = 1; 56 while(init()) 57 { 58 floyd(); 59 printf("Case #%d\n",cas ++); 60 for(int i = 0;i < que;i ++) 61 { 62 int a,b; 63 scanf("%d%d",&a,&b); 64 if(g[a][b] == -1) 65 { 66 printf("no path\n"); 67 } 68 else 69 { 70 printf("%d\n",g[a][b]); 71 } 72 } 73 } 74 75 return 0; 76 }