http://poj.org/problem?id=3255
| Time Limit: 2000MS |
|
Memory Limit: 65536K |
| Total Submissions: 15673 |
|
Accepted: 5507 |
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 intersectionN.
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:NandR
Lines 2..R+1:each line contains three space-separated integers:A,B, andDThat's describe a road that connects intersectionsAandBand has lengthD(1≤D≤5000)
Output
Line 1:the length of the second shortest path between node 1 and nodeN
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
1#include <algorithm>2#include <cstdio>3#include <queue>4 5 using namespacestd;6 7 Const intINF (0x3f3f3f3f);8 Const intN50000+ the);9 Const intM100000+5);Ten One intHed[n],had[n],sumedge; A structEdge - { - intv,next,w; the }edge1[m],edge2[m]; -InlinevoidInsintUintVintW) - { -edge1[++sumedge].v=v; +edge1[sumedge].next=Hed[u]; -edge1[sumedge].w=W; +hed[u]=Sumedge; Aedge2[sumedge].v=u; atedge2[sumedge].next=Had[v]; -edge2[sumedge].w=W; -had[v]=Sumedge; - } - - intDis[n]; in BOOLInq[n]; - voidSPFA (ints) to { + for(intI=1; i<s;i++) dis[i]=INF; -dis[s]=0; inq[s]=1; thequeue<int>que; Que.push (s); * for(intu,v;!que.empty ();) $ {Panax NotoginsengU=que.front (); Que.pop (); inq[u]=0; - for(intI=had[u];i;i=edge2[i].next) the { +v=edge2[i].v; A if(dis[v]>dis[u]+EDGE2[I].W) the { +dis[v]=dis[u]+EDGE2[I].W; - if(!inq[v]) Que.push (v), inq[v]=1; $ } $ } - } - } the - structNodeWuyi { the intnow,g; -FriendBOOL operator<(Node a,node b) Wu { - returna.g+dis[a.now]>b.g+Dis[b.now]; About } $ }; - intAstar (intSintTintk) - { -Priority_queue<node>que; A intCnt=0; Node u,v; +u.g=0; u.now=s; the que.push (u); - for(;!que.empty ();) $ { theu=que.top (); Que.pop (); the if(u.now==t) cnt++; the if(cnt==k)returnU.G; the for(intI=hed[u.now];i;i=edge1[i].next) - { inv.now=edge1[i].v; thev.g=u.g+EDGE1[I].W; the Que.push (v); About } the } the } the +InlinevoidReadint&x) - { thex=0; RegisterCharCh=GetChar ();Bayi for(;ch>'9'|| ch<'0';) ch=GetChar (); the for(; ch>='0'&&ch<='9'; Ch=getchar ()) x=x*Ten+ch-'0'; the } - - intAC () the { the intn,m; Read (n), read (m); the for(intv,u,w;m--;) the Read (U), read ( v), read (W), INS (u,v,w); -SPFA (n); printf"%d\n", Astar (1N2)); the return 0; the } the 94 intI_want_ac=AC (); the intMain () {;}WA dropped A *
Poj--t 3225 Roadblocks