POJ—1797(Heavy Transportation)

来源:互联网 发布:淘宝30天历史最低价 编辑:程序博客网 时间:2024/05/24 06:34
Heavy Transportation
Time Limit: 3000MS
Memory Limit: 30000KTotal Submissions: 39744
Accepted: 10443

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

13 31 2 31 3 42 3 5

Sample Output

Scenario #1:4


题意分析:Hugo想要扩展他的公司,他有起重机要到目的地,到达目的地有很多条路径,但是,每一条路都有相应承重量,现在需要找出到达目的地的最大承重道路的承重质量。


解题分析:首先,每一条路径的承重量取决于承重量最小的那条道路(短板效应),所以就是找所有路径的最小值,然后选择最小值最大的路径,输出其最小值,这是最短路的变种,需要运用松弛操作,遍历所有路径,记录每条路径的最小值,同时选择同目的地路径最大值。


AC code

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int map[1010][1010];const int integer = 99999999;int main(void){int a;int t=0;scanf("%d",&a);while(a--){int b,c;int i,j;int d[1010];int v[1010];int site1,site2,dis;scanf("%d%d",&b,&c);memset(v,0,sizeof(v));//切记初始化,要不然会一直 Runtime Error ; for(i=0;i<1001;i++)   for(j=0;j<1001;j++)        map[i][j] = 0 ;//转换为邻接矩阵 for(i=0;i<c;i++){scanf("%d%d%d",&site1,&site2,&dis);map[site1-1][site2-1] = map[site2-1][site1-1] =  dis;}//记录0到所有路径的初值 for(i=0;i<b;i++)d[i] = map[0][i];for(i=0;i<c;i++){int m = 0; int x=0;for(j=0;j<b;j++){if(m <= d[j] && !v[j]){x = j;m = d[j];}}v[x] = 1;//松弛操作 for(j=0;j<b;j++)d[j] = max(d[j],min(d[x],map[x][j]));} /*松弛解释:首先通过赋初值给出了从0到所有点的距离,同时在通过0的所有路径 由大到小找到找到最大值,找到连接点j,标记此点,使得下一次就不再遍历,同时以该点为中转点,如有路径j-k,可以选择0-k还是0-j-k,选择路径并记录,重复此操作,得到最终结果。 */ printf("Scenario #%d:\n",++t);//本题出发点是 ,终点是b-1点; printf("%d\n\n",d[b-1]);}return 0;}



原创粉丝点击