24h購物| | PChome| 登入
2012-04-16 22:39:42| 人氣1,550| 回應0 | 上一篇 | 下一篇

[UVA] 544 - Heavy Cargo

推薦 0 收藏 0 轉貼0 訂閱站台


  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 ( $2 le n le 200$) and the number of road segments r ( $1 le r le 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


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;
}

台長: Morris
人氣(1,550) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 11222 - Only I did it!
此分類上一篇:[UVA][Greedy] 11369 - Shopaholic

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文