HDU 1874 unblocked Project continued (dijkstra+ priority queue)

Source: Internet
Author: User

Unblocked Works continued




Problem description a province since the implementation of many years of smooth engineering plans, finally built a lot of roads. But the road is not good, every time from one town to another town, there are many ways to choose, and some programmes are more than others to walk the distance is much shorter. This makes pedestrians very troubled.

Now that you know the starting and ending points, you can figure out how much distance you need to walk from the start to the end.

Input This topic contains multiple sets of data, please process to the end of the file.
The first row of each group of data contains two positive integers N and M (0<n<200,0<m<1000), representing the number of existing towns and the number of roads that have been built. The towns were numbered 0~n-1.
Next is the M-Line road information. Each line has three integers a,b,x (0<=a,b<n,a!=b,0<x<10000), indicating that there is a two-way road with X length between town A and town B.
The next line has two integer s,t (0<=s,t<n), representing the starting and ending points, respectively.

Output for each set of data, in a row, the shortest distance to walk. If there is no route from S to T, then output-1.

Sample INPUT3 30 1 10 2 31 2 10 23 10 1 11 2

Sample output2-1
1#include <cstdio>2#include <queue>3#include <algorithm>4#include <cstring>5 using namespacestd;6 7 structEdge8 {9     int  from, to,dist;TenEdgeintUintVintW): from(U), to (v), Dist (w) {}; One }; A  - structHeapnode - { the     intu,d; -     BOOL operator< (ConstHeapnode &temp)Const -     { -         returnD>TEMP.D; +     } - }; +  A Const intinf=0x3f3f3f3f; at intM,n; -Vector<edge>edges; -vector<int>g[205];  - intvis[205],d[205]; -  - voidInit () in { -      for(intI=0; i<m;i++) to g[i].clear (); + edges.clear (); -memset (Vis,0,sizeof(Vis)); the } *  $ voidAddedge (intUintVintW)Panax Notoginseng { - Edges.push_back (Edge (U,v,w)); the Edges.push_back (Edge (V,u,w)); +     intm=edges.size (); AG[u].push_back (M-2);  theG[v].push_back (M-1); + } -  $ intDijkstraintSintT) $ { -Priority_queueQ; -Q.push ((heapnode) {S,0}); the      for(intI=0; i<m;i++) d[i]=inf; -d[s]=0;Wuyi      while(!q.empty ()) the     { -         intu=q.top (). u; Wu Q.pop (); -         if(Vis[u]) About         Continue; $vis[u]=true; -          for(intI=0; I<g[u].size (); i++) -         { -edge& e=Edges[g[u][i]]; A             if(d[e.to]>d[u]+e.dist) +             { thed[e.to]=d[u]+e.dist; - Q.push ((heapnode) {e.to,d[e.to]}); $             } the         } the     } the     returnD[t]; the } -  in intMain () the { the     //freopen ("In.txt", "R", stdin); About     inta,b,c,s,t; the      while(SCANF ("%d%d", &m,&n)! =EOF) the     { the init (); +          for(intI=0; i<n;i++) -         { thescanf"%d%d%d",&a,&b,&c);Bayi Addedge (a,b,c); the         } thescanf"%d%d",&s,&T); -         intans=Dijkstra (s,t); -         if(ans==inf) theprintf"-1\n"); the         Else theprintf"%d\n", ans);  the     } -     return 0; the}

HDU 1874 unblocked Project continued (dijkstra+ priority queue)

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.