HDU 2544 Single Source Shortest path problem dijkstra+ heap optimization template

Source: Internet
Author: User

Shortest circuit Time limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 41168 Accepted Submission (s): 17992


Problem description in the annual school game, all the students who enter the finals will get a very beautiful T-shirt. But every time our staff put demonstrating clothes from the store back to the game, it was very tiring! So now they want to find the shortest route from the store to the arena, can you help them?


Input inputs include multiple sets of data. The first row of each group of data is two integers n, m (n<=100,m<=10000), n indicates that there are several intersections on the streets of Chengdu, the intersection labeled 1 is the location of the store, the intersection labeled N is the location of the stadium, M said there are several roads in Chengdu. N=m=0 indicates the end of the input. The next m line, each line consists of 3 integers a,b,c (1<=a,b&lt;=n,1<=c<=1000), indicating that there is a road between junction A and intersection B, and our staff needs C minutes to walk this road.
Enter a guarantee that there are at least 1 shops to the track.

Output one line for each set of inputs, indicating the shortest time the worker has walked from the store to the arena
Sample Input
2 11 2 33 31 2 52 3 53 1 20 0

Sample Output
32

SOURCEUESTC 6th Programming Contest Online The shortest entry question, just start contact, good magic ~ ~


HDU 2544 Shortest Path//Dijkstra heap Optimization template//Most reference Challenge Program Design contest book Code #include<map> #include <vector> #include <cstdio># include<iostream> #include <cstring> #include <string> #include <algorithm> #include <cmath > #include <stack> #include <queue> #include <set> #define INF 0x3f3f3f3f#define mem (a,x) memset (a,x    , sizeof (a)) using namespace Std;typedef long long ll;typedef pair<int,int> pii;inline int in () {int Res=0;char C;    while (C=getchar ()) < ' 0 ' | | c> ' 9 ');    while (c>= ' 0 ' && c<= ' 9 ') res=res*10+c-' 0 ', C=getchar (); return res;}    struct st{int to;    int cost;        St (int a,int B)//constructor {to=a;    Cost=b; }};int dis[111];vector<st> v[111];//by specifying the greater<pii> parameter, the heap takes the value Priority_queue<pii in the first order from small to large.                                    vector<pii>,greater<pii> > Q;    Pair<int,int> first represents the shortest distance, and second represents the vertex number int main () {int n,m;  while (~SCANF ("%d%d", &n,&m) && N) {      MEM (Dis,inf);        Initializes infinity for (int i=0;i<=n;i++) {v[i].clear ();            } for (int i=1;i<=m;i++) {int t=in ();            int T1=in (), T2=in ();            V[t].push_back (St (T1,T2));        V[t1].push_back (St (T,T2));//no-map must push two times} q.push (PII (0,1));       dis[1]=0;                 Initialize while (!q.empty ()) {PII x = Q.top ();            Q.pop ();            int Tmp=x.second;            if (Dis[tmp]<x.first) continue;                for (int i=0;i< (int) v[tmp].size (); i++)//The point at which the current vertex can reach {st T=v[tmp][i]; if (dis[t.to]>dis[tmp]+t.cost)//current vertex minimum distance + is it closer to this point than the minimum distance it can reach {dis[t.to]=dis[tmp]  +t.cost; Update Q.push (PII (dis[t.to],t.to)) in small words;    and Queued}}} cout<<dis[n]<<endl; } return 0;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 2544 Single Source Shortest path problem dijkstra+ heap optimization template

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.