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\n",counts++);
78         printf("%d tons\n",w[a][b]);
79         printf("\n");
80     }
81     return 0;
82 }

 

原文地址:https://www.cnblogs.com/wkfvawl/p/9556610.html

时间: 2024-09-29 12:44:04

Heavy Cargo POJ 2263 (Floyd传递闭包)的相关文章

Cow Contest POJ - 3660 floyd传递闭包

#include<iostream> #include<cstring> using namespace std; const int N=110,INF=0x3f3f3f3f; int f[N][N]; int main() { int n,m; cin>>n>>m; memset(f,0x3f,sizeof f); int x,y; for(int i=0;i<m;i++) { cin>>x>>y; //x>y f[x

Poj 2263 Heavy Cargo Floyd 求最大容量路

f[i][j] = max(f[i][j],min(f[i][k],f[j][k])) #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cst

POJ 2263 Heavy Cargo(Floyd + map)

Heavy Cargo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3768   Accepted: 2013 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

POJ 2263 Heavy Cargo(二分+并查集)

题目地址:POJ 2263 这题是在网上的一篇关于优先队列的博文中看到的..但是实在没看出跟优先队列有什么关系..我用的二分+并查集做出来了... 二分路的载重量.然后用并查集检查是否连通. 代码如下: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <ctype.h> #

poj2263 Heavy Cargo --- floyd求最大容量路

求给定起点到终点的路径中,最小边权的最大值 #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #define inf 0x3f

POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】

Treasure Exploration Time Limit:6000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2594 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploratio

poj 2263

群 #include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>using namespace std;#include <map>int a[205][205];int main(int argc, char *argv[]){ int m,n,i,j,k,r,p=0; string s1,s2; while(c

POJ3660:Cow Contest(Floyd传递闭包)

Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16941   Accepted: 9447 题目链接:http://poj.org/problem?id=3660 Description: N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all k

POJ 1094 (传递闭包 + 拓扑排序)

题目链接: POJ 1094 题目大意:有 1 ~ N 个大写字母,且从 A 开始依次 N 个.再给你 M 个小于的关系,比如 A < B ,让你判断三种可能: 1.在第 i 个关系罗列之后,是否可以满足使得这 N 个字母能递增关系. 2.在第 i 个罗列之后,是否会出现矛盾,例如 A > B,而在第 i 个状态出现后,B > A ,故矛盾. 3.如果 M 个条件罗列完后都没有出现矛盾,且还无法判断 N 个字母的排列顺序,则输出  Sorted sequence cannot be de