Unblocked works continued (Dijkstra)

Source: Internet
Author: User

Unblocked Works continued

Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 42384 Accepted Submission (s): 15689


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 is also a Dijkstra template problem!
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <queue>6 using namespacestd;7 Const intMAXN =205;8 Const intINF =0x3f3f3f3f;9 structedge{Ten     intTo,cost; OneFriendBOOL operator<(Edge A,edge B) { A         returnA.cost >B.cost; -     } - }; the intD[MAXN]; -Vector<edge>G[MAXN]; - BOOLDONE[MAXN]; - voidDijkstraints) { +memset (D,inf,sizeof(d)); -memset (Done,false,sizeof(done)); +D[s] =0; APriority_queue<edge>Q; atQ.push (Edge) {s,0}); -      while(!Q.empty ()) { -Edge cur =q.top (); Q.pop (); -         intv =cur.to; -         if(Done[v])Continue; -DONE[V] =true; in          for(inti =0; I<g[v].size (); i++){ -Edge E =G[v][i]; to             if(d[e.to]>d[v]+e.cost) { +D[e.to] = d[v]+E.cost; - Q.push (Edge) {e.to,d[e.to]}); the             } *         } $     }Panax Notoginseng } - voidsolve () { the     intn,m; +      while(SCANF ("%d%d", &n,&m)! =EOF) { A          for(inti =0; i<m; i++){ the             inta,b,c; +scanf"%d%d%d",&a,&b,&c); - G[a].push_back (Edge) {b,c}); $ G[b].push_back (Edge) {a,c}); $         } -         ints,t; -scanf"%d%d",&s,&t); the Dijkstra (s); -         if(D[t] = = INF) printf ("-1\n");Wuyi         Elseprintf"%d\n", D[t]); the          for(inti =0; i<maxn; i++) g[i].clear (); -     } Wu } - intMain () About { $ solve (); -     return 0; -}

Unblocked works continued (Dijkstra)

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.