POJ3255 Roadblocks [Dijkstra,次短路]
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
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
分析:
//It is made by HolseLee on 17th Aug 2018
//POJ3255
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<queue>
#include<algorithm>
#define Max(a,b) (a)>(b)?(a):(b)
#define Min(a,b) (a)<(b)?(a):(b)
#define Swap(a,b) (a)^=(b)^=(a)^=(b)
using namespace std; const int N=;
const int M=1e5+;
typedef pair<int,int> P;
int n,m,head[N],siz,dis[N],dist[N];
struct Node{
int to,val,nxt;
}edge[M<<];
priority_queue<P,vector<P>,greater<P> > T; inline int read()
{
char ch=getchar();int num=;bool flag=false;
while(ch<''||ch>''){if(ch=='-')flag=true;ch=getchar();}
while(ch>=''&&ch<=''){num=num*+ch-'';ch=getchar();}
return flag?-num:num;
} inline void add(int x,int y,int z)
{
edge[++siz].to=y;
edge[siz].val=z;
edge[siz].nxt=head[x];
head[x]=siz;
} void dijkstra()
{
memset(dis,0x7f,sizeof(dis));
memset(dist,0x7f,sizeof(dist));
dis[]=;
T.push(P(,));
int x,y,d,dt;
while(!T.empty()){
x=T.top().first,d=T.top().second;T.pop();
if(dist[x]<d)continue;
for(int i=head[x];i!=-;i=edge[i].nxt){
y=edge[i].to;
dt=d+edge[i].val;
if(dis[y]>dt){
Swap(dis[y],dt);
T.push(P(y,dis[y]));
}
if(dist[y]>dt&&dis[y]<dt){
dist[y]=dt;
T.push(P(y,dist[y]));
}
}
}
} int main()
{
n=read();m=read();
int x,y,z;
memset(head,-,sizeof(head));
for(int i=;i<=m;++i){
x=read(),y=read(),z=read();
add(x,y,z);add(y,x,z);
}
dijkstra();
printf("%d\n",dist[n]);
return ;
}
POJ3255 Roadblocks [Dijkstra,次短路]的更多相关文章
- POJ3255 Roadblocks 【次短路】
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7760 Accepted: 2848 Descri ...
- POJ3255 Roadblocks 严格次短路
题目大意:求图的严格次短路. 方法1: SPFA,同时求单源最短路径和单源次短路径.站在节点u上放松与其向量的v的次短路径时时,先尝试由u的最短路径放松,再尝试由u的次短路径放松(该两步并非非此即彼) ...
- Dijkstra最短路算法
Dijkstra最短路算法 --转自啊哈磊[坐在马桶上看算法]算法7:Dijkstra最短路算法 上节我们介绍了神奇的只有五行的Floyd最短路算法,它可以方便的求得任意两点的最短路径,这称为“多源最 ...
- dijkstra(最短路)和Prim(最小生成树)下的堆优化
dijkstra(最短路)和Prim(最小生成树)下的堆优化 最小堆: down(i)[向下调整]:从第k层的点i开始向下操作,第k层的点与第k+1层的点(如果有)进行值大小的判断,如果父节点的值大于 ...
- 【坐在马桶上看算法】算法7:Dijkstra最短路算法
上周我们介绍了神奇的只有五行的Floyd最短路算法,它可以方便的求得任意两点的最短路径,这称为“多源最短路”.本周来来介绍指定一个点(源点)到其余各个顶点的最短路径,也叫做“单源最短路径 ...
- 【POJ3255/洛谷2865】[Usaco2006 Nov]路障Roadblocks(次短路)
题目: POJ3255 洛谷2865 分析: 这道题第一眼看上去有点懵-- 不过既然要求次短路,那估计跟最短路有点关系,所以就拿着优先队列优化的Dijkstra乱搞,搞着搞着就通了. 开两个数组:\( ...
- 【POJ - 3255】Roadblocks(次短路 Dijkstra算法)
Roadblocks 直接翻译了 Descriptions Bessie搬到了一个新的农场,有时候他会回去看他的老朋友.但是他不想很快的回去,他喜欢欣赏沿途的风景,所以他会选择次短路,因为她知道一定有 ...
- 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: 13594 Accepted: 4783 Descr ...
随机推荐
- DLL基本知识
一.生成方式: 使用DEF文件定义导出接口或使用__declspec(dllexport)描述接口,编译链接后生成dll+lib,其中lib是导入库,里面只有对导出接口的描述,而没有具体实现. 二.链 ...
- CF758 D. Ability To Convert 细节处理字符串
link 题意:给定进制数n及一串数字,问在此进制下这串数能看成最小的数(10进制)是多少(如HEX下 1|13|11 = 475) 思路:此题要仔细思考细节.首先要想使数最小那么必定有个想法是使低位 ...
- GridControl详解(二)表格的列名配置
点击Run Designer控件上的按钮,弹出视图设计窗口: 列配置: 我们配置完列名后,会发现设计视图发生了变化:
- MSSQL 基础知识002
---启用sa账号 1. 先使用一个windows账号登陆. 2.在数据库实例上面右键,属性,安全性,登录名,sa. 右键,属性. 常规,修改sa的密码. 状态,启用sa账号. 主键的作用: 1.唯一 ...
- Mysql中的primary key 与auto_increment
mysql> create table cc(id int auto_increment); ERROR (): Incorrect table definition; there can be ...
- [003] largest_subarray_with_equal_1&0
[Description] Given an array with only '1' and '0', find a largest length sub-array which contains e ...
- python不可以打印.doc文件
[背景] 需求: 打印word文件 模块: python-docx [问题] 传递xxx.doc文件给python脚本,执行后,控制台没有内容输出 经查询后了解到,大致理由: doc是早一代的word ...
- PHP利用rand(1,100)函数产生10个1~100之间的随机数
//echo rand(1,100); $max=0; $min=100; for($i=0;$i<=9;$i++){ $rand[$i]=rand(1,100); if($rand[$i]&g ...
- oracle 一个网站
http://www.oracle.com/technetwork/cn/articles/11g-pivot-101924-zhs.html
- redis关闭保护模式
1. set key出现的报错 在192.168.56.57客户端登录192.168.56.56的redis服务器时,报错如下: [root@localhost src]# ./redis-cli - ...