首页 > 代码库 > POJ 3615 Cow Hurdles

POJ 3615 Cow Hurdles

http://poj.org/problem?id=3615

floyd 最短路径的变形 dist[i][j]变化为 : i j之间的最大边

 

那么输入的时候可以直接把dist[i][j] 当作i j 之间的边进行输入

转移方程 dist[i][j] = max(dist[i][j], min(dist[i][k], dist[k][j]))

 1 #include <iostream>
 2 #include <string.h>
 3 #include <stdio.h>
 4 #define INF 0x3fffffff
 5 using namespace std;
 6 const int maxn = 305;
 7 int Map[maxn][maxn];
 8 
 9 int dist[maxn][maxn];//dist[i][j]表示 i 到 j 的最大边是多少?
10 //转移方程 dist[i][j] = min( dist[i][j] ,max(dist[i][k], dist[k][j])) ---> "**"
11 //
12 //与floyd的区别 dist是 i-j的最短‘距离‘
13 int main()
14 {
15     int m, n, k;
16     scanf("%d%d%d", &m, &n, &k);
17     for(int i = 1; i <= m; i++)
18         for (int j = 1; j <= m; j++)
19             dist[i][j] = INF;
20     for (int i = 0; i < n; i++)
21     {
22         int from, to, cost;
23         scanf("%d%d%d", &from, &to, &cost);
24         dist[from][to] = cost;
25     }
26     for(int i = 1; i <= m; i++)
27     {
28         for (int j = 1; j <= m; j++)
29         {
30             for (int k = 1; k <= m; k++)
31             {
32                // if (dist[j][k] == -1) dist[j][k] = max(dist[j][i], dist[i][k]);
33                 //else
34                     dist[j][k] = min(dist[j][k], max(dist[j][i], dist[i][k]));
35             }
36         }
37     }
38     for (int i = 0; i < k; i++)
39     {
40         int from, to;
41         scanf("%d%d", &from, &to);
42         if (dist[from][to] == INF) cout << -1 << endl;
43         else  cout << dist[from][to] << endl;
44     }
45     return 0;
46 }

 

POJ 3615 Cow Hurdles