poj 1797 Heavy Transportation 最短路径的另一用法

来源:互联网 发布:wings 知乎 ti6 编辑:程序博客网 时间:2024/05/08 19:17

C - Heavy Transportation
Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 1797

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


解题分析:

题目大致意思就是找出从起点到终点的最大物流(其中每条路由最大限重)。运用最短路的另一思想

       例:                    

map[3][1]=4;   dis[3]=4;map[2][1]=;   dis[2]=3;经过第一轮循环找到最大的物流值    dis[3]=4;   并且标记,接下来更新路径,但是发现没有间接经过点3的。故此此路径就是为此路上的最大物流值。接着再次循环,找出最大的物流值,dis[2]=3;   并且标记。更新路径。发现在3-------->1过程中,可以间接经过2.就是  3------->2------->1;于是找出第二条路中所出现的最小物流,就是本条路的最大物流值。之后再于3----->1这天的最大物流值对比,留取最大的即为3----->1的最大物流值。


               

#include<cstdio>#include<iostream>#include<cstring>using namespace std;int  map[1005][1005];int dis[1005];bool mark[1005];int m,n;void dijstra(){int max;int k;int i,j;int p,temp;memset(mark,0,sizeof(mark));for(i=2;i<=n;i++)    dis[i]=map[i][1];for(i=2;i<=n;i++){    max=0;for(j=2;j<=n;j++){if(!mark[j]&&dis[j]>max)//找到最大物流{max=dis[j];k=j; } }mark[k]=true;for(p=2;p<=n;p++)//更新物流 {if(!mark[p]&&map[k][p]!=-1){temp=min(dis[k],map[k][p]);//选取这条路中能经过的最小物流         if(dis[p]!=-1)//如果另外一条路是通的,那么就留下物流最大的一条 {dis[p]=temp>dis[p]?temp:dis[p];} else//如果不同,选择第一次的最小物流 {dis[p]=temp;}} }}}int main(){int t;int a,b,c;int i;int sum=1;scanf("%d",&t);while(t--){memset(map,-1,sizeof(map));scanf("%d%d",&n,&m);for(i=0;i<m;i++){scanf("%d%d%d",&a,&b,&c);{map[a][b]=c;map[b][a]=c;}} dijstra();printf("Scenario #%d:\n%d\n\n",sum++,dis[n]);}return 0;} 

    








0 0