Poj Roadblocks (secondary short circuit)

Source: Internet
Author: User

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 the shortest path length is dis[], the second short path length is ddis[], then dis[v] = min (Dis[u] + cost[u][v], Ddis[u] + cost[u][v]), so we just need The shortest path and the second short path are calculated.  This is not the same as the shortest path, in the implementation of the Dijkstra algorithm, we need to record the shortest path, but also to record the second short path. Code:
1#include <vector>2#include <map>3#include <Set>4#include <algorithm>5#include <iostream>6#include <cstdio>7#include <cmath>8#include <cstdlib>9#include <string>Ten#include <cstring> One#include <queue> A using namespacestd; - #defineINF 0x3f3f3f3f - #defineMAX 5055 the  - intN,r; - structnode{ -     intto ; +     intCost ; - }; +Vector<node>G[max]; A intDis[max],ddis[max]; at  - voidSolve () - { -typedef pair<int,int>P; -priority_queue<p,vector<p>,greater<p> >que; -Fill (dis,dis+n,inf); inFill (ddis,ddis+n,inf); -dis[0]=0; toQue.push (P (0,0)); +  -      while(!Que.empty ()) { theP p=Que.top (); Que.pop (); *         intv=p.second,d1=P.first; $         if(DDIS[V]&LT;D1)Continue;Panax Notoginseng          for(intI=0; I<g[v].size (); i++){ -Node s=G[v][i]; the             intd2=d1+S.cost; +             if(dis[s.to]>D2) { A swap (DIS[S.TO],D2); the Que.push (P (dis[s.to],s.to)); +             } -             if(Ddis[s.to]>d2 && dis[s.to]<D2) { $ddis[s.to]=D2; $ Que.push (P (ddis[s.to],s.to)); -             } -         } the     } -printf"%d\n", ddis[n-1]);Wuyi } the  - intMain () Wu { -     int  from, To,cost; About node E; $      while(~SCANF ("%d%d",&n,&R)) { -          while(r--){ -scanf"%d%d%d",& from,&to,&Cost ); -              from--;to--; Ae.to=to;e.cost=Cost ; +g[ from].push_back (e); thee.to= from; - G[to].push_back (e); $         } the solve (); the     } the}

Poj Roadblocks (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.