POJ - 3255(双向次短路)
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
次短路板子
次短路求解方法:若要求a到b的次短路,先以a为起点进行一次spfa,得到的数组为d,再以b为
起点进行spfa,得到数组为d1,然后枚举所给的每一条直连边,假设端点为u,v,边长为w,
求得每一个d[u]+d1[v]+w,取这些中大于最短路的最小的一个就是次短路.
#include<iostream> #include<cstdio> #include<queue> #include<algorithm> #include<cstring> using namespace std; const int maxn=5000+10; const int maxm=200000+10; const int inf=0x3f3f3f3f; int d[maxn],d1[maxn],head[maxn],inq[maxn],q[maxn]; int tol=0; int n,r; struct Edge { int from,to,w,next; }edge[maxm]; void init() { memset(head,-1,sizeof(head)); tol=0; } void addedge(int u,int v,int w) { edge[tol].from=u,edge[tol].to=v,edge[tol].w=w,edge[tol].next=head[u],head[u]=tol++; edge[tol].from=v,edge[tol].to=u,edge[tol].w=w,edge[tol].next=head[v],head[v]=tol++; } void spfa(int s,int *d) { memset(inq,0,sizeof(inq)); for(int i=1;i<=n;i++) d[i]=inf; d[s]=0,inq[s]=1; queue<int>q; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); inq[u]=0; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to,w=edge[i].w; if(d[v]>d[u]+w) { d[v]=d[u]+w; if(inq[v]) continue; inq[v]=1; q.push(v); } } } } int main() { while(~scanf("%d%d",&n,&r)) { init(); while(r--) { int u,v,w; scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); } spfa(1,d); spfa(n,d1); int ans=d[n],res=inf; for(int i=0;i<tol;i++) { int u=edge[i].from,v=edge[i].to,w=edge[i].w; if(d[u]+d1[v]+w>ans) res=min(res,d[u]+d1[v]+w); } printf("%d\n",res); } }Starry_Sky_Dream 原创文章 50获赞 4访问量 2337 关注 私信
- poj 3255 次短路算法(好)
- POJ-1511 Invitation Cards (双向单源最短路)
- 【POJ】3255 Roadblocks(次短路+spfa)
- poj-3255-Roadblocks (求到源点的次短路,Dijkstra改进)
- poj 3255(次短路)
- poj 3255 Roadblocks(spfa次短路 || Dijsktra)
- POJ 3255 Roadblocks (次短路模板)
- POJ 3255 Roadblocks 次短路
- POJ 3255 Roadblocks (Dijkstra求次短路)
- poj 3255 Roadblocks 次短路 spfa 解题报告
- POJ-----3268双向最短路
- poj 3255 求次大最短路
- poj 3268 Silver Cow Party(dijkstra ++ 双向最短路)
- POJ 3255 Roadblocks——次短路
- POJ 3255 - 求两点间的第二短路
- poj 3255 Roadblocks(次短路)
- POJ3255 - Roadblocks - 次短路(spfa+LLL优化)
- POJ 3255 Roadblocks(次短路)
- POJ 3255 Roadblocks(A*求次短路)
- POJ 3255 Roadblocks (次短路)