POJ3255 Roadblocks 【次短路】
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 7760 | Accepted: 2848 |
Description
Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the
shortest path. She knows there must be some second-shortest path.
The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1..N. Bessie starts at intersection 1, and her friend (the destination)
is at intersection N.
The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if
two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).
Input
Lines 2..R+1: Each line contains three space-separated integers: A, B, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)
Output
Sample Input
4 4
1 2 100
2 4 200
2 3 250
3 4 100
Sample Output
450
Hint
Source
堆优化版本号的Dijkstra
#include <stdio.h>
#include <string.h>
#include <queue>
#include <iostream>
#include <vector>
#include <algorithm> using namespace std; #define maxn 5010
#define maxm 200010
#define inf 0x3f3f3f3f int N, R, head[maxn], id;
struct Node {
int v, w, next;
} E[maxm];
int dis[maxn], dis2[maxn];
typedef pair<int, int> P; void addEdge(int u, int v, int w) {
E[id].v = v; E[id].w = w;
E[id].next = head[u];
head[u] = id++;
} void getMap() {
int a, b, c; id = 0;
memset(head, -1, sizeof(int) * (N + 1));
while(R--) {
scanf("%d%d%d", &a, &b, &c);
addEdge(a, b, c);
addEdge(b, a, c);
}
} void Dijkstra() {
int u = 1, v, d, d2, i;
P now;
for(i = 1; i <= N; ++i) {
dis[i] = dis2[i] = inf;
}
priority_queue<P, vector<P>, greater<P> > pq;
dis[u] = 0;
pq.push(P(0, u));
while(!pq.empty()) {
now = pq.top(); pq.pop();
d = now.first;
u = now.second;
if(d > dis2[u]) continue; for(i = head[u]; i != -1; i = E[i].next) {
v = E[i].v;
d2 = d + E[i].w;
if(d2 < dis[v]) {
swap(dis[v], d2);
pq.push(P(dis[v], v));
}
if(dis2[v] > d2 && dis[v] < d2) {
dis2[v] = d2;
pq.push(P(dis2[v], v));
}
}
}
printf("%d\n", dis2[N]);
} int main() {
while(scanf("%d%d", &N, &R) == 2) {
getMap();
Dijkstra();
}
return 0;
}
POJ3255 Roadblocks 【次短路】的更多相关文章
- poj3255 Roadblocks 次短路
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10098 Accepted: 3620 Descr ...
- Bzoj 1726: [Usaco2006 Nov]Roadblocks第二短路 dijkstra,堆,A*,次短路
1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 969 Solved: 468[S ...
- BZOJ1726: [Usaco2006 Nov]Roadblocks第二短路
1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 768 Solved: 369[S ...
- BZOJ 1726: [Usaco2006 Nov]Roadblocks第二短路( 最短路 )
从起点和终点各跑一次最短路 , 然后枚举每一条边 , 更新answer ---------------------------------------------------------------- ...
- BZOJ 1726: [Usaco2006 Nov]Roadblocks第二短路
1726: [Usaco2006 Nov]Roadblocks第二短路 Description 贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她 ...
- 1726: [Usaco2006 Nov]Roadblocks第二短路
1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 835 Solved: 398[S ...
- 最短路【bzoj1726】: [Usaco2006 Nov]Roadblocks第二短路
1726: [Usaco2006 Nov]Roadblocks第二短路 Description 贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她 ...
- POJ3255 Roadblocks [Dijkstra,次短路]
题目传送门 Roadblocks Description Bessie has moved to a small farm and sometimes enjoys returning to visi ...
- POJ3255 Roadblocks 严格次短路
题目大意:求图的严格次短路. 方法1: SPFA,同时求单源最短路径和单源次短路径.站在节点u上放松与其向量的v的次短路径时时,先尝试由u的最短路径放松,再尝试由u的次短路径放松(该两步并非非此即彼) ...
随机推荐
- TWaver可视化编辑器的前世今生(一)电信网管编辑器
走到今天,TWaver,一个致力于在技术领域(Technology)的弄潮儿(Waver),已经是第十二个年头.当年网吧的小网管都是IDC机房的运维人员了,TWaver也见证了这个时代的成长变迁. 萌 ...
- 解决普遍pc端公共底部永远在下面框架
<div style="width: 90%;height: 3000px;margin: 0 auto; background: red;"></div> ...
- xshell连接Linux(centos6.8)失败的解决方法
注意:本人使用的是Centos6.8版本. Centos7以上版本linux命令会又不一样的地方. 启动xshell终端进行连接服务器:使用命令:ssh + ip地址或是手动使用可视化操作窗口操作, ...
- LeetCode(77) Combinations
题目 Given two integers n and k, return all possible combinations of k numbers out of 1 - n. For examp ...
- POJ 1144 Network (求割点)
题意: 给定一幅无向图, 求出图的割点. 割点模板:http://www.cnblogs.com/Jadon97/p/8328750.html 分析: 输入有点麻烦, 用stringsteam 会比较 ...
- Java 关于循环的练习--和为n的正数序列
要求:输入一个正数n,输出所有和为n的连续正数序列. 分析可以从1开始连续加,若到i的和等于n则输出1到i之间的连续正数,若到i的和大于n,则改为从2开始连续加,再判断到i的和是否等于n,等于则输出2 ...
- 大数据学习——flume日志分类采集汇总
1. 案例场景 A.B两台日志服务机器实时生产日志主要类型为access.log.nginx.log.web.log 现在要求: 把A.B 机器中的access.log.nginx.log.web.l ...
- PC硬件以及引导加载器
PC 硬件 本文介绍供 x86 运行的个人计算机(PC)硬件平台. PC 是指遵守一定工业标准的计算机,它的目标是使得不同厂家生产的机器都能够运行一定范围内的软件.这些标准随时时间迁移不断变化,因此9 ...
- zoj 2201 No Brainer
No Brainer Time Limit: 2 Seconds Memory Limit: 65536 KB Zombies love to eat brains. Yum. Input ...
- hexo干货系列:(三)hexo的Jacman主题优化
前言 上一篇介绍了Jacman主题的安装和配置,今天根据上次的基础做了些优化,让博客看起来很舒服. 正文 首页文章展示摘要 该主题首页文章列表默认是全部展开,感觉不好,我关闭掉了,只展示少量摘要. 修 ...