POJ 3255【次短路】
2016-08-10 23:59
232 查看
I - Problem I
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld
& %llu
Submit Status Practice POJ
3255
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
Sample Output
Hint
Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld
& %llu
Submit Status Practice POJ
3255
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)
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<climits> #include<string> #include<queue> #include<stack> #include<algorithm> using namespace std; #define rep(i,j,k)for(i=j;i<k;i++) #define per(i,j,k)for(i=j;i>k;i--) #define MS(x,y)memset(x,y,sizeof(x)) typedef long long LL; const int INF =0x7FFFFFFF; const int low(int x){return x&-x;} const int MN=5100,MR=1e6+10; int first[MN],next[MR*2],v[MR*2],w[MR*2]; int d[2][MN]; int n, r, e,ans,i; bool vis[MN]; typedef pair<int,int>pi; void addedge(int a,int b,int c) { next[e]=first[a],v[e]=b,w[e]=c; first[a]=e++; } void Read() { int a,b,c; MS(first,-1); e=0; while(r--){ scanf("%d%d%d",&a,&b,&c); addedge(a,b,c); addedge(b,a,c); } } void Dijkstra(int cur,int t) { priority_queue<pi,vector<pi>,greater<pi> > q; rep(i,0,n+1) d[t][i]=INF; d[t][cur]=0; q.push(make_pair(d[t][cur],cur)); MS(vis,0); while(!q.empty()) { pi u=q.top(); q.pop(); int x=u.second; if(vis[x])continue; vis[x]=true; for(int k=first[x];k!=-1;k=next[k]) { if(d[t][v[k]]>d[t][x]+w[k]) { d[t][v[k]]=d[t][x]+w[k]; q.push(make_pair(d[t][v[k]],v[k])); } } } } void f() { Dijkstra(1,0); Dijkstra(n,1); int x=d[0] ; ans=INF; for(int i=0;i<e;i+=2) { int e1=i,e2=i+1; int a=v[e2],b=v[e1]; int res=d[0][a]+d[1][b]+w[e1]; if (res<ans&&res>x) ans=res; res=d[1][a]+d[0][b]+w[e2]; if (res<ans&&res>x) ans=res; } } int main() { while(~scanf("%d%d", &n, &r)) { Read(); f(); printf("%d\n", ans); } return 0; }
相关文章推荐
- POJ 3255 Roadblocks (dijkstra最短路)
- POJ 3255 Roadblocks(A*求次短路)
- POJ 3255(次短路)
- POJ 3255(次短路+SPFA)
- POJ 3255 RoadBlocks(次短路+dijkstra变形)
- poj 3255 Roadblocks(次短路板子题)
- POJ 3255 (次短路)
- POJ 3255 Roadblocks (Dijkstra求次短路)
- POJ 3255 Roadblocks (次短路模板)
- poj 3255(次短路)
- poj 3255 Roadblocks【次短路】
- POJ 3255 Roadblocks 求次短路
- POJ 3255 Roadblocks——次短路
- poj 3255(次短路)
- poj3255-Roadblocks-次短路
- POJ 3255 —— 次短路dijkstra
- poj 3255 spfa(求次短路)
- hdu 6181 Two Paths(次短路径长度)POJ 3255 Roadblocks ( 次短路长度)
- POJ 3255 Roadblocks(次短路模板题)
- poj 3255 最短路和次短路