Description
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
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
- 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算法
AC代碼
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int map[205][205], num_space = 1;
char a[50], b[50], name[205][50]; //name[]存放地址名
int find(char *a) //地點與下標映射
{
for(int i = 1; i < 201; i++)
if(strcmp(a, name[i]) == 0)
return i;
int len = strlen(a);
strcpy( name[num_space], a );
name[num_space][len] = '\0';
num_space++;
return num_space - 1;
}
int main()
{
int n, m, weight, cases = 1;
while( scanf("%d%d", &n, &m) && (n || m) )
{
memset(map, -1, sizeof(map)); //map[]初始化,兩城鎮如果不通,公路負重記爲-1
memset(name, 0, sizeof(name));
for(int h = 0; h < m; h++)
{
scanf("%s%s%d", a, b, &weight);
int t1 = find(a);
int t2 = find(b);
map[t1][t2] = map[t2][t1] = weight;
}
for(int k = 1; k <= n; k++)
for(int j = 1; j <= n; j++)
for(int i = 1; i <= n; i++)
map[i][j] = max( map[i][j], min(map[i][k], map[k][j]) ); //min(map[i][k], map[k][j])是可替代路的最大承壓
printf("Scenario #%d\n", cases++);
scanf("%s%s", a, b);
int t1 = find(a);
int t2 = find(b);
printf("%d tons\n\n", map[t1][t2]);
}
return 0;
}