Roadblocks
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: A, B, 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 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的更多相关文章
-
POJ3255 Roadblocks [Dijkstra,次短路]
题目传送门 Roadblocks Description Bessie has moved to a small farm and sometimes enjoys returning to visi ...
-
poj3255 Roadblocks 次短路
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10098 Accepted: 3620 Descr ...
-
POJ3255 Roadblocks 【次短路】
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7760 Accepted: 2848 Descri ...
-
POJ3255(Roadblocks)--次短路径
点这里看题目 3228K 485MS G++ 2453B 根据题意和测试用例知道这是一个求次短路径的题目.次短路径,就是比最短路径长那么一丢丢的路径,而题中又是要求从一点到指定点的次短路径,果断Dij ...
-
POJ3255 Roadblocks 严格次短路
题目大意:求图的严格次短路. 方法1: SPFA,同时求单源最短路径和单源次短路径.站在节点u上放松与其向量的v的次短路径时时,先尝试由u的最短路径放松,再尝试由u的次短路径放松(该两步并非非此即彼) ...
-
poj图论解题报告索引
最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ...
-
cogs 315. [POJ3255] 地砖RoadBlocks
315. [POJ3255] 地砖RoadBlocks ★★★ 输入文件:block.in 输出文件:block.out 简单对比时间限制:1 s 内存限制:128 MB Descri ...
-
【POJ3255/洛谷2865】[Usaco2006 Nov]路障Roadblocks(次短路)
题目: POJ3255 洛谷2865 分析: 这道题第一眼看上去有点懵-- 不过既然要求次短路,那估计跟最短路有点关系,所以就拿着优先队列优化的Dijkstra乱搞,搞着搞着就通了. 开两个数组:\( ...
-
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 ...
随机推荐
-
NYOJ-取石子(二)
取石子(二) 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 小王喜欢与同事玩一些小游戏,今天他们选择了玩取石子. 游戏规则如下:共有N堆石子,已知每堆中石子的数量,并且 ...
-
commonJS — 函数操作(for Function)
for Function github: https://github.com/laixiangran/commonJS/blob/master/src/forFunction.js 代码 /** * ...
-
LintCode ";Find Peak Element II";
Idea is the same: climbing up the hill along one edge (Greedy)! Visualize it in your mind! class Sol ...
-
(LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重
原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...
-
MySql Connector/Net Mysql like 搜索中文的问题(c#和asp.net连接mysql)
Connector/Net 6.9.8 选择.net/mono即可,不需要安装. 将对应版本的MySql.Data.dll复制到bin目录下即可使用 http://dev.mysql.com/down ...
-
IOS开发-OC学习-kvc,kvo
kvc是用来方便的设置实例的属性值的,比如person类的实例p1有一个name的属性,那么我们可以通过kvc去设置p1的name,语法是: [ 对象 setValue:@"xiaming& ...
-
基于vue,打印机打印暂且处理
基于vue单页面应用.暂且没找到合适的方案,什么vue-print .jquery.print.js.jqprint.js..canvas生成图片啊 大多不能保证页面样式保持原样. 所以,选择了最土 ...
-
Zookeeper运维
一.运维配置 参考:http://zookeeper.apache.org/doc/r3.4.6/zookeeperAdmin.html#sc_configuration 基础配置 ...
-
[转] Javascript 原型链
1. 类 在C或者Java里,int a;定义了一个int类型的变量a.其中int是类型的名字,a是具体的变量. Javascript 模仿自 Java, 有一部分面向对象编程的部分.在面向对象的编程 ...
-
C# WinForm窗体及其控件的自适应
3步骤: 1.在需要自适应的Form中实例化全局变量 AutoSizeFormClass.cs源码在下方 AutoSizeFormClass asc = new AutoSizeFormClass ...