Secondary short Circuit

Source: Internet
Author: User

Roadblocks
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7921 Accepted: 2896

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

At first I thought just for the end of the N to find a second short-circuit, only apply for a DIS2 variable, and then update Dis[n] When the DIS2 can be updated, but the "program design" This book is good, not only to find the end of the second short circuit, but also to find all the nodes of the short-circuit, so there is a dis2[ MAXN].

#define_crt_secure_no_deprecate#include<iostream>#include<cstdio>#include<vector>#include<queue>#include<functional>using namespacestd;#defineMAXR 100001#defineMAXN 5002#defineINF 100000000intN, R;//intersections,roadsstructedge{intto, cost; Edge (Const intAConst intb): to (a), cost (b) {}};vector<edge> G[MAXN];//adjacency Matrixtypedef pair<int,int>P;priority_queue<p, Vector<p&gt, greater<p>> que;//Priority Queue DijkstraintDIS[MAXN], DIS2[MAXN];voidsolve () {//initializationFill (dis, dis +MAXN, INF); Fill (Dis2, Dis2+MAXN, INF); //CIN >> N >>R; intA, B, C;  while(r--) {scanf (" %d%d%d", &a, &b, &c);  G[a].push_back (Edge (b, c));    At first I just applied for the edge temp variable, which will be wrong when called, so I added the constructor G[b].push_back (Edge (A, c)) directly; }    //shortest way to start from N=1dis[1] =0; Que.push (P (0,1));  while(!Que.empty ()) {P PIR=que.top (); Que.pop (); intv = pir.second, d =Pir.first; if(Dis2[v] < D)Continue;  for(inti =0; I < g[v].size (); i++) {Edge&e =G[v][i]; intD2 = E.cost +D; if(dis[e.to]>D2)                {Swap (dis[e.to], D2);            Que.push (P (dis[e.to], e.to)); }            if(Dis2[e.to]>d2&&dis[e.to] <D2) {Dis2[e.to]=D2;            Que.push (P (dis2[e.to], e.to)); }}} printf ("%d\n", Dis2[n]);}intMain () {solve ();//GetChar ();    return 0;}

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.