# POJ2263 Heavy Cargo用最短路得到最大流

Heavy Cargo
 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4380 Accepted: 2288

Description

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;

#define MAX 9999999

#define LEN 210

int G[LEN][LEN]; //某点到某点两点间的的距离
int dist[LEN]; //记录当前点到源点的最短路径长度
int mark[LEN]; //加入进来的点的集合

//初始化map为正无穷大
void init()
{
int i,j;
for(i=0;i<LEN;i++)
{
for(j=0;j<LEN;j++)
{
G[i][j]=0;
}
}
}

//n:多少条路 start:起始点
//dist[i],最后存储着start 到i点的最短距离
void myDijstra(int n,int start)
{
int i,j,max,pos;
for(i=1;i<=n;i++)
{
mark[i]=0;//没有点加入
dist[i]=G[start][i];//把start 附近点 dis[]初始化
}

mark[start]=1;//把起始点加进来
dist[start]=0;

for(i=1;i<=n;i++)
{
max=0;
for(j=1;j<=n;j++)
{
if(!mark[j] && dist[j] > max)
{
max=dist[j];
pos=j;//标记
}
}

if(max == 0)//已经不能通了
break;

mark[pos]=1;//把K加进来

//做松弛操作
for(j=1;j<=n;j++)
{
if(!mark[j] && G[pos][j] ) //start->j or start->pos,pos->j
{
if(dist[pos] > G[pos][j])
dist[j] = G[pos][j] > dist[j] ? G[pos][j] : dist[j];
else
dist[j] = dist[pos] > dist[j] ? dist[pos] : dist[j];
}
}
}
}

int main()
{
int i,ppp = 1;

int s, e, len, k = 1;

char s1[35],s2[35];
map<string, int>m;
int n, r;
while(scanf("%d%d", &n, &r), n + r)
{
k = 1;
init();
for(i = 0; i < r; i++)
{
scanf("%s%s%d", s1, s2, &len);
if(m.find(s1) == m.end())
m[s1] = s = k++;
else
s = m[s1];
if(m.find(s2) == m.end())
m[s2] = e = k++;
else
e = m[s2];
G[s][e] = G[e][s] = len;
}
scanf("%s%s",s1,s2);
s = m[s1];
e = m[s2];
myDijstra(n,s);//调用方法(点数，起始点)
printf("Scenario #%d\n",ppp++);
printf("%d tons\n",dist[e]);
printf("\n");
m.clear();
}
return 0;
}

#### POJ 2263 Heavy Cargo(Floyd变形)

2014-07-13 15:06:41

#### (Relax dijkstra1.2)POJ 2263 Heavy Cargo(使用dijkstra来求解最大生成树问题)

2013-11-24 15:51:30

#### POJ2263:Heavy Cargo

2012-04-10 13:45:40

#### POJ2263 Heavy Cargo

2016-05-19 23:37:32

#### lingo求解最短路和最大流问题

2016-01-02 10:39:33

#### poj2263 Heavy Cargo(dijstra 变形）

2013-09-22 19:56:58

#### uva 544 - Heavy Cargo（生成树）

2015-09-12 22:19:09

#### UVa 10806 Dijkstra, Dijkstra (无向图+最小费用最大流+最短路:)

2015-08-07 20:22:41

#### Heavy Cargo 最短路变种

2012-08-15 23:06:09

#### hdu5294（最大流+最短路）Tricks Device

2016-04-08 22:04:57