POJ1797 Heavy Transportation —— 最短路变形

题目链接:http://poj.org/problem?id=1797

 

Heavy Transportation
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 39999   Accepted: 10515

Description

Background 
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight. 
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know. 

Problem 
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's place). You may assume that there is at least one path. All streets can be travelled in both directions.

Input

The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.

Sample Input

1
3 3
1 2 3
1 3 4
2 3 5

Sample Output

Scenario #1:
4

Source

TUD Programming Contest 2004, Darmstadt, Germany
 
 
 
题解:
1.最短路径的变形:把dis[]从原来的记录最短距离 变为 记录不同路径上最小边权中的最大值。
2.利用dijkstra算法时,每次松弛都是选取dis的最大值。
 
 
代码如下:
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 #define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++)
13 #define ms(a,b) memset((a),(b),sizeof((a)))
14 using namespace std;
15 typedef long long LL;
16 const double EPS = 1e-8;
17 const int INF = 2e9;
18 const LL LNF = 9e18;
19 const int MOD = 1e9+7;
20 const int MAXN = 1e3+10;
21 
22 int n, m;
23 
24 struct edge
25 {
26     int to, w, next;
27 }edge[MAXN*MAXN];
28 int cnt, head[MAXN];
29 
30 void addedge(int u, int v, int w)
31 {
32     edge[cnt].to = v;
33     edge[cnt].w = w;
34     edge[cnt].next = head[u];
35     head[u] = cnt++;
36 }
37 
38 void init()
39 {
40     cnt = 0;
41     memset(head, -1, sizeof(head));
42 }
43 
44 int dis[MAXN];
45 bool vis[MAXN];
46 void dijkstra(int st)
47 {
48     memset(vis, 0, sizeof(vis));
49     for(int i = 1; i<=n; i++)
50         dis[i] = (i==st?INF:0);
51 
52     for(int i = 1; i<=n; i++)
53     {
54         int k, maxx = 0;
55         for(int j = 1; j<=n; j++)
56             if(!vis[j] && dis[j]>maxx)
57                 maxx = dis[k=j];
58 
59         vis[k] = 1;
60         for(int j = head[k]; j!=-1; j = edge[j].next)
61             if(!vis[edge[j].to])
62                 dis[edge[j].to] = max(dis[edge[j].to], min(dis[k], edge[j].w) );
63     }
64 }
65 
66 int x[MAXN], y[MAXN];
67 int main()
68 {
69     int T;
70     scanf("%d", &T);
71     for(int kase = 1; kase<=T; kase++)
72     {
73         scanf("%d%d", &n, &m);
74         init();
75         for(int i = 1; i<=m; i++)
76         {
77             int u, v, w;
78             scanf("%d%d%d", &u, &v, &w);
79             addedge(u,v,w);
80             addedge(v,u,w);
81         }
82 
83         dijkstra(1);
84         printf("Scenario #%d:\n",kase);
85         printf("%d\n\n", dis[n]);
86     }
87 }
View Code

 

 

posted on 2017-09-26 11:04  h_z_cong  阅读(357)  评论(0编辑  收藏  举报

导航