Heavy Transportation
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 18592 | Accepted: 4915 |
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个城市组成的交通系统。 道路链接着两个城市,给定每条道路的承载量(无方向)。求从1号城市到n号城市的最大承载量。 例如样例:从1号城市到3号城市有两条路径,如图: 第一条路径为绿颜色的:承载量为4; 第二条路径为红颜色的:承载量为min(3,5)=3, 故从1到3的最大承载量为4。 思路:初看之下以为这条题是网络流,其实非也。本题求的从1到n的所有路径中最小段路径的最大值. 数学一点就是:定义一个函数f(1,a1,a2...,n)=min{Map[i][j],其中Map[i][j]表示从i城市到j城市的道路承载量,i,j属于集合(1,a1,a2...n)}; 由于从1到n的路径有很多条,即集合(1,a1,a2...n)有多个。答案为ans=max{f(i,a1,a2....n)};说了这么多,我不知道自己表达清楚了没有,重点还是把那句话理解清楚就行, 我数学概括能力确实有待提高。 因此,这道完全就是可以按照最短路的方法处理,Dijkstra的时候,在进行松弛操作的时候有点不同。 以前的松弛操作是:dist[v]=min(dist[v],dist[u]+w(u,v)); 而现在则变成了: dist[v]=max(dist[v],min(dist[v],w(u,v))); 给几组数据 5 7 1 3 4 1 2 3 3 2 5 3 5 9 2 5 9 2 4 5 2 4 10 //4 5 7 1 3 4 1 2 3 3 5 6 3 2 2 2 5 9 2 4 5 1 4 10 //5/* @author : liuwen */ #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <cstdlib> #include <queue> #include <stack> #include <map> #include <vector> #include <cmath> using namespace std; const int maxn=1005; const int inf=0x1ffffff; int Map[maxn][maxn],dist[maxn],n,m; void initial_map() { for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++) Map[i][j]=(i==j)?0:-inf; } } void Dijkstra() { bool p[maxn]; memset(p,0,sizeof(p)); for(int i=1;i<=n;i++) dist[i]=Map[1][i]; dist[1]=0; p[1]=true; int i,j,k,Max=-inf; for(i=1;i<=n-1;i++){ Max=-inf,k=0; for(j=1;j<=n;j++){ if(!p[j]&&Max<dist[j]){ Max=dist[j]; k=j; } } if(k==0) return; p[k]=true; for(j=1;j<=n;j++){ if(!p[j]&&Map[k][j]!=-inf){ dist[j]=max(min(dist[k],Map[k][j]),dist[j]);//现在的松弛操作,跟以往的松弛操作不同 } } } } int main() { // freopen("in.txt","r",stdin); int T,cas=0; scanf("%d",&T); while(T--){ initial_map(); scanf("%d%d",&n,&m); while(m--){ int u,v,w; scanf("%d%d%d",&u,&v,&w); Map[u][v]=max(Map[u][v],w); //无向图 Map[v][u]=max(Map[v][u],w); } Dijkstra(); printf("Scenario #%d:\n",++cas); printf("%d\n\n",dist[n]); } return 0; }