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 ...
随机推荐
- 前端PHP入门-012-回调函数[慎入]
尽力而为,对于WEB前端和美工同学,比较难了!但是你们都学过JS的闭包等操作,那么这里也一定没有问题! 回调函数,可以配合匿名函数和变量函数实现更加优美.复杂的一种函数结构. 回调函数,就是在处理一个 ...
- [USACO07MAR]黄金阵容均衡Gold Balanced L…
https://www.luogu.org/problem/show?pid=1360 题目描述 Farmer John's N cows (1 ≤ N ≤ 100,000) share many s ...
- C11线程管理:条件变量
1.简介 C11提供另外一种用于等待的同步机制,它可以阻塞一个或者多个线程,直到收到另外一个线程发出的通知或者超时,才会唤醒当前阻塞的线程.条件变量要和互斥量配合起来使用. condition_var ...
- 在ASP.NET中备份和还原数据库
昨天看了<C#项目实录>中的进销存管理系统,和其他书里讲的案例一样,无非也就是数据库增删查改,但是这个进销存系统中有一个备份和还原数据库的功能,蛮有兴趣的,看了一下代码,原来如此, ...
- PHP做文件限速下载
<?php include("DBDA.class.php"); $db = new DBDA(); $bs = $_SERVER["QUERY_STRING&qu ...
- Java 注解全面解析
1.基本语法 注解定义看起来很像接口的定义.事实上,与其他任何接口一样,注解也将会编译成class文件. @Target(ElementType.Method) @Retention(Retentio ...
- 【leetcode 简单】第十题 实现strStr()
实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...
- 1-spring xml 和 注解 解析过程
spring mvc 入口 DispatcherServlet,类关系图如下所示 DispatcherServlet 就是一个 Servlet,那Servlet 的初始化方法 init()在哪里,通过 ...
- JSON.parse()——json字符串转JS
JSON 通常用于与服务端交换数据. 在接收服务器数据时一般是字符串. 我们可以使用 JSON.parse() 方法将数据转换为 JavaScript 对象. 语法 JSON.parse(text[, ...
- Perl6多线程3: Promise start / in / await
创建一个Promise 并自动运行: my $p = Promise.start({say 'Hello, Promise!'}); 如果把代码改成如下, 我们会发现什么也没打印: ;say 'Hel ...