HDU 2680 Choose the best route (dijkstra+ Priority queue Optimization)

Source: Internet
Author: User

Choose the best route




Problem Descriptionone Day, Kiki wants to visit one of her friends. As she is liable to carsickness, she wants to arrive at her friend's home as soon as possible. Now give your a map of the city ' s traffic route, and the stations which is near Kiki's home so, she can take. Suppose Kiki can change the bus at any station. Please find the least time Kiki needs to spend. To make it easy, if the city has n bus stations, the stations would been expressed as an integer 1,2,3...N.

Inputthere is several test cases.
Each case begins with three integers n, m and S, (N<1000,M<20000,1=<S<=N) n stands for the number of bus statio NS in this city and M stands for the number of directed ways between bus stations. (maybe there is several ways between, bus stations.) s stands for the bus station that near Kiki ' s friend's home.
Then follow m lines, each line contains three integers p, q, T (0<t<=1000). means from station p to station Q there is a and it'll costs T minutes.
Then a line with an integer w (0<w<n), means the number of stations Kiki can take at the beginning. Then follows W integers stands for these stations.

Outputthe output contains one line for each data set:the least time Kiki needs to spend, if it's impossible to find such A route, just output "-1".

Sample INPUT5 8 51 2 21 5 31 3 42 4 72 5 62 3 53 5 14 5 122 34 3 41 2 31 3 42 3 211

Sample output1-1

1#include <cstdio>2#include <cstring>3#include <queue>4#include <vector>5#include <algorithm>6 using namespacestd;7 8 structEdge9 {Ten     intu,v,w; OneEdgeintUintVintW): U (U), V (v), W (w) {}; A }; -  - structHeapnode the { -     intu,d; -     BOOL operator< (Constheapnode& temp)Const -     { +         returnD>TEMP.D; -     } + }; A  atvector<int>g[1005]; -Vector<edge>edges; - Const intinf=0x3f3f3f3f; - intvis[1005]; - intd[1005]; - intEd; in intM,n; -  to voidInit () + { -      for(intI=1; i<=m;i++) the g[i].clear (); * edges.clear (); $ }Panax Notoginseng  - voidAddedge (intUintVintW) the { + Edges.push_back (Edge (U,v,w)); A     intm=edges.size (); theG[u].push_back (M-1); + } -  $ voidDijkstrainted) $ { -memset (Vis,0,sizeof(Vis)); -Priority_queueQ; theQ.push ((Heapnode) {ed,0}); -      for(intI=1; i<=m;i++)Wuyid[i]=inf; thed[ed]=0; -      while(!q.empty ()) Wu     { -         intu=q.top (). u; About Q.pop (); $         if(Vis[u]) -             Continue; -vis[u]=1; -          for(intI=0; I<g[u].size (); i++) A         { +edge& e=Edges[g[u][i]]; the             if(d[e.v]>d[u]+E.W) -             { $d[e.v]=d[u]+E.W; the Q.push ((heapnode) {E.V,D[E.V]}); the             } the         } the     } - } in  the intMain () the { About     intSt,a,b,c,num; the      while(SCANF ("%d%d%d", &m,&n,&ed)! =EOF) the     { the init (); +          for(intI=0; i<n;i++) -         { thescanf"%d%d%d",&a,&b,&c);Bayi Addedge (b,a,c); the         } the Dijkstra (ed); -scanf"%d",&num); -         intans=inf; the          while(num--) the         { thescanf"%d",&St); theans=min (ans,d[st]); -         } the         if(ans==inf) theprintf"-1\n"); the         Else94printf"%d\n", ans); the     } the     return 0; the}

HDU 2680 Choose the best route (dijkstra+ Priority queue Optimization)

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.