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)