Heavy Cargo POJ 2263 (Floyd传递闭包)

Posted wkfvawl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Heavy Cargo POJ 2263 (Floyd传递闭包)相关的知识,希望对你有一定的参考价值。

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

题目意思:有n个城市,m条连接两个城市的道路,每条道路有自己的最大复载量。现在问从城市a到城市b,车上的最大载重能为多少。

解题思路:这里并不是求解最短路径,而是要求通行能力(可称为容量)最大的路,这种路可以成为最大容量路,可以用Floyd算法求最短路径的思想求解。
 w[i][j]=max(w[i][j],min(w[i][k],w[k][j]))
这里需要好好的思考一下,对于点i和点j,中间点的加入更改的递推式应该取最大值,因为求的就是最大的容量值,而对于新加进来的i-k和k-j必须取小的值,因为小的值才是这条路的容量值,

三重循环遍历之后就求出了每两点之间的最大容量值。注意初始化的时候w应该都为0,因为求的是最大值。

 1 #include<cstring>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #define INF 0x3f3f3f3f
 5 using namespace std;
 6 int w[210][210];
 7 char start[30];
 8 char dest[30];
 9 int n,m;
10 int num;
11 char city[210][30];///城市名
12 void Floyd()
13 {
14     int i,j,k;
15     for(k=0; k<n; k++)
16     {
17         for(i=0; i<n; i++)
18         {
19             for(j=0; j<n; j++)
20             {
21                 w[i][j]=max(w[i][j],min(w[i][k],w[k][j]));
22             }
23         }
24     }
25 }
26 int index(char *s)///给城市分配编号
27 {
28     int i;
29     for(i=0; i<num; i++)
30     {
31         if(!strcmp(city[i],s))
32         {
33             return i;
34         }
35     }
36     strcpy(city[i],s);
37     num++;
38     return i;
39 }
40 int main()
41 {
42     int i,j,limit;
43     int counts;
44     int a,b;
45     counts=1;
46     while(scanf("%d%d",&n,&m)!=EOF)
47     {
48         getchar();
49         if(n==0)
50         {
51             break;
52         }
53         for(i=0; i<n; i++)///初始化邻接表
54         {
55             for(j=0; j<n; j++)
56             {
57                 w[i][j]=0;
58             }
59         }
60         for(i=0; i<n; i++)
61         {
62             w[i][i]=INF;
63         }
64         num=0;
65         for(i=0; i<m; i++)
66         {
67             scanf("%s%s%d",start,dest,&limit);
68             getchar();
69             a=index(start);
70             b=index(dest);
71             w[a][b]=w[b][a]=limit;///双向
72         }
73         Floyd();
74         scanf("%s%s",start,dest);///读入起点城市和终点城市
75         a=index(start);
76         b=index(dest);
77         printf("Scenario #%d
",counts++);
78         printf("%d tons
",w[a][b]);
79         printf("
");
80     }
81     return 0;
82 }

 

 

以上是关于Heavy Cargo POJ 2263 (Floyd传递闭包)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 1797 Heavy Transportation

poj 1797 Heavy Transportation

POJ 1797 Heavy Transportation (Dijkstra)

POJ-1797 Heavy Transportation( 最短路 )

POJ 1797 Heavy Transportation

POJ 1797 Heavy Transportation (最大生成树)