Heavy Cargo POJ 2263 (Floyd传递闭包)
Description
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
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
- 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,因为求的是最大值。
#include<cstring>
#include<cstdio>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int w[][];
char start[];
char dest[];
int n,m;
int num;
char city[][];///城市名
void Floyd()
{
int i,j,k;
for(k=; k<n; k++)
{
for(i=; i<n; i++)
{
for(j=; j<n; j++)
{
w[i][j]=max(w[i][j],min(w[i][k],w[k][j]));
}
}
}
}
int index(char *s)///给城市分配编号
{
int i;
for(i=; i<num; i++)
{
if(!strcmp(city[i],s))
{
return i;
}
}
strcpy(city[i],s);
num++;
return i;
}
int main()
{
int i,j,limit;
int counts;
int a,b;
counts=;
while(scanf("%d%d",&n,&m)!=EOF)
{
getchar();
if(n==)
{
break;
}
for(i=; i<n; i++)///初始化邻接表
{
for(j=; j<n; j++)
{
w[i][j]=;
}
}
for(i=; i<n; i++)
{
w[i][i]=INF;
}
num=;
for(i=; i<m; i++)
{
scanf("%s%s%d",start,dest,&limit);
getchar();
a=index(start);
b=index(dest);
w[a][b]=w[b][a]=limit;///双向
}
Floyd();
scanf("%s%s",start,dest);///读入起点城市和终点城市
a=index(start);
b=index(dest);
printf("Scenario #%d\n",counts++);
printf("%d tons\n",w[a][b]);
printf("\n");
}
return ;
}
Heavy Cargo POJ 2263 (Floyd传递闭包)的更多相关文章
- POJ 3275 Floyd传递闭包
题意:Farmer John想按照奶牛产奶的能力给她们排序.现在已知有N头奶牛(1 ≤ N ≤ 1,000).FJ通过比较,已经知道了M(1 ≤ M ≤ 10,000)对相对关系.每一对关系表示为&q ...
- POJ 3660 Floyd传递闭包
题意:牛有强弱,给出一些牛的强弱的胜负关系,问可以确定几头牛的排名. 思路: Floyd传递闭包 // by SiriusRen #include <bitset> #include &l ...
- Cow Contest POJ - 3660 floyd传递闭包
#include<iostream> #include<cstring> using namespace std; ,INF=0x3f3f3f3f; int f[N][N]; ...
- POJ 2263 Heavy Cargo(Floyd + map)
Heavy Cargo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3768 Accepted: 2013 Descr ...
- POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】
Treasure Exploration Time Limit:6000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64 ...
- POJ 3660—— Cow Contest——————【Floyd传递闭包】
Cow Contest Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3660 Cow ContestCow(Floyd传递闭包)题解
题意:给出m个关系,问你能确定机头牛的排名 思路:要确定排名那必须要把他和其他n-1头牛比过才行,所以Floyd传递闭包,如果赢的+输的有n-1就能确定排名. 代码: #include<cstd ...
- POJ2263 Heavy Cargo
Heavy Cargo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4004 Accepted: 2124 Descr ...
- POJ3660:Cow Contest(Floyd传递闭包)
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16941 Accepted: 9447 题目链接 ...
随机推荐
- 浅谈 Virtual DOM 的那些事
背景 我们都知道频繁的dom给我们带来的代价是昂贵的,例如我们有时候需要去更新Table 的部分数据,必须去重新重绘表格,这代价实在是太大了,相比于频繁的手动去操作dom而带来性能问题,vdom很好的 ...
- Excel工作表保护的密码破解与清除...假装自己破解密码系列?
有一次我女朋友让我帮忙解一个excel表格的保护密码,然后~用了宏 网上下载来的Excel经常会有工作表保护,也就是无法修改,妄图做任何修改的时候你就会看见这句话: 您试图更改的单元格或图表位于受保护 ...
- javascript node节点学习
node节点学习 1 . 获取节点(元素)的方法 document.getElementById(); document.getElementsByTagName() document.getElem ...
- PHP 好用第三方库
PHP 好用第三方库 whoops 更好的php错误报告库 [github]:https://github.com/filp/whoops Whoops是一个易于处理和调试错误的PHP库 .它提供基于 ...
- TF-IDF介绍
TF-IDF是什么 TF-IDF是一种统计方法,用以评估一个词对于一篇文章或语料库中一篇文章的重要性.字词的重要性随着它在文件中出现的次数成正比增加,但同时会随着它在语料库中出现的频率成反比下降. T ...
- 100-Days-Of-ML-Code 评注版(Day 2)
Day2_Simple_Linear_Regression(一元线性回归) 本文引用自 Simple Linear Regression, 对其中内容进行了评注与补充说明. 回归分析是一种预测性的建模 ...
- Linux 字符设备驱动—— ioremap() 函数解析
一. ioremap() 函数基础概念 几乎每一种外设都是通过读写设备上的相关寄存器来进行的,通常包括控制寄存器.状态寄存器和数据寄存器三大类,外设的寄存器通常被连续地编址.根据CPU体系结构的不同, ...
- 《Act with Prudence》读后感
<97 Things Every Should Know>中第一个编程方面的建议 文章链接:行事谨慎 很赞同文章中的观点,在做项目中是要谨慎行事和考虑后果.一直在项目前期考虑不够周到,以至 ...
- linux——Shell编程基础
1. shell 脚本的执行方式 1.1 直接绝对路径执行 1.2 相对路径执行 首先进入到shell脚本所造的目录 PS:用./执行要增加x权限.用bash执行可以不增加x权限 1.3 在当前she ...
- IDEA的基本配置
一.安装 常规下载安装,换路径即可. 亲测可用破解方法:进入ide主页面,help-register-license server,然后输入 http://idea.iteblog.com/key.P ...