POJ2263 Heavy Cargo


Heavy Cargo
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4004   Accepted: 2124

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



Floyd直接解,唯一的难点是城市名称与图上点的对应,但是有STL还怕什么呢?

用STL的map保存城市和点的映射以后跑一遍floyd就行

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<map>
 6 #include<cstring>
 7 using namespace std;
 8 map<string,int>mp;
 9 int m[200][200];
10 int n,r;
11 int cnt=0;
12 int anscnt=0;
13 void floyd(){
14     int i,j,k;
15     for(i=1;i<=n;i++)
16       for(j=1;j<=n;j++)
17         for(k=1;k<=n;k++){
18             m[i][j]=max(m[i][j],min(m[i][k],m[k][j]));
19         }
20     return;
21 }
22 int main(){
23     while(scanf("%d%d",&n,&r)!=EOF && n!=0 &&r!=0){
24         memset(m,0,sizeof(m));
25         mp.clear();
26         cnt=0;
27         int i,j;
28         for(i=1;i<=n;i++)m[i][i]=1000000;
29         //初始化 
30         string u,v;//为了用STL的map,开了string 
31         int x;
32         for(i=1;i<=r;i++){
33             cin>>u>>v>>x;
34             if(!mp.count(u))mp[u]=++cnt;//城市名与结点对应 
35             if(!mp.count(v))mp[v]=++cnt;
36 //            cout<<u<<"  "<<v<<"  "<<cnt<<" "<<x<<endl;//测试 
37             m[mp[u]][mp[v]]=m[mp[v]][mp[u]]=x;
38         }
39         floyd();
40         cin>>u>>v;
41         printf("Scenario #%d
",++anscnt);
42         printf("%d tons

",m[mp[u]][mp[v]]);
43     }
44     return 0;    
45 }



原文地址:https://www.cnblogs.com/SilverNebula/p/5550576.html