Hdoj 1874 unblocked works continued (single Source shortest way +dijkstra)

Source: Internet
Author: User

Topic Links: http://acm.hdu.edu.cn/showproblem.php?pid=1874

Thinking Analysis: The problem is given a graph, the starting point and the endpoint, the minimum distance from the starting point to the endpoint is required to find out;

The shortest-circuit length from the starting point to all other points is calculated using the Dijkstra algorithm, if the shortest-circuit length is int_max, indicating that no path is connected from the starting point to the point;

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 (intStartintN) {Priority_queue<pii, Vector<pii>, greater<pii> >Q;  for(inti =0; I < n; ++i) d[i]= (i = = start?)0: Int_max); memset (Done, NULL,sizeof(done));    Q.push (Make_pair (D[start], start));  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) {intstart, end;  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])); } scanf ("%d%d", &start, &end);        Dijkstra (start, N); if(D[end] = =Int_max) D[end]= -1; printf ("%d\n", D[end]);  for(inti =0; i < N; ++i) g[i].clear (); }    return 0;}

Hdoj 1874 unblocked works continued (single Source shortest way +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.