POJ 1797 Heavy Transportation(Dijkstra变形——最长路径最小权值)

题目链接:

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

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

题意描述:
输入路口数及道路数以及每条路的承重量
计算并输出有最大承重量路径中的最小承重量(有点绕,其实就是最结实的那条路径中最不结实的一段路限重是多少)
解题思路:
最短路径问题的变型,处理数据使用迪杰斯特拉算法即可。
题目很经典,另外
最长路径最小权值题目 请参考博客:http://www.cnblogs.com/wenzhixin/p/7336948.html
最短路径双重最小权值题目参考博客:http://www.cnblogs.com/wenzhixin/p/7405802.html
代码实现:
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 const int inf=99999999;
 6 int d[1010],e[1010][1010],book[1010];
 7 int main()
 8 {
 9     int t,n,m,i,j,t1,t2,t3,max,u,k,c=1;
10     scanf("%d",&t);
11     while(t--)
12     {
13         scanf("%d%d",&n,&m); 
14         for(i=1;i<=n;i++)
15             for(j=1;j<=n;j++)
16                 e[i][j]=-inf;//初始化为无穷小值,后面求最长路径下的情况 
17         for(i=1;i<=m;i++)
18         {
19             scanf("%d%d%d",&t1,&t2,&t3);
20             e[t1][t2]=e[t2][t1]=t3;
21         }
22         for(i=1;i<=n;i++)
23             d[i]=e[1][i];
24         memset(book,0,sizeof(book));
25         book[1]=1;
26         for(i=1;i<=n-1;i++)
27         {
28             max=-inf;
29             for(j=1;j<=n;j++)//找到1到各个非树结点中(最小承重量)的最大承重量 
30             {
31                 if(!book[j] && d[j] > max)
32                 {
33                     max=d[j];
34                     u=j;
35                 }
36             }
37             book[u]=1;
38             for(k=1;k<=n;k++)
39             {//更新1到各个非树结点的最小承重量为
40         //之前的最大承重量 和 u到各个非树结点的承重量 中较小者 大于之前结果的承重量 
41                 if(!book[k] && d[k] < min(d[u],e[u][k]))//c++提交 
42                     d[k]=min(d[u],e[u][k]);
43             }
44         }
45         printf("Scenario #%d:\n%d\n\n",c++,d[n]);
46         //d中存的是从1到每个结点的最小承重量 
47     }
48     return 0;
49 }

 



posted @ 2017-08-09 23:35  Reqaw  阅读(665)  评论(0编辑  收藏  举报