POJ 2263
2013-10-03 01:50
120 查看
Heavy Cargo
Description
Big Johnsson Trucks Inc. is a company specialized in manufacturing big trucks. Their latest model, the Godzilla V12, is so big that the amount of cargo you can transport with it is never limited by the truck itself. It is only limited by the weight restrictions
that apply for the roads along the path you want to drive.
Given start and destination city, your job is to determine the maximum load of the Godzilla V12 so that there still exists a path between the two specified cities.
Input
The input will contain one or more test cases. The first line of each test case will contain two integers: the number of cities n (2<=n<=200) and the number of road segments r (1<=r<=19900) making up the street network.
Then r lines will follow, each one describing one road segment by naming the two cities connected by the segment and giving the weight limit for trucks that use this segment. Names are not longer than 30 characters and do not contain white-space characters.
Weight limits are integers in the range 0 - 10000. Roads can always be travelled in both directions.
The last line of the test case contains two city names: start and destination.
Input will be terminated by two values of 0 for n and r.
Output
For each test case, print three lines:
a line saying "Scenario #x" where x is the number of the test case
a line saying "y tons" where y is the maximum possible load
a blank line
Sample Input
Sample Output
Source
Ulm Local 1998
你有一辆载重无限货车,但是每条路的承重是有限的,现在从s到t,最多能载重多少?
设在最大生成树中从s到t路径上最小的边权为W1
设在原图中任意一条从s到t的路径上最小的边权是Wi
则 W1>=Wi
证明(基于克鲁斯卡尔算法):
假设存在Wi>W1那么在算法的排序步骤中Wi必然排在W1前面,因为Wi又是s到t路径上最小的边,所以处理到Wi的时候s到t路径上的所有边都会在生成树中。又因为在一颗树中从s到t只有唯一的路径,这与W1是最大生成树上的边矛盾!
所以求最大生成树再去求s到t路径上的最小权值就可以了。
也可以这样做,排序边,把边依次加入图中直到s到t连通,最后加的边的权值就是答案,这本质上还是运用上述结论。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2854 | Accepted: 1576 |
Big Johnsson Trucks Inc. is a company specialized in manufacturing big trucks. Their latest model, the Godzilla V12, is so big that the amount of cargo you can transport with it is never limited by the truck itself. It is only limited by the weight restrictions
that apply for the roads along the path you want to drive.
Given start and destination city, your job is to determine the maximum load of the Godzilla V12 so that there still exists a path between the two specified cities.
Input
The input will contain one or more test cases. The first line of each test case will contain two integers: the number of cities n (2<=n<=200) and the number of road segments r (1<=r<=19900) making up the street network.
Then r lines will follow, each one describing one road segment by naming the two cities connected by the segment and giving the weight limit for trucks that use this segment. Names are not longer than 30 characters and do not contain white-space characters.
Weight limits are integers in the range 0 - 10000. Roads can always be travelled in both directions.
The last line of the test case contains two city names: start and destination.
Input will be terminated by two values of 0 for n and r.
Output
For each test case, print three lines:
a line saying "Scenario #x" where x is the number of the test case
a line saying "y tons" where y is the maximum possible load
a blank line
Sample Input
4 3 Karlsruhe Stuttgart 100 Stuttgart Ulm 80 Ulm Muenchen 120 Karlsruhe Muenchen 5 5 Karlsruhe Stuttgart 100 Stuttgart Ulm 80 Ulm Muenchen 120 Karlsruhe Hamburg 220 Hamburg Muenchen 170 Muenchen Karlsruhe 0 0
Sample Output
Scenario #1 80 tons Scenario #2 170 tons
Source
Ulm Local 1998
你有一辆载重无限货车,但是每条路的承重是有限的,现在从s到t,最多能载重多少?
设在最大生成树中从s到t路径上最小的边权为W1
设在原图中任意一条从s到t的路径上最小的边权是Wi
则 W1>=Wi
证明(基于克鲁斯卡尔算法):
假设存在Wi>W1那么在算法的排序步骤中Wi必然排在W1前面,因为Wi又是s到t路径上最小的边,所以处理到Wi的时候s到t路径上的所有边都会在生成树中。又因为在一颗树中从s到t只有唯一的路径,这与W1是最大生成树上的边矛盾!
所以求最大生成树再去求s到t路径上的最小权值就可以了。
也可以这样做,排序边,把边依次加入图中直到s到t连通,最后加的边的权值就是答案,这本质上还是运用上述结论。
#include<cstdio> #include<cstring> #include<algorithm> #include<map> #include<string> #define maxn 209 #define maxm 40000 #define INF 999999999 using namespace std; struct Edge { int u,v,w; }; int n,m,tot,ans; int first[maxn]; int next[maxm],u[maxm],v[maxm],w[maxm]; struct Edge edges[maxm]; int p[maxn]; map<string,int>mp; int s,t; char s1[1000],s2[1000]; int findset(int x) { return x==p[x]?x:p[x]=findset(p[x]); } void unionset(int x,int y) { p[findset(x)]=findset(y); } void add(int x,int y,int z) { u[tot]=x;v[tot]=y;w[tot]=z; next[tot]=first[x]; first[x]=tot++; } bool cmp(Edge x,Edge y) { return x.w>y.w; } bool dfs(int cur,int fa) { if(cur==t) { return 1; } for(int e=first[cur];e!=-1;e=next[e]) { if(v[e]!=fa) { if(dfs(v[e],cur)) { ans=min(ans,w[e]); return 1; } } } return 0; } int main() { int cot=0; while(scanf("%d%d",&n,&m),n||m) { int cnt=1;cot++; memset(first,-1,sizeof(first)); tot=0; mp.clear(); for(int i=0;i<m;i++) { int z; scanf("%s%s%d",s1,s2,&z); if(!mp[string(s1)]) { mp[string(s1)]=cnt++; } if(!mp[string(s2)]) { mp[string(s2)]=cnt++; } int x=mp[string(s1)]; int y=mp[string(s2)]; edges[i]=(Edge){x,y,z}; } scanf("%s%s",s1,s2); s=mp[string(s1)]; t=mp[string(s2)]; sort(edges,edges+m,cmp); for(int i=1;i<=n;i++)p[i]=i; for(int i=0;i<m;i++) { int x=edges[i].u; int y=edges[i].v; int z=edges[i].w; if(findset(x)!=findset(y)) { unionset(x,y); add(x,y,z); add(y,x,z); } } ans=INF; dfs(s,-1); printf("Scenario #%d\n",cot); printf("%d tons\n",ans); printf("\n"); } }
相关文章推荐
- POJ 2263 Heavy Cargo(Floyd变形)
- poj 2263 Heavy Cargo 最短路 变形
- 小比赛(POJ 2992,UVALive 4108,POJ 2263,POJ 3617,HDU 5305,POJ 1988,POJ 1679,POJ 3481)
- poj 2263 Heavy Cargo
- poj2263 zoj1952 Heavy Cargo(floyd||spfa)
- poj 2263
- POJ 2263 POJ 1556
- ACM: 最大流问题 图论题 poj 2263
- POJ-2263 Heavy Cargo
- POJ 2263 Heavy Cargo 多种解法
- POJ 2263 最短路 路径上最小值
- poj 2263 Heavy Cargo floyd基础,就是输入的时候处理字符串纠结一点!!!!
- poj2263
- 谁说不能这样做题之关于一道题的四种实现,POJ 2263
- POJ 2263 floyd思想
- POJ 2263 Heavy Cargo 最短路dijkstra
- POJ 2263 Heavy Cargo
- Floyd算法解决POJ 2263
- poj 2263 Heavy Cargo(floyd)
- poj 2263 floyd+map