Heavy Cargo
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2696 | Accepted: 1498 |
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.
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.
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
这是一道变形的floyd。他不是要求最短的路径,而是要求最大容量的路。可以借用floyd的思想来完成这个题。他将递推公式加以了改变。
下面是代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define maxc 300 #define inf 1000000000 #define max(a,b) (a>b?a:b) #define min(a,b) (a<b?a:b) int caseno=0; int n,r; int w[maxc][maxc]; char city[maxc][30]; char start[30],dest[30]; int numcity; //城市名在city数组中的序号 using namespace std; int index(char* s){//求出输入的城市在city数组里面的位置 int i; for(i=0;i<numcity;i++){ if(!strcmp(city[i],s)) return i; } strcpy(city[i],s);//如果不存在就将他追加到city数组里去 numcity++; return i; } int read(){ int i,j,k,limit; scanf("%d%d",&n,&r); if(n==0) return 0; for(i=0;i<n;i++) for(j=0;j<n;j++){ w[i][j]=0; } for(i=0;i<n;i++) w[i][i]=inf; numcity=0; for(k=0;k<r;k++){ scanf("%s%s%d",start,dest,&limit); i=index(start); j=index(dest); w[i][j]=w[j][i]=limit; } scanf("%s%s",start,dest); return 1; } void slove(){ int i,j,k; for(k=0;k<n;k++) for(i=0;i<n;i++) for(j=0;j<n;j++){ w[i][j]=max(w[i][j],min(w[i][k],w[k][j]) );//核心关键 递推关系式 } i=index(start); j=index(dest); printf("Scenario #%d\n",++caseno); printf("%d tons\n\n",w[i][j]); } int main(){ while(read()) slove(); return 0; }