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的次短路径放松(该两步并非非此即彼) ...
随机推荐
- minGW64编译Qt
1.安装minGW64,设置bin目录到环境变量Path 2.cmd 到qt的Src目录 3.configure -debug-and-release -opensource -prefix &quo ...
- HDU-3746-Cyclic Nacklace(KMP,循环节)
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- [LOJ] 分块九题 2
https://loj.ac/problem/6278 区间修改,查询区间第k大. 块内有序(另存),块内二分. 还是用vector吧,数组拷贝排序,下标搞不来.. //Stay foolish,st ...
- <Spring Cloud>入门二 Eureka Client
1.搭建一个通用工程 1.1 pom 文件 <?xml version="1.0" encoding="UTF-8"?> <project x ...
- mysql系列安装
Mysql的5种安装方法:1.yum/rpm安装(适合要求不高,并发不大,公司内部,企业内部一些应用场景)2.编译安装(在线高并发业务)3.二进制安装(编译安装后,使用自带工具打包,然后拷贝到相同平台 ...
- 【Mysql数据库】知识点总结
本文转载自:http://www.cnblogs.com/tonghun/p/7191131.html 一 数据库常用操作 mysql -u+username -p+password:登陆数据库管理系 ...
- js 函数节流和防抖
js 函数节流和防抖 throttle 节流 事件触发到结束后只执行一次. 应用场景 触发mousemove事件的时候, 如鼠标移动. 触发keyup事件的情况, 如搜索. 触发scroll事件的时候 ...
- Canvas标签
1.Canvas标签: HTML5<canvas>元素用于图形的绘制,通过脚本(通常是javascript)来完成<canvas>标签只是图形容器,必须使用脚本来绘制图形.你可 ...
- CSS3---关于背景
1.background-origin:设置元素背景图片的原始起始位置. background-origin : border-box | padding-box | content-box; ...
- 条款24:若所有参数皆需要类型转换,请为此采用non-member函数(Declare non-member functions when type conversions should apply to all parameters)
NOTE: 1.如果你需要为某个函数的所有参数(包括this指针所指的那个隐喻参数)进行类型转换,那么这个函数必须是个non-member.