最短路变形 poj3615&
问题:
牛要跨过一些障碍,希望以最小的体力跨过障碍,并且对于一条路径,只在乎其中最高的障碍。
输入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&的更多相关文章
- 对短路变形POJ3615
Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are ...
- 最短路变形 poj3615& poj2263
问题: 牛要跨过一些障碍,希望以最小的体力跨过障碍,并且对于一条路径,只在乎其中最高的障碍. 输入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. 紧急救援 最短路变形
作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上.当其他城市有紧急求 ...
随机推荐
- Java基础中的一些注意点(续)
1.局部(local)变量是在一个方法内定义的变量, 也被称作自动(automatic).临时(temporary)或栈(stack)变量 - 当一个方法被执行时, 局部变量被创建: ...
- 【toplink】 位居第一的Java对象关系可持续性体系结构
TopLink,是位居第一的Java对象关系可持续性体系结构,原署WebGain公司的产品,后被Oracle收购,并重新包装为Oracle AS TopLink.TOPLink为在关系数据库表中存储 ...
- cocos2dx3.4 保存json文件
头文件: #include "json/document.h" #include "json/stringbuffer.h" #include "js ...
- Objective-c开发中混合使用ARC
首选“Compile Sources”的位置: 选中工程->TARGETS->相应的target然后选中右侧的“Build Phases”,向下就找到“Compile Sources”了. ...
- MXNet在64位Win7下的编译安装
注:本文原创,作者:Noah Zhang (http://www.cnblogs.com/noahzn/) 我笔记本配置比较低,想装个轻量级的MXNet试试,装完之后报错,不是有效的应用程序,找不到 ...
- poj 3373 Changing Digits (DFS + 记忆化剪枝+鸽巢原理思想)
http://poj.org/problem?id=3373 Changing Digits Time Limit: 3000MS Memory Limit: 65536K Total Submi ...
- vs git .ignore
## Ignore Visual Studio temporary files, build results, and## files generated by popular Visual Stud ...
- TCP 协议如何保证可靠传输
一.综述 1.确认和重传:接收方收到报文就会确认,发送方发送一段时间后没有收到确认就重传. 2.数据校验 3.数据合理分片和排序: UDP:IP数据报大于1500字节,大于MTU.这个时候发送方IP层 ...
- JAVA实现word doc docx pdf excel的在线浏览 - 仿百度文库 源码
我们具体实现思路是这样的 首先下载并安装openoffice和swftools openoffice下载地址:http://www.openoffice.org/download/index.html ...
- /MD, /MDD, /ML, /MT,/MTD(使用运行时库)
1. VC编译选项 多线程(/MT)多线程调试(/MTd)多线程 DLL (/MD)多线程调试 DLL (/MDd) 2. C 运行时库 ...