Description
Bessie have moved to a small farm and sometimes enjoys returning to visit one of hers best friends. She does not want-to get-to-her-old home too quickly, because she likes the scenery along the. 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 both of the N (1≤ N ≤5000) intersections, conveniently numbered 1.. n. Bessie starts at intersection 1, and she friend (the destination) is at intersection N.
The Second-shortest path may share roads with any of the shortest paths, and it could backtrack i.e., use the same road or I Ntersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path (s) (i.e., if EST paths exist, the second-shortest path is the one whose length are longer than those but no longer than any other path).
Input
Line 1:two space-separated integers:
Nand
R
Lines 2..
R+1:each line contains three space-separated integers:
A,
B, and
DThat's describe a road that connects intersections
Aand
Band 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 41 2 1002 4 2002 3 2503 4 100
Sample Output
450
Hint
3 (length 100+250+100=450), 4 (length 100+200=300) and 1, 2, routes:1, 2
Source
Usaco 2006 November Gold A short-circuit problem, just log to each point of the short circuit is OK, and then with each update of the shortest and minor short-circuit to continue to update his neighbor points. Code:
#include <cstdio>#include<iostream>#include<cstring>#include<map>#include<queue>#defineINF 0x3f3f3f3f#defineMax 200005using namespaceStd;typedef pair<int,int>p;intN,r;intFir[max],nex[max],u[max],v[max],w[max];intdis[5001],sdis[5001];intMain () {scanf ("%d%d",&n,&R); memset (Fir,-1,sizeof(FIR)); memset (Dis,inf,sizeof(DIS)); memset (Sdis,inf,sizeof(SDIs)); for(inti =0; i < R;i + +) {scanf ("%d%d%d",&u[i],&v[i],&W[i]); U[i+ R] =V[i]; V[i+ R] =U[i]; W[i+ R] =W[i]; Nex[i]=Fir[u[i]]; Fir[u[i]]=i; Nex[i+ R] = Fir[u[i +R]]; Fir[u[i+ r]] = i +R; } dis[1] =0; Priority_queue<p,vector<p>,greater<p> >Q; Q.push (P (0,1)); while(!Q.empty ()) {P S=Q.top (); Q.pop (); if(Sdis[s.second] < S.first)Continue; intK =Fir[s.second]; while(k! =-1) { intD = w[k] +S.first; if(D <Dis[v[k]]) {Q.push (P (d,v[k])); Swap (D,dis[v[k]]); } if(D <Sdis[v[k]]) {Q.push (P (d,v[k])); SDIS[V[K]]=D; } k=Nex[k]; }} printf ("%d", Sdis[n]);}View Code
POJ 3255 Roadblocks