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)