Roadblocks (POJ 3255)

Source: Internet
Author: User

  • The original question is as follows: Roadblocks
    Time Limit: 2000MS Memory Limit: 65536K
    Total Submissions: 19314 Accepted: 6777

    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: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
  • Solution: This problem requires a short-circuit, according to the idea of the Dijkstra algorithm, in order to determine the vertices that have not been determined by the minimum distance of the vertex can be obtained the shortest way, on this basis to make a few changes, you can obtain a short-circuit, A short-circuit to a vertex v is no more than two cases: the shortest path to the other vertex u, plus the u→v edge, the ② to U ① and the u→v side. Therefore, only the shortest and minor short-circuiting of all vertices are required. For each vertex, we record the shortest and second short, like the Dijkstra algorithm, constantly updating the two distances. (Note: The dichotomy between men and women in this problem is a useless trap condition, and many problems, if there is a special structure, will often consider how to use this structure, but this problem is not)
  • Code:
    1#include <cstdio>2#include <queue>3#include <vector>4#include <functional>5#include <cctype>6#include <algorithm>7 #defineNum s ' 0 '8 9 using namespacestd;Tentypedef pair<int,int>P; One  A structEdge - { -     intto ; the     intCost ; - }; -  - Const intmax_v= the; + Const intinf=0x3f3f3f3f; - intV,e,n; +Vector<edge>G[max_v]; A intDist[max_v]; at intDist2[max_v]; -  - voidsolve (); -  - voidSwapint&x,int&y) - { in     intt=x; -x=y;y=T; to } +  - voidReadint&x) { the     Chars; *x=0; $     BOOLflag=0;Panax Notoginseng      while(!isdigit (s=GetChar ())) -(s=='-') && (flag=true); the      for(X=num;isdigit (S=getchar ()); x=x*Ten+num); +(flag) && (x=-x); A } the  + voidWriteintx) - { $     if(x<0) $     { -Putchar ('-'); -x=-x; the     } -     if(x>9)WuyiWrite (x/Ten); thePutchar (%Ten+'0'); - } Wu  - intMain () About { $ read (V); Read (E); -      for(intI=0; i<e; i++) -     { - Edge E; A         ints,t,c; + Read (s); Read ( t); Read (c) ; thee.to=t-1; e.cost=C; -g[s-1].push_back (e); $e.to=s-1; e.cost=C; theg[t-1].push_back (e); the     } the solve (); theWrite (dist2[v-1]); -Putchar ('\ n'); in } the  the voidSolve () About { thePriority_queue<p, Vector<p>, greater<p> >que; theFill (Dist, dist+V, INF); theFill (Dist2, dist2+V, INF); +dist[0]=0; -Que.push (P (0,0)); the      while(!que.empty ())Bayi     { theP p=que.top (); Que.pop (); the         intV=p.second, d=P.first; -         if(dist2[v]<d)Continue; -          for(intI=0; I<g[v].size (); i++) the         { theEdge &e=G[v][i]; the             intD2=d+E.cost; the             if(dist[e.to]>D2) -             { the swap (DIST[E.TO],D2); the Que.push (P (dist[e.to],e.to)); the             }94             if(Dist2[e.to]>d2 && dist[e.to]<D2) the             { thedist2[e.to]=D2; the Que.push (P (dist2[e.to],e.to));98             } About         } -     }101}

Roadblocks (POJ 3255)

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.