(beginer) 最小生成树 UVA 544 Heavy Cargo
2014-02-08 09:55
302 查看
Heavy Cargo |
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 file will contain one or more test cases. The first line of eachtest case will contain two integers: the number of cities n (
![](http://uva.onlinejudge.org/external/5/544img1.gif)
) and the number of road
segments r (
![](http://uva.onlinejudge.org/external/5/544img2.gif)
)
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 3Karlsruhe 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 #180 tons
Scenario #2
170 tons
题意:给出起点和终点,找出一条路能使货车在限制条件下容量最大。
思路:一开始图没有边,然后我们将边从大到小进行排序,然后从大到小进行加边,加完后,dfs一下,找到路径中容量限制最小的就是答案。
代码:
#include<iostream> #include<cstdio> #include<string> #include<string.h> #include<cstring> #include<map> #include<algorithm> #include<vector> using namespace std; const int maxn = 200+10; const int maxm = 19910; const int inf = 1e9; int n , m , s , t , size , ans; map<string,int> cityno; int p[maxn]; int find(int x) { if (x==p[x]) return x; return p[x] = find(p[x]); } struct Edge { Edge(int uu,int vv,int ww) :u(uu) , v(vv) , w(ww) { } Edge() { } int u , v; int w; }edge[maxm]; inline bool operator < (const Edge&e1,const Edge&e2) { return e1.w > e2.w; } vector<Edge> G[maxn]; void input() { cityno.clear(); string str; char buffer[50]; int u , v , w; size = 0; for (int i = 0 ; i < m ; ++i) { scanf("%s",buffer); str = buffer; if (cityno.count(str)) u = cityno[str]; else u = cityno[str] = ++size; scanf("%s",buffer); str = buffer; if (cityno.count(str)) v = cityno[str]; else v = cityno[str] = ++size; scanf("%d",&w); edge[i] = Edge(u,v,w); } scanf("%s",buffer); str = buffer; if (cityno.count(str)) s = cityno[str]; else s = cityno[str] = ++size; scanf("%s",buffer); str= buffer; if (cityno.count(str)) t = cityno[str]; else t = cityno[str] = ++size; } bool dfs(int u , int fa) { if (u==t) return true; for (int i = 0 ; i < G[u].size() ; ++i) { int v = G[u][i].v , w = G[u][i].w; if (v==fa) continue; if (dfs(v,u)) { ans = min(w,ans); return true; } } return false; } void solve() { for (int i = 1 ; i <= n ; ++i) G[i].clear() , p[i] = i; sort(edge,edge+m); for (int i = 0 ; i < m ; ++i) { int u = find(edge[i].u); int v = find(edge[i].v); if (u==v) continue; p[u] = v; G[u].push_back(Edge(u,v,edge[i].w)); G[v].push_back(Edge(v,u,edge[i].w)); } ans = inf; dfs(s,-1); printf("%d tons\n\n",ans); } int main() { int k = 0; while (scanf("%d%d",&n,&m)==2,n+m) { input(); ++k; printf("Scenario #%d\n",k); solve(); } }
相关文章推荐
- UVa 544 Heavy Cargo( 最小生成树的variant)
- uva 544 - Heavy Cargo(生成树)
- UVA 544 - Heavy Cargo(最小瓶颈路)
- (beginer) 最小生成树 UVA 1395 Slim Span
- (beginer) 最小生成树 UVA 10600 ACM Contest and Blackout
- (beginer) 最小生成树 UVA 11747 Heavy Cycle Edges
- UVA 1395 Slim Span(最小生成树)
- 最小生成树(苗条生成树 uva 1395)
- UVA1395 Slim Span(枚举最小生成树)
- UVa 11354 - Bond(最小生成树+倍增lca)
- uva 10034(最小生成树)
- UVa 10147 Highways(最小生成树,水)
- (intermediate) 最小生成树 UVA 1504 Genghis Khan the Conqueror
- Kingdoms UVA - 12507 状压||dfs搜索+最小生成树
- uva 10453 Make Palindrome(dp,最小回文生成)
- UVA 10397 - Connect the Campus(最小生成树)
- POJ 3026 Borg Maze & UVA 10307 Killing Aliens in Borg Maze(BFS,最小生成树)
- Another Minimum Spanning Tree - UVaLive 3662 曼哈顿最小生成树
- 【UVA 10369】 Arctic Network (最小生成树)
- 【UVA 1151】 Buy or Build (有某些特别的东东的最小生成树)