[UVA] 544 - Heavy Cargo
Heavy Cargo
Heavy Cargo |
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 file will contain one or more test cases. The first line of each test case will contain two integers: the number of cities n ( ) and the number of road segments r ( ) 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
SPFA
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef struct {
int to, v;
} Arc;
typedef vector<Arc>::iterator it;
vector<Arc> Link[201];
int Min(int x, int y) {
return x < y ? x : y;
}
int SPFA(int st, int ed, int n) {
queue<int> Q;
int dis[201];
int used[201];
memset(dis, 0, sizeof(dis));
memset(used, 0, sizeof(used));
dis[st] = 0xfffffff;
for(it i = Link[st].begin(); i != Link[st].end(); i++) {
if(dis[i->to] < Min(dis[st], i->v)) {
dis[i->to] = Min(dis[st], i->v);
if(used[i->to] == 0) {
used[i->to] = 1;
Q.push(i->to);
}
}
}
int tv;
while(!Q.empty()) {
tv = Q.front();
Q.pop();
used[tv] = 0;
for(it i = Link[tv].begin(); i != Link[tv].end(); i++) {
if(dis[i->to] < Min(dis[tv], i->v)) {
dis[i->to] = Min(dis[tv], i->v);
if(used[i->to] == 0) {
used[i->to] = 1;
Q.push(i->to);
}
}
}
}
return dis[ed];
}
int main() {
int n, r, Case = 0, i;
while(scanf("%d %d", &n, &r) == 2) {
if(n == 0 && r == 0)
break;
map<string, int> record;
for(i = 0; i <= n; i++)
Link[i].clear();
int encode = 0, v, nx, ny;
string x, y;
while(r--) {
cin >> x >> y >> v;
if(record[x] == 0) {
encode ++;
record[x] = encode;
}
if(record[y] == 0) {
encode++;
record[y] = encode;
}
nx = record[x], ny = record[y];
Arc arc;
arc.to = ny, arc.v = v;
Link[nx].push_back(arc);
arc.to = nx, arc.v = v;
Link[ny].push_back(arc);
}
cin >> x >> y;
printf("Scenario #%d\n", ++Case);
nx = record[x], ny = record[y];
printf("%d tons\n", SPFA(nx, ny, n));
puts("");
}
return 0;
}