POJ 1797 Heavy Transportation

题意:有1~n个顶点,找所有连通的路中承载重量最大的那条路径

分析:首先想到的贪心,就是每次找最大的那条,就是Dijkstra变形嘛,跟我做的上一道题有点像,贴下一道题的链接:https://www.cnblogs.com/ducklu/p/9188670.html

这一道题是每一次找最大然后收录,然后松弛的时候也有变化

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <algorithm>
 5 #include <cmath>
 6 #define MAXVERTEXNUM 1010
 7 //#define INF 99999999
 8 using namespace std;
 9 
10 int D[MAXVERTEXNUM][MAXVERTEXNUM], dist[MAXVERTEXNUM];
11 int Nv, Ne;
12 
13 void Dijkstra()
14 {
15     int Visited[MAXVERTEXNUM];
16     memset(Visited, 0, sizeof(Visited));
17     for (int i = 2; i <= Nv; ++i)
18     {
19         if (D[1][i])
20             dist[i] = D[1][i];
21         else
22             dist[i] = 0;
23     }
24     Visited[1] = 1;
25     dist[1] = 0;
26 
27     for (int i = 1; i <= Nv; ++i)
28     {
29         int MAXDist = -1, pos;
30         for (int j = 2; j <= Nv; ++j)
31             if (!Visited[j] && dist[j] > MAXDist)
32             {
33                 MAXDist = dist[j];
34                 pos = j;
35             }
36 
37         Visited[pos] = 1;
38 
39         for (int j = 2; j <= Nv; ++j)
40         {
41             if (!Visited[j] && D[pos][j])
42                 dist[j] = max(dist[j], min(dist[pos], D[pos][j]));
43         }
44     }
45 }
46 
47 int main()
48 {
49     ios::sync_with_stdio(false);
50 
51     int cas;
52     cin >> cas;
53     for (int k = 1; k <= cas; ++k)
54     {
55         memset(D, 0, sizeof(D));
56         cin >> Nv >> Ne;
57         for (int i = 1; i <= Ne; ++i)
58         {
59             int V1, V2, Edge;
60             cin >> V1 >> V2 >> Edge;
61             D[V1][V2] = D[V2][V1] = Edge;
62         }
63 
64         Dijkstra();
65         printf("Scenario #%d:\n%d\n\n", k, dist[Nv]);
66     }
67 
68     return 0;
69 }

 

posted @ 2018-06-15 20:47  duck_lu  阅读(73)  评论(0编辑  收藏  举报