Hdoj 2544 Shortest Path (shortest path +dijkstrea algorithm)

Source: Internet
Author: User

title Link:http://acm.hdu.edu.cn/showproblem.php?pid=2544

thought analysis: The problem is given an Dijkstra graph, which requires the shortest path length from the starting point to the end point, and the shortest distance from the starting point to all other points can be obtained by using the algorithm.

The code is as follows:

#include <queue>#include<climits>#include<cstdio>#include<vector>#include<utility>#include<cstring>#include<iostream>#include<algorithm>#include<functional>using namespaceStd;typedef pair<int,int>PII;Const intMax_n =10000+Ten;Const intMax_m = -+Ten;intU[max_n], V[max_n], w[max_n];BOOLDone[max_n];intD[max_n];vector<PII>G[max_n];voidDijkstra (intN) {Priority_queue<pii, Vector<pii>, greater<pii> >Q;  for(inti =1; I <= N; ++i) d[i]= (i = =1?0: Int_max); memset (Done, NULL,sizeof(done)); Q.push (Make_pair (d[1],1));  while(!Q.empty ()) {PII x=Q.top ();        Q.pop (); intU =X.second; if(Done[u])Continue; Done[u]=true;  for(inti =0; I < g[u].size (); ++i) {intv =G[u][i].first; intW =G[u][i].second; if(D[v] > D[u] +W) {D[v]= D[u] +W;            Q.push (Make_pair (d[v], v)); }        }    }}intMain () {intN, M;  while(SCANF ("%d%d", &n, &m)! = EOF && N &&M) { for(intE =1; e <= M; ++e) {scanf (" %d%d%d", &u[e], &v[e], &W[e]);            G[u[e]].push_back (Make_pair (V[e], w[e]));        G[v[e]].push_back (Make_pair (U[e], w[e]));        } Dijkstra (N); printf ("%d\n", D[n]);  for(inti =0; I <= N; ++i) g[i].clear (); }    return 0;}

Hdoj 2544 Shortest Path (shortest path +dijkstrea algorithm)

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.