Roadblocks
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 13594   Accepted: 4783

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

[Submit]   [Go Back]   [Status]   [Discuss]

把求最短路的算法稍微改一下,每次记录节点的最短路和次短路。

在求最短路的时候会一次次地更新到那个结点的距离,在更新到真正的最短的距离时候,他的上一个就是次短距离,每次把那个值记录下来就行了。

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <map>
#define for(i,x,n) for(int i=x;i<n;i++)
#define ll long long int
#define INF 0x3f3f3f3f
#define MOD 1000000007 using namespace std; int MAX_V,MAX_E;
struct edge{int to,cost;};
typedef pair<int,int> P;//最短距离,编号
vector<edge> G[];
int d[];//最短距离
int d2[];//次短距离 void dijkstra(int s){
priority_queue<P,vector<P>,greater<P> > que;
fill(d,d+MAX_V,INF);
fill(d2,d2+MAX_V,INF);
d[s]=;
que.push(P(d[s],s));
while(!que.empty()){
P p=que.top(); que.pop();
int v=p.second;
for(i,,G[v].size()){
int to=G[v][i].to;
int dd=p.first+G[v][i].cost;
if(dd<d[to]){
d[to]^=dd;
dd^=d[to];
d[to]^=dd;
que.push(P(d[to],to));
}
if(dd<d2[to] && dd>d[to]){
d2[to]=dd;
que.push(P(d2[to],to));
}
}
}
} int main()
{
int x,y,z;
scanf("%d %d",&MAX_V,&MAX_E);
for(i,,MAX_E){
scanf("%d %d %d",&x,&y,&z);
x-=;
y-=;
edge ee;
ee.to=y; ee.cost=z;
G[x].push_back(ee);
ee.to=x; ee.cost=z;
G[y].push_back(ee);
}
dijkstra();
printf("%d",d2[MAX_V-]);
return ;
}

poj3255 Roadblocks的更多相关文章

  1. POJ3255 Roadblocks [Dijkstra,次短路]

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

  2. poj3255 Roadblocks 次短路

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

  3. POJ3255 Roadblocks 【次短路】

    Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7760   Accepted: 2848 Descri ...

  4. POJ3255(Roadblocks)--次短路径

    点这里看题目 3228K 485MS G++ 2453B 根据题意和测试用例知道这是一个求次短路径的题目.次短路径,就是比最短路径长那么一丢丢的路径,而题中又是要求从一点到指定点的次短路径,果断Dij ...

  5. POJ3255 Roadblocks 严格次短路

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

  6. poj图论解题报告索引

    最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ...

  7. cogs 315. [POJ3255] 地砖RoadBlocks

    315. [POJ3255] 地砖RoadBlocks ★★★   输入文件:block.in   输出文件:block.out   简单对比时间限制:1 s   内存限制:128 MB Descri ...

  8. 【POJ3255/洛谷2865】[Usaco2006 Nov]路障Roadblocks(次短路)

    题目: POJ3255 洛谷2865 分析: 这道题第一眼看上去有点懵-- 不过既然要求次短路,那估计跟最短路有点关系,所以就拿着优先队列优化的Dijkstra乱搞,搞着搞着就通了. 开两个数组:\( ...

  9. POJ——T 3255 Roadblocks|| COGS——T 315. [POJ3255] 地砖RoadBlocks || 洛谷—— P2865 [USACO06NOV]路障Roadblocks

    http://poj.org/problem?id=3255 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15680   ...

随机推荐

  1. JS数字指定长度不足前补零的实现

    问题描述:         要求输出的数字长度是固定的,如长度为2,数字为1,则输出01,即不够位数就在之前补足0. 解决方法: 方法1 function fn1(num, length) { ret ...

  2. I2C总线协议图解

    原帖地址:https://www.cnblogs.com/aaronLinux/p/6218660.html

  3. Spring Boot中使用Swagger2自动构建API文档

    由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...

  4. MySQL监控全部执行过的sql语句

    MySQL监控全部执行过的sql语句 查看是否开启日志记录show variables like “general_log%” ; +——————+———-+|Variable_name|Value| ...

  5. MUI框架开发HTML5手机APP(二)--页面跳转传值&底部选项卡切换

      概 述 JRedu 在上一篇博客中,我们学习了如何使用Hbuilder创建一个APP,同时如何使用MUI搭建属于自己的第一款APP,没有学习的同学可以戳链接学习: http://www.cnblo ...

  6. [Linux] - SVN忽略文件夹更新的命令与方法

    在服务器上有时需要忽略某个文件夹及内容的更新,可以使用命令: svn update --set-depth=exclude FOLDER_NAME 比如需要忽略static目录: svn update ...

  7. CentOS7本地安装MySQL5.7

    操作系统:3.10.0-514.el7.x86_64 安装包:mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz 1:检查是否安装了 libaio(centos7默认 ...

  8. ios the request was denied by service delegate for reason unspecified

    报错的情况如下: xcode8(The request was denied by service delegate (SBMainWorkspace) for reason: Unspecified ...

  9. SignalR代理对象异常:Uncaught TypeError: Cannot read property 'client' of undefined 推出的结论 SignalR 简单示例 通过三个DEMO学会SignalR的三种实现方式 SignalR推送框架两个项目永久连接通讯使用 SignalR 集线器简单实例2 用SignalR创建实时永久长连接异步网络应用程序

    SignalR代理对象异常:Uncaught TypeError: Cannot read property 'client' of undefined 推出的结论   异常汇总:http://www ...

  10. 在新安装的Centos中安装python3.7 解决pip和yum问题

    首先要先安装依赖包: yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-deve ...