博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3255 Roadblocks (次短路 SPFA )
阅读量:5363 次
发布时间:2019-06-15

本文共 3156 字,大约阅读时间需要 10 分钟。

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

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)

分析:

次短路不可能与最短路完全重合,那么就一定会有一段比较绕路。绕路的地方不可能超过两处,那样就有一条路短于这条路而长于最短路,矛盾。 因此可以对所有的边加以枚举。

首先用Dijkstra或SPFA以原点和终点为源分别做一次单源最短路,并把答案存在dist_0和dist_n两个数组中。那么,对于任何一条边(i, j),

下面的二者之一就有可能是次短路的长:

dist_0[i] + len(i, j) + dist_n[j] 和 dist_0[j] + len(i, j) + dist_n[i]

注意,如果其中一个的长度等于最短路的长度(即dist_n[0]),就一定不能选,因为这违反次短路的定义。两个都要枚举,因为可能有其中一个等于最短路的长,如果只取较小的值另外一个就废掉了。

#include
#include
#include
#include
const int INF=0x3f3f3f3f;using namespace std;int n,m,cnt;struct Node{ int to,val; int next;} node[200100];int head[5009],vis[5009];//寻找头结点,在找最短路的时候这个点有没有访问过int dis_1[5009],dis_n[5009];//到1号节点的最短路,到n号节点的最短路void add(int a,int b,int w)//将边的信息采用头插法保存下来{ node[cnt].to=b; node[cnt].val=w; node[cnt].next=head[a]; head[a]=cnt; cnt++;}void spfa(int st,int dis[])//spfa求最短路的模板{ memset(vis,0,sizeof(vis)); queue
q; dis[st]=0; vis[st]=1; q.push(st); while(!q.empty()) { int x=q.front(); q.pop(); vis[x]=0; for(int i=head[x];i!=-1;i=node[i].next) { int y=node[i].to; if(dis[y]>dis[x]+node[i].val) { dis[y]=dis[x]+node[i].val; if(vis[y]==0) { vis[y]=1; q.push(y); } } } }}int main(){ int a,b,w; while(~scanf("%d%d",&n,&m)) { for(int i=0;i<=n;i++)//初始化 { head[i]=-1; dis_1[i]=dis_n[i]=INF; } cnt=0;//代表边数 for(int i=0; i
dis_1[n]&&ans>temp) { ans=temp; } } } printf("%d\n",ans); } return 0;}

转载于:https://www.cnblogs.com/cmmdc/p/7727923.html

你可能感兴趣的文章
在同步方法中调用异步方法时如何避免死锁问题
查看>>
hdu 1233 还是畅通工程 Kruskal 最小生成树 并查集
查看>>
poj 1905 Expanding Rods 二分答案
查看>>
ASP.NET+C#面试题
查看>>
学习进度条09
查看>>
HapMap
查看>>
Java 垃圾回收(GC) 泛读
查看>>
PostSQL | Debug记录
查看>>
EXPORT_SYMBOL
查看>>
学会WCF之试错法——超时
查看>>
I/O复习
查看>>
cobbler-web 界面技术详解
查看>>
LightOJ - 1356 Prime Independence (数论+二分图匹配)
查看>>
国外程序员的生活如何
查看>>
一些算法题
查看>>
软件测试第三次作业——习题2.3.7
查看>>
如何实现VM框架中的数据绑定
查看>>
静态属性和静态方法1
查看>>
HDU 1829 A Bug's Life
查看>>
HDU 3631 Shortest Path
查看>>