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 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
题目类型:Floyd思想
算法分析:以读入的字符串构建图,然后使用Floyd算法思想来迭代求解,迭代公式为:edge[i][j] = max (edge[i][j], min (edge[i][k], edge[k][j]))。注意一定要初始化edge[i][j]数组,其中i == j时,edge[i][j] == INF,表示自己到自己的路径上载重无限制。当i != j时,edge[i][j] == 0,表示i到j的路径上载重限制为无穷
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
#include <iostream> #include <fstream> #include <algorithm> #include <iomanip> #include <cstring> #include <cstdio> #include <cmath> #include <map> #include <string> #include <vector> #include <stack> #include <queue> #include <set> #include <list> #include <ctime> using namespace std; const int maxn = 666 + 66; const int INF = 66666666; int edge[maxn][maxn]; int n, r; vector <string> vec; int GetVal (string val) { int i; for (i = 0; i < vec.size (); i++) if (val == vec[i]) return i; vec.push_back (val); return i; } void floyd () { int i, j, k; for (k = 0; k < n; k++) { for (i = 0; i < n; i++) { for (j = 0; j < n; j++) edge[i][j] = max (edge[i][j], min (edge[i][k], edge[k][j])); } } } int main() { // ifstream cin ("aaa.txt"); int flag = 1; while (cin >> n >> r) { if (n == 0 && r == 0) break; vec.clear (); int i, j; for (i = 0; i < maxn; i++) for (j = 0; j < maxn; j++) { if (i == j) edge[i][j] = INF; else edge[i][j] = 0; } string val_a, val_b; int val_w; for (i = 0; i < r; i++) { cin >> val_a >> val_b >> val_w; int u = GetVal (val_a); int v = GetVal (val_b); edge[u][v] = val_w; edge[v][u] = val_w; } floyd (); cout << "Scenario #" << flag++ << endl; cin >> val_a >> val_b; int u = GetVal (val_a); int v = GetVal (val_b); cout << edge[u][v] << " tons" << endl << endl; } return 0; } |
- « 上一篇:zoj1948
- zoj1967:下一篇 »