首页 > 代码库 > POJ 1797 Heavy Transportation

POJ 1797 Heavy Transportation

题目:

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

题意描述:
输入路口数及道路数以及每条路的承重量
计算并输出有最大承重量路径中的最小承重量(有点绕,其实就是最结实的那条路径中最不结实的一段路限重是多少)
又一次展现了代码的神奇力量
解题思路:
最短路径问题的变型,处理数据使用迪杰斯特拉算法即可。
代码实现:
 1 #include<stdio.h>
 2 #include<algorithm>
 3 using namespace std;
 4 const int inf=99999999;
 5 int d[1010],e[1010][1010],book[1010];
 6 int main()
 7 {
 8     int t,n,m,i,j,t1,t2,t3,max,u,k,c=1;
 9     scanf("%d",&t);//先输入t 
10     while(t--)
11     {
12         scanf("%d%d",&n,&m);//先输入循环次数再循环 
13         for(i=1;i<=n;i++)
14         {
15             for(j=1;j<=n;j++)
16             {
17                 if(i==j)
18                 e[i][j]=0;
19                 else
20                 e[i][j]=-inf;//根据需求,后面求最小 则初始化最大,最大 则初始化最小 
21             }
22         }
23         for(i=1;i<=m;i++)
24         {
25             scanf("%d%d%d",&t1,&t2,&t3);
26             e[t1][t2]=e[t2][t1]=t3;
27         }
28         
29         for(i=1;i<=n;i++)
30         {
31             d[i]=e[1][i];
32             book[i]=0;
33         }
34         book[1]=1;
35         for(i=1;i<=n-1;i++)
36         {
37             max=-inf;
38             for(j=1;j<=n;j++)//找到1到各个非树结点中(最小承重量)的最大承重量 
39             {
40                 if(!book[j] && d[j] > max)
41                 {
42                     max=d[j];
43                     u=j;
44                 }
45             }
46             book[u]=1;
47             for(k=1;k<=n;k++)
48             {//更新1到各个非树结点的最小承重量为
49         //之前的最大承重量 和 u到各个非树结点的承重量 中较小者 大于之前结果的承重量 
50                 if(!book[k] && d[k] < min(d[u],e[u][k]))//c++提交 
51                     d[k]=min(d[u],e[u][k]);
52             }
53         }
54         printf("Scenario #%d:\n",c++);
55         printf("%d\n\n",d[n]);//d中存的是从1到每个结点的最小承重量 
56     }
57     return 0;
58 }

POJ 1797 Heavy Transportation