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 ...
随机推荐
- eclipse异常解决:Errors occurred during the build.........
在MyEclipse下编辑文件保存时,编译java工程,出现以下弹出提示.每次保存都会弹出,不堪其扰. "Errors occurred during the build.......... ...
- Python中四种样式的99乘法表
1.常规型. #常规型 i=1 while i<=9: j=1 while j<=i: print(''%d*%d=%2d''%(i,j,i*j),end='') i+=1 #等号只是用来 ...
- 【Python】 用户图形界面GUI wxpython I 基本用法和组件
wxpython - 基本用法和组件 wxpython是python对跨平台GUI库wxWidgets的封装.wxWidgets是由C++写成的. wxpython被包装进了wx模块中,用它设计GUI ...
- [Java] JDK 环境配置(图文)
Windows10 上的安装配置 1.前往 JDK 官网下载对应 jdk 版本安装包: http://www.oracle.com/technetwork/java/javase/downloads/ ...
- Notepad++中实现Markdown语法高亮与实时预览
Notepad ++是一个十分强大的编辑器,除了可以用来制作一般的纯文字说明文件,也十分适合编写计算机程序代码.Notepad ++不仅有语法高亮度显示,也有语法折叠功能,并且支持宏以及扩充基本功能的 ...
- [Java反射机制]用反射改进简单工厂模式设计
如果做开发的工作,工厂设计模式大概都已经深入人心了,比较常见的例子就是在代码中实现数据库操作类,考虑到后期可能会有数据库类型变换或者迁移,一般都会对一个数据库的操作类抽象出来一个接口,然后用工厂去获取 ...
- Idea简单SpringMVC框架配置
前边已经介绍过了Struts在Idea上的配置,相对于Struts来说,我觉得SpringMVC有更多的优势,首先Struts是需要对action进行配置,页面发送不同的请求,就需要配置不同的acti ...
- beta冲刺 用户使用调查报告
测评结果 一.使用体验 数据加载响应很快,页面切换丝滑流畅. UI有点偏暗,有些字被覆盖了. 页面布局过于居中,两侧空白范围较大. 总体功能完善. 二.登录.注册.忘记密码界面 管理员登录按钮太靠下, ...
- 冲刺总结随笔(Alpha)
冲刺总结随笔 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.项目预期进展及现实进展 项目预期 ...
- Beta版本展示博客
1 团队介绍 团队组成: 齐爽爽(258)个人博客:http://www.cnblogs.com/shuangshuangblog/ 马帅(248)个人博客:http://www.cnblogs.co ...