POJ-2263 Heavy Cargo---最短路变形&&最小边的最大值
题目链接:
https://vjudge.net/problem/POJ-2263
题目大意:
有n个城市,m条连接两个城市的道路,每条道路有自己的最大复载量。现在问从城市a到城市b,车上的最大载重能为多少。
思路:
这里求的不是最短路,求的是最大容量路,意思就是每条路的最小边就是这条路的容量值,要求出最大的容量值。可以用Floyd的思想来求解。设Map[i][j]表示从i到j的容量值,递推方程变成:
Map[i][j] = MAX{ Map[i][j], MIN{ Map[i][k], Map[k][j] } 。这里需要好好的思考一下,对于点i和点j,中间点的加入更改的递推式应该取最大值,因为求的就是最大的容量值,而对于新加进来的i-k和k-j必须取小的值,因为小的值才是这条路的容量值,三重循环遍历之后就求出了每两点之间的最大容量值。注意初始化的时候Map应该都为0,因为求的是最大值
其实Dijkstra和Bellman算法也可以求解,同样的松弛方程改成上述含义就可以。
拓展:POJ2253最大边的最小值,思路一样,方程正好相反
Floyd:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<sstream>
#define MEM(a, b) memset(a, b, sizeof(a));
using namespace std;
typedef long long ll;
const int maxn = + ;
const int INF = 0x3f3f3f3f;
int T, n, m, cases, tot;
int Map[maxn][maxn];
map<string, int>id;
set<string>cnt;
int getid(string s)
{
if(cnt.count(s))return id[s];
cnt.insert(s);
return id[s] = cnt.size();
}
int main()
{
while(cin >> n >> m && (n + m))
{
string s1, s2;
int d;
cnt.clear();
id.clear();
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)Map[i][j] = ;
}
for(int i = ; i < m; i++)
{
cin >> s1 >> s2 >> d;
int u = getid(s1);
int v = getid(s2);
//cout<<u<<" "<<v<<endl;
Map[v][u] = Map[u][v] = d;
}
cin >> s1 >> s2;
for(int k = ; k <= n; k++)
{
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
{
Map[i][j] = max(Map[i][j], min(Map[i][k], Map[j][k]));
}
}
}
int u = getid(s1);
int v = getid(s2);
printf("Scenario #%d\n", ++cases);
printf("%d tons\n\n", Map[u][v]);
}
return ;
}
dijkstra:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<sstream>
#define MEM(a, b) memset(a, b, sizeof(a));
using namespace std;
typedef long long ll;
const int maxn = + ;
const int INF = 0x3f3f3f3f;
int T, n, m, cases, tot;
int Map[maxn][maxn];
map<string, int>id;
set<string>cnt;
int d[maxn];
bool v[maxn];
void dijkstra(int u)
{
MEM(v, );
MEM(d, );
d[u] = INF;
for(int i = ; i < n; i++)
{
int x, m = ;//求距离最远的加入
for(int i = ; i <= n; i++)if(!v[i] && d[i] >= m)m = d[x = i];//找到最大的标记
v[x] = ;
//cout<<m<<endl;
for(int i = ; i <= n; i++)d[i] = max(d[i], min(d[x], Map[x][i]));
}
}
int getid(string s)
{
if(cnt.count(s))return id[s];
cnt.insert(s);
return id[s] = cnt.size();
}
int main()
{
while(cin >> n >> m && (n + m))
{
string s1, s2;
int w;
cnt.clear();
id.clear();
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)Map[i][j] = ;
}
for(int i = ; i < m; i++)
{
cin >> s1 >> s2 >> w;
int u = getid(s1);
int v = getid(s2);
//cout<<u<<" "<<v<<endl;
Map[v][u] = Map[u][v] = w;
}
cin >> s1 >> s2;
int u = getid(s1);
int v = getid(s2);
dijkstra(u);
printf("Scenario #%d\n", ++cases);
printf("%d tons\n\n", d[v]);
}
return ;
}
POJ-2263 Heavy Cargo---最短路变形&&最小边的最大值的更多相关文章
- POJ 2263 Heavy Cargo(Floyd + map)
Heavy Cargo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3768 Accepted: 2013 Descr ...
- POJ 2263 Heavy Cargo(ZOJ 1952)
最短路变形或最大生成树变形. 问 目标两地之间能通过的小重量. 用最短路把初始赋为INF.其它为0.然后找 dis[v]=min(dis[u], d); 生成树就是把最大生成树找出来.直到出发和终点能 ...
- POJ 1797 Heavy Transprotation ( 最短路变形 || 最小生成树 )
题意 : 找出 1 到 N 点的所有路径当中拥有最大承载量的一条路,输出这个最大承载量!而每一条路的最大承载量由拥有最大承载量的那一条边决定 分析 : 与 POJ 2253 相似且求的东西正好相反,属 ...
- POJ 1797 Heavy Transportation 最短路变形(dijkstra算法)
题目:click here 题意: 有n个城市,m条道路,在每条道路上有一个承载量,现在要求从1到n城市最大承载量,而最大承载量就是从城市1到城市n所有通路上的最大承载量.分析: 其实这个求最大边可以 ...
- POJ 2263 Heavy Cargo 多种解法
好题.这题可以有三种解法:1.Dijkstra 2.优先队列 3.并查集 我这里是优先队列的实现,以后有时间再用另两种方法做做..方法就是每次都选当前节点所连的权值最大的边,然后BFS搜索. ...
- POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...
- POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...
- POJ 2253 Frogger【最短路变形——路径上最小的最大权】
链接: http://poj.org/problem?id=2253 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ 1797 Heavy Transportation(Dijkstra变形——最长路径最小权值)
题目链接: http://poj.org/problem?id=1797 Background Hugo Heavy is happy. After the breakdown of the Carg ...
随机推荐
- ListIterator
1,ListIterator与Iterator Iterator的功能:next(),hasNext(),remove() 功能太少,因此出现了ListIterator,他的功能要比Iterator多 ...
- C++关联容器知识总结
C++的容器类型可以分为顺序容器和关联容器两大类.顺序容器的知识可以参看我上篇的随笔<C++顺序容器知识总结>.关联容器支持通过键值来高效的查找和读取元素,这是它和顺序容器最大的区别.两种 ...
- Java注解(3)-注解处理器(编译期|RetentionPolicy.SOURCE)
注解的处理除了可以在运行时通过反射机制处理外,还可以在编译期进行处理.在编译期处理注解时,会处理到不再产生新的源文件为止,之后再对所有源文件进行编译. Java5中提供了apt工具来进行编译期的注解处 ...
- PHP 密码重置,发送邮件,随机长度字母数字密码
<?php include ("database.php"); require_once ('email.class.php'); date_default_timezone ...
- TensorFlow-谷歌深度学习库 用tfrecord写入读取
TensorFlow自带一种数据格式叫做tfrecords. 你可以把你的输入转成专属与TensorFlow的tfrecords格式并保存在本地. -关于输入碎碎念:输入比如图片,可以有各种格式呀首先 ...
- spring学习笔记二 注解及AOP
本节需要导入spring-aop包 注解 使用注解的目的是为了代替配置,在使用注解时,省略键时,则是为value赋值. 扫描某个包下的所有类中的注解 <?xml version="1. ...
- 【Spring源码深度解析学习系列】默认标签解析(三)
Spring的标签包括默认标签和自定义标签两种 默认标签的解析方法: ###DefaultBeanDefinitionDocumentReader.java### private void parse ...
- 极光征文 | 写写文章就能赢 Filco,岂不美滋滋
由极光社区举办的第二届征文大赛 --「我和极光的那些事儿」又来啦! 在简书平台发布文章并投稿至「我和极光的那些事」专题,只要参与就能 100% 获得京东购物卡,更有机会赢取象征信仰的 Filco 机械 ...
- 『备注』&#x; 格式 的编码转换
在很多 网站(或者很多 WebService), 我们总能看到 Ӓ &#A22A; 这种格式 的编码. 如何将这种编码 转换成 实际文本,C#代码如下: //各种 幺蛾子网页图标 请参见: ...
- 【alpha冲刺】随笔合集
Daily Scrum Meeting 第一天 [Alpha]Daily Scrum Meeting第一次 第二天 [Alpha]Daily Scrum Meeting第二次 第三天 [Alpha]D ...