[UVA] 544 - Heavy Cargo@Morris' Blog|PChome Online 人新台
2012-04-16 22:39:42| 人1,590| 回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,590) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA] 11222 - Only I did it!
此分上一篇:[UVA][Greedy] 11369 - Shopaholic

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86