Heavy Transportation
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 21632 | Accepted: 5754 |
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.
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
題目大意:
首先輸入n個城市,m條街道,然後輸入每兩個城市之間的最大載容量,判斷從1號城市到n號城市的最大載容量是多少的問題。因爲要從1號城市往n號城市派一輛汽車過去,當然要滿足汽車的載重最大了,但是每條街道又有自己的載容量限制,所以我們只需要對於Dijkstra()稍作一些修改就好。
解題思路:
在Dijkstra()算法中,以前求得的是最短路,判斷的條件是:
if ( book[j] == 0 && dis[j] < _min )
{
_min = dis[j];
u = j;
}
這裏需要把_min改成_max。。。而max的值爲-1.在init()的過程中,也只需在開始的時候將edge[i][j]改爲全部爲0的了。。。
還有一處需要改動的地方,就是在進行邊的鬆弛的過程中,我們要做到的是 if ( book[v]==0&&dis[v] < min( dis[u],edge[u][v]) )
代碼:
# include<cstdio>
# include<iostream>
using namespace std;
# define MAX 1234
int edge[MAX][MAX];
int book[MAX];
int dis[MAX];
int n,m;
int u,v;
void init()
{
for ( int i = 1;i <= n;i++ )
{
for ( int j = 1;j <= n;j++ )
{
edge[i][j] = 0;
}
}
}
void input()
{
for ( int i = 1;i <= m;i++ )
{
int t1,t2,t3;
cin>>t1>>t2>>t3;
edge[t1][t2] = t3;
edge[t2][t1] = t3;
}
}
int Dijkstra()
{
for ( int i = 1;i <= n;i++ )
{
book[i] = 0;
dis[i] = edge[1][i];
}
int _max;
book[1] = 1;
for ( int i = 1;i <= n-1;i++ )
{
_max = -1;
for ( int j = 1;j <= n;j++ )
{
if ( book[j]==0&&dis[j] > _max )
{
_max = dis[j];
u = j;
}
}
book[u] = 1;
for ( v = 1;v <= n;v++ )
{
if ( book[v]==0&&dis[v] < min( dis[u],edge[u][v]) )
{
dis[v] = min( dis[u],edge[u][v] );
}
}
}
return dis[n];
}
int main(void)
{
int t;cin>>t;
int icase = 1;
while ( t-- )
{
cin>>n>>m;
init();
input();
printf("Scenario #%d:\n",icase++);
printf("%d\n\n",Dijkstra());
}
return 0;
}