最大生成树POJ2263Heavy Cargo解题报告
来源:互联网 发布:网络大电影招商案 编辑:程序博客网 时间:2024/05/21 03:18
Heavy Cargo
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3877 Accepted: 2060
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
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
这题跟POJ1797一个模型。
难点是将城市转换成数组下标。就是一个string对应一个int,因此可以用map
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#define MAXN 205#define INF 0xffffffusing namespace std;int father[MAXN];struct Road{ int a,b,w;}road[20000];int findRoot(int x){ return father[x]==x?x:father[x]=findRoot(father[x]);}bool cmp(Road a,Road b){ return a.w>b.w;}int main(){ freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); int n,r,cases=1; while(scanf("%d%d",&n,&r)&&n){ map<string,int> MyMap; char a[35],b[35],from[35],to[35]; int _count=1; for(int i=0;i<r;i++){ scanf("%s%s%d",&a,&b,&road[i].w); if(!MyMap[a])//a城市还未出现过 MyMap[a]=_count++;//给a城市编号 if(!MyMap[b])//b城市未出现过 MyMap[b]=_count++;//给b城市编号 road[i].a=MyMap[a],road[i].b=MyMap[b]; } scanf("%s%s",&from,&to); sort(road,road+r,cmp); for(int i=0;i<n;i++)father[i]=i;//初始化集合 int ans=INF; for(int i=0;i<r;i++){//对于每一边 int rootA=findRoot(road[i].a),rootB=findRoot(road[i].b);//每一边两个顶点 if(rootA!=rootB){ father[rootA]=rootB; if(ans>road[i].w){ ans=road[i].w; } if(findRoot(MyMap[from])==findRoot(MyMap[to])) break; } } printf("Scenario #%d\n%d tons\n\n",cases++,ans); } return 0;}
- 最大生成树POJ2263Heavy Cargo解题报告
- poj2263Heavy Cargo floyd
- 最大生成树POJ1797Heavy Transportation解题报告
- POJ3723 Conscription 解题报告【Kruskal/最大生成树】
- poj 3723 Conscription 最大生成树 解题报告
- hdu 3367 Pseudoforest 变形最大生成树 解题报告
- hdu 3367 Pseudoforest (伪森林) not 最大生成树 解题报告
- NOIP2013 货车运输 解题报告(最大生成树+倍增lca)
- Conscription(POJ NO.3723)解题报告(最大生成树Kruskal算法)
- codevs 3287||NOIP 2013 货车运输 最大生成树+倍增 解题报告
- (Relax dijkstra1.2)POJ 2263 Heavy Cargo(使用dijkstra来求解最大生成树问题)
- 最小生成树——解题报告
- BZOJ 2429 最小生成树 解题报告
- codevs 1078 最小生成树 解题报告
- uva 544 - Heavy Cargo(生成树)
- 【最大的算式】解题报告
- 查找最大元素解题报告
- [NOIP2000] 乘积最大-解题报告
- LeetCode----Reverse Linked List II
- 2.2栈
- Oracle EBS 用户配置文件
- python之re库
- java中用dom4j对xml文件进行操作
- 最大生成树POJ2263Heavy Cargo解题报告
- 警惕 Xcode 木马
- C#中Winform程序调用WebService服务
- iOS的KVO底层实现原理
- 聚类与分类的区别
- 理解透明物体渲染
- poj 2138 字符串dp
- Android Studio
- 记录——《C Primer Plus (第五版)》第七章编程练习第四题