poj3615
floyd水题,将map[i][j]理解为从i到j的路径中高度最小即可,将松弛条件改为map[i][j]=MIN(map[i][j],MAX(map[i][k],map[k][j]));好好理解一下吧
View Code
1 #include <stdio.h>
2 #include <string.h>
3 #define MAXN 310
4 #define INF (1<<29)
5 #define MIN(a,b) ((a)<(b)?(a):(b))
6 #define MAX(a,b) ((a)>(b)?(a):(b))
7 int n,m,t;
8 int map[MAXN][MAXN];
9 void floyd()
10 {
11 for(int k=1;k<=n;++k)
12 for(int i=1;i<=n;++i)
13 for(int j=1;j<=n;++j)
14 map[i][j]=MIN(map[i][j],MAX(map[i][k],map[k][j]));
15 }
16 int main()
17 {
18 while(scanf("%d %d %d",&n,&m,&t)==3)
19 {
20 int a,b,c;
21 for(int i=1;i<=n;++i)
22 for(int j=1;j<=n;map[i][j++]=INF);
23 for(int i=0;i<m;++i)
24 {
25 scanf("%d %d %d",&a,&b,&c);
26 map[a][b]=c;
27 }
28 floyd();
29 for(int i=0;i<t;++i)
30 {
31 scanf("%d %d",&a,&b);
32 if(map[a][b]==INF) printf("-1\n");
33 else printf("%d\n",map[a][b]);
34 }
35 }
36 return 0;
37 }