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

Line 1: Two space-separated integers: N and R 

Lines 2..R+1: Each line contains three space-separated integers: AB, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)

Output

Line 1: The length of the second shortest path between node 1 and node N

Sample Input

4 4
1 2 100
2 4 200
2 3 250
3 4 100

Sample Output

450

Hint

Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)

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 【次短路】的更多相关文章

  1. poj3255 Roadblocks 次短路

    Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10098   Accepted: 3620 Descr ...

  2. Bzoj 1726: [Usaco2006 Nov]Roadblocks第二短路 dijkstra,堆,A*,次短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 969  Solved: 468[S ...

  3. BZOJ1726: [Usaco2006 Nov]Roadblocks第二短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 768  Solved: 369[S ...

  4. BZOJ 1726: [Usaco2006 Nov]Roadblocks第二短路( 最短路 )

    从起点和终点各跑一次最短路 , 然后枚举每一条边 , 更新answer ---------------------------------------------------------------- ...

  5. BZOJ 1726: [Usaco2006 Nov]Roadblocks第二短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Description 贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她 ...

  6. 1726: [Usaco2006 Nov]Roadblocks第二短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 835  Solved: 398[S ...

  7. 最短路【bzoj1726】: [Usaco2006 Nov]Roadblocks第二短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Description 贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她 ...

  8. POJ3255 Roadblocks [Dijkstra,次短路]

    题目传送门 Roadblocks Description Bessie has moved to a small farm and sometimes enjoys returning to visi ...

  9. POJ3255 Roadblocks 严格次短路

    题目大意:求图的严格次短路. 方法1: SPFA,同时求单源最短路径和单源次短路径.站在节点u上放松与其向量的v的次短路径时时,先尝试由u的最短路径放松,再尝试由u的次短路径放松(该两步并非非此即彼) ...

随机推荐

  1. 图片充当li标签列表标志

    默认情况下,浏览器使用一个黑圆圈作为列表标志,可以用图片取代它: ul {list-style: none} ul li{ background-image: url("img/logo_0 ...

  2. 初探node.js

    一.定义及优势 定义:Node.js是一个基于 Chrome V8 引擎 的 JavaScript 运行时,它以事件驱动为基础实现了非阻塞模型. 优势:由于Web场景下的大多数任务(静态资源读取.数据 ...

  3. Java sleep方法的作用(sleep())

    sleep() 方法的作用是在指定的毫秒数内让当前“正在执行的线程”休眠(暂停执行).这个“正在执行的线程”是指 this.currentThread() 返回的线程. 例 1 下面通过一个案例来理解 ...

  4. No-2.常用 Linux 命令的基本使用

    常用 Linux 命令的基本使用 01. 学习 Linux 终端命令的原因 Linux 刚面世时并没有图形界面,所有的操作全靠命令完成,如 磁盘操作.文件存取.目录操作.进程管理.文件权限 设定等 在 ...

  5. 线程的start和run方法的区别

    回到这个问题,可以用源码的角度去回答,这样会让面试官对有更好的印象 ------>如果直接调用run方法的话,所执行的线程是main线程.调用start方法的话,会新建一个子线程,去执行run方 ...

  6. 在Foxmail邮件客户端登录263企业邮箱

    一.问题描述 首次用Foxmail登录263企业,输入账号和密码,创建 二.问题分析 客户端配置地址: 协议类型 服务器地址 默认端 加密端(SSL) POP pop.263.net 110 1995 ...

  7. Swift中Singleton的实现

    一.意图 保证一个类公有一个实例,并提供一个访问它的全局访问点. 二.使用场景 1.使用场景 当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时 当这个唯一实例应该是通过子类化可扩展的,并且 ...

  8. 洛谷——P3801 红色的幻想乡

    P3801 红色的幻想乡 推荐阅读 https://blog.csdn.net/qq_41252892/article/details/79035942 非常清楚 线段树单点修改 emmm没什么了 # ...

  9. AT2663 Namori Grundy

    题目描述: luogu 题解: 好多细节,比如说每个点有且仅有一条入边. 所以说这个图一定是一个基环外向树. 考虑只是一个环的情况,我们可以发现,当环长为偶数时我们可以$01$交替染色,但环长为奇数时 ...

  10. Linux基础学习-命令行与图形界面切换

    命令行模式和图形界面模式切换 打开文件 vim /etc/inittab # systemd uses 'targets' instead of runlevels. By default, ther ...