最短路变形 poj3615& poj2263
问题:
牛要跨过一些障碍,希望以最小的体力跨过障碍,并且对于一条路径,只在乎其中最高的障碍。
输入N代表站点数,标记为1—N,输入M代表路径数,从站点S到E之间需要跨过高度为H的障碍。
输入T代表牛要完成的任务数。对于每个任务,输入A,B,输出一条从站点A到B的路径,使需要跨过的最高障碍为最低。
代码:(Floyd
/*******************************************
Problem: 3615 User:
Memory: 960K Time: 688MS
Language: G++ Result: Accepted
********************************************/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 305; int mp[N][N];
int dis[N], vis[N]; void floyd(int n)
{
int i, j, k;
for (k = 1; k <= n; ++k)
for (i = 1; i <= n; ++i)
for (j = 1; j <= n; ++j)
if (mp[i][j] > max(mp[i][k] , mp[k][j]))
mp[i][j] = max(mp[i][k], mp[k][j]);
} int main()
{
int n, m, t;
while (scanf("%d%d%d", &n, &m, &t) != EOF) {
int i, j;
int a, b, h;
for (i = 1; i <= n; ++i)
for (j = 1; j <= n; ++j)
mp[i][j] = INF;
for (i = 0; i < m; ++i) {
scanf("%d%d%d", &a, &b, &h);
mp[a][b] = h;
}
floyd(n);
for (i = 0; i < t; ++i) {
scanf("%d%d", &a, &b);
printf("%d\n", mp[a][b] == INF ? -1 : mp[a][b]);
}
}
return 0;
}
poj2263
和上题相似,求两点之间最小值最大的路径
代码:(dijkstra
/******************************************
Problem: 2263 User:
Memory: 812K Time: 125MS
Language: G++ Result: Accepted
*******************************************/
#include <iostream>
#include <cstring>
#include <string>
#include <map>
#include <algorithm>
#include <cstdio>
using namespace std; const int N = 205;
const int INF = 0x3f3f3f3f; int mp[N][N];
int vis[N], dis[N];
map<string, int>my_map; int dijkstra(int n, int s, int e)
{
int i, j;
memset(vis, 0, sizeof(vis));
for (i = 1; i <= n; ++i)
dis[i] = mp[s][i];
vis[s] = 1;
for (i = 0; i < n; ++i) {
int max_dis = 0;
int max_x = 1;
for (j = 1; j <= n; ++j) {
if (!vis[j] && dis[j] > max_dis) {
max_dis = dis[j];
max_x = j;
}
}
if (max_x == e) return max_dis;
vis[max_x] = 1;
for (j = 1; j <= n; ++j) {
if (!vis[j] && dis[j] < min(dis[max_x], mp[j][max_x]))
dis[j] = min(dis[max_x], mp[j][max_x]);
}
}
return 0;
} int main()
{
int n, m;
int times = 1;
while (scanf("%d%d", &n, &m) != EOF) {
if (n == 0 && m == 0) break;
int i, j;
my_map.clear();
int cnt = 1;
string stra, strb;
int x, y;
int weight;
for (i = 1; i <= n; ++i)
for (j = 1; j <= n; ++j)
mp[i][j] = 0; for (i = 0; i < m; ++i) {
cin >> stra >> strb >> weight;
if (my_map[stra] == 0) my_map[stra] = cnt++;
if (my_map[strb] == 0) my_map[strb] = cnt++;
x = my_map[stra];
y = my_map[strb];
if (mp[x][y] < weight)
mp[y][x] = mp[x][y] = weight;
}
cin >> stra >> strb;
x = my_map[stra];
y = my_map[strb];
printf("Scenario #%d\n%d tons\n\n", times++, dijkstra(n, x, y)); }
return 0;
}
最短路变形 poj3615& poj2263的更多相关文章
- 对短路变形POJ3615
Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are ...
- 最短路变形 poj3615&
问题: 牛要跨过一些障碍,希望以最小的体力跨过障碍,并且对于一条路径,只在乎其中最高的障碍. 输入N代表站点数,标记为1—N,输入M代表路径数,从站点S到E之间需要跨过高度为H的障碍. 输入T代表牛要 ...
- POJ-2253.Frogger.(求每条路径中最大值的最小值,最短路变形)
做到了这个题,感觉网上的博客是真的水,只有kuangbin大神一句话就点醒了我,所以我写这篇博客是为了让最短路的入门者尽快脱坑...... 本题思路:本题是最短路的变形,要求出最短路中的最大跳跃距离, ...
- 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-1797Heavy Transportation,最短路变形,用dijkstra稍加修改就可以了;
Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Description Background Hugo ...
- HDOJ find the safest road 1596【最短路变形】
find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HN0I2000最优乘车 (最短路变形)
HN0I2000最优乘车 (最短路变形) 版权声明:本篇随笔版权归作者YJSheep(www.cnblogs.com/yangyaojia)所有,转载请保留原地址! [试题]为了简化城市公共汽车收费系 ...
- 天梯杯 PAT L2-001. 紧急救援 最短路变形
作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上.当其他城市有紧急求 ...
随机推荐
- hdu-1394(线段树&逆序数的性质和求法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目大意: 给出一个序列,一对逆序数就是满足i<j&&a[i]>a[ ...
- Educational Codeforces Round 51 D. Bicolorings(dp)
https://codeforces.com/contest/1051/problem/D 题意 一个2*n的矩阵,你可以用黑白格子去填充他,求联通块数目等于k的方案数,答案%998244353. 思 ...
- opencv知识积累
1.OpenCV 3计算机视觉:Python语言实现 https://github.com/techfort/pycv 2.OpenCV3编程入门 opencv 均值模糊:一般用来处理图像的随机噪声 ...
- C++IO cin
cin cin.get() 每次只读缓冲区一个字符,不能接收空格 cin.getline() 读缓冲区一行,能够接收空格 cin.ignore(2) 忽略缓冲器2个字节 int i = cin.pee ...
- IntelliJ IDEA 2017版 Spring5最基本的bean例子创建
一.简述 SpringBoot是基于spring框架之上的快速开发的框架.Spring4核心就是容器,容器提供了对bean的装配和管理. spring依赖加载: ...
- hadoop Hive 的建表 和导入导出及索引视图
1.hive 的导入导出 1.1 hive的常见数据导入方法 1.1.1 从本地系统中导入数据到hive表 1.创建student表 [ROW FORMAT DELIMITED]关键字,是用来设 ...
- Bagging和Boosting的区别
转:http://www.cnblogs.com/liuwu265/p/4690486.html Bagging和Boosting都是将已有的分类或回归算法通过一定方式组合起来,形成一个性能更加强大的 ...
- linux上安装Elasticsearch
搭建环境centos7及 首先通过工具上传tar包到/usr/local/mypackage/elasticsearch 解压tar包 解压后进入config目录,编辑配置文件 vi elastics ...
- C#-VS支持的语言
其中C语言选C++
- matlab矢量场数值可视化(动态数值模拟)
https://blog.csdn.net/eric_e/article/details/81294092 D3.js实现数据可视化 三维可视化 风场可视化(数据插值):风场是动态变化的,实时刷新的, ...