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. 百度brpc 压测工具rpc_press解析

    1. 背景 昨天看到一段brpc中的压测代码rpc_press, 看着不错.整理一下. 发压工具的难点不是发送请求,而是要注意下面的2点: 保证能发出足够的qps,比如上万qps 控制发送合理的qps ...

  2. Nginx配置,413 Request Entity Too Large错误解决

    今天有同事找我,说图片上传之后,不知道去哪里了.分析了一下问题,找到原因之后做了处理,这里简要记录一下. 问题原因: 1.首先后台log并无错误信息: 2.捡查了一下浏览器,发现network中有报错 ...

  3. php读取ini配置文件属性

    ini的内容格式如下,请根据自己的INI,格式修改下段程序. autostart = false font_size = font_color = red =================== fu ...

  4. webstorm+nodejs+express配置

  5. N-gram的简单的介绍

    目录: 1. 联合概率 2. 条件概率 3. N-gram的计算方式 4. 评估N-gram的模型. 前言: N-gram是机器学习中NLP处理中的一个较为重要的语言模型,常用来做句子相似度比较,模糊 ...

  6. linux下udev

    如果你使用Linux比较长时间了,那你就知道,在对待设备文件这块,Linux改变了几次策略.在Linux早期,设备文件仅仅是是一些带有适当的属性集的普通文件,它由mknod命令创建,文件存放在/dev ...

  7. MVC5 IIS7 403错误

    问题背景 MVC5部署到IIS7中显示403错误. 解决方案 <system.webServer> → <modules> 节点下的 ApplicationInsightsWe ...

  8. 解決中文地址Uri.IsWellFormedUriString返回false

    數字和大小寫字母都ok,但是中文地址就會有問題 public bool IslocalURL(string url) { if (string.IsNullOrEmpty(url)) { return ...

  9. 【iCore4 双核心板_ARM】例程三十八:DSP MATH库测试

    实验现象: 核心代码: int main(void) { /* USER CODE BEGIN 1 */ int i,j; int res; ]; ; /* USER CODE END 1 */ /* ...

  10. Faster-RCNN 算法解读(转)

    论文:<Faster R-CNN: Towards Real-Time ObjectDetection with Region Proposal Networks> 摘要:算法主要解决两个 ...