POJ3255 roadblocks [Dijkstra, secondary short circuit]

Source: Internet
Author: User

Topic Portal

roadblocks

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: N and R
Lines 2.. R+1:each line contains three space-separated integers: a, B, and D that describe A ro Ad that connects intersections A and B and have 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 Analysis:A sentence test instructions, a short-circuit template. in general, a short circuit can be done by first running the shortest way, and then record the path, in the shortest path every time the deletion of one of the side, and then run the most short-circuit to find a short-circuit. However, there is a more concise and rapid method, using $dijkstra$ to find the shortest and second short-circuit. The idea is very simple, in seeking the shortest possible time to open another array to record a short-circuit, each update is updated to a minimum path can be updated to the secondary short-circuit, or update the path is shorter than the most short-circuit short time than the current record to be updated. However, there are some details to be aware of, the specific can see the code. Code:
//It is made by Holselee on 17th 2018//POJ3255#include <cstring>#include<cstdio>#include<cstdlib>#include<cmath>#include<iostream>#include<iomanip>#include<queue>#include<algorithm>#defineMax (a) (a) > (b)? (a):(B)#defineMin (a) (a) < (b)? (a):(B)#defineSwap (A, a) (a) ^= (b) ^= (a) ^= (b)using namespacestd;Const intn=5005;Const intm=1e5+7; typedef pair<int,int>P;intN,m,head[n],siz,dis[n],dist[n];structnode{intTO,VAL,NXT;} Edge[m<<1];p riority_queue<P,vector<P>,greater<P> >T;inlineintRead () {CharCh=getchar ();intnum=0;BOOLflag=false;  while(ch<'0'|| Ch>'9'){if(ch=='-') flag=true; ch=GetChar ();}  while(ch>='0'&&ch<='9') {num=num*Ten+ch-'0'; ch=GetChar ();} returnflag?-Num:num;} InlinevoidAddintXintYintz) {edge[++siz].to=y; Edge[siz].val=Z; EDGE[SIZ].NXT=Head[x]; HEAD[X]=siz;}voidDijkstra () {memset (DIS,0x7f,sizeof(DIS)); memset (Dist,0x7f,sizeof(Dist)); dis[1]=0; T.push (P (1,0)); intX,y,d,dt;  while(!T.empty ()) {x=t.top (). first,d=T.top (). Second;        T.pop (); if(dist[x]<d)Continue;  for(inti=head[x];i!=-1; i=edge[i].nxt) {y=edge[i].to; DT=d+Edge[i].val; if(dis[y]>DT)                {Swap (DIS[Y],DT);            T.push (P (Y,dis[y])); }            if(dist[y]>dt&&dis[y]<DT) {Dist[y]=DT;            T.push (P (Y,dist[y])); }        }    }}intMain () {n=read (); m=read (); intx, Y, Z memset (Head,-1,sizeof(head));  for(intI=1; i<=m;++i) {x=read (), Y=read (), z=read ();    Add (x, y, z); add (y,x,z);    } Dijkstra (); printf ("%d\n", Dist[n]); return 0;}

POJ3255 roadblocks [Dijkstra, secondary short circuit]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.