Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1874
Unblocked Works continued
Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 36359 Accepted Submission (s): 13355
Problem description a province since the implementation of many years of smooth engineering plans, finally built a lot of roads. But the road is not good, every time from one town to another town, there are many ways to choose, and some programmes are more than others to walk the distance is much shorter. This makes pedestrians very troubled.
Now that you know the starting and ending points, you can figure out how much distance you need to walk from the start to the end.
Input This topic contains multiple sets of data, please process to the end of the file.
The first row of each group of data contains two positive integers N and M (0<n<200,0<m<1000), representing the number of existing towns and the number of roads that have been built. The towns were numbered 0~n-1.
Next is the M-Line road information. Each line has three integers a,b,x (0<=a,b<n,a!=b,0<x<10000), indicating that there is a two-way road with X length between town A and town B.
The next line has two integer s,t (0<=s,t<n), representing the starting and ending points, respectively.
Output for each set of data, in a row, the shortest distance to walk. If there is no route from S to T, then output-1.
Sample Input
3 30 1 10 2 31 2 10 23 10 1 11 2
Sample Output
2-1
Dijkstra algorithm, and the implementation of the prim algorithm is very similar to the first practice of water problem
#include <iostream> #include <vector> #include <cstring> #include <cstdio> #define Min (x, y) < Y? x:y) const int INF = 0xfffffff;const int maxn = 210;using namespace std;struct node{int v,len; Node (int v= 0,int len = 0): V (v), Len (len) {}};bool intree[maxn];int mindist[maxn];int n,m;vector<node>g[maxn]; Similarly, the vector is used to implement void Init () {for (int i=0;i<maxn;i++) {mindist[i]=inf) using the adjacency list to store the graph. G[i].clear ();//vector initialization must not forget!! Intree[i]=false; }}int Dijktra (int S, int T) {int tempmin,addnode,vex;//First part, the "distance manifestation" intree[s]=true of the point around the starting point; for (unsigned int i=0;i<g[s].size (); i++) {vex = G[S][I].V; Mindist[vex]=min (Mindist[vex],g[s][i].len); Since there may be heavy edges, it is necessary to find the smallest side} mindist[s]=0 when initializing; for (int nodenum=0;nodenum<n-1;nodenum++) {tempmin=inf; for (int i=0;i<n;i++) {if (!intree[i]&&mindist[i]<tempmin) {//here and prim algorithm as Tempmin = Mindist[i]; AddNode= i; }} Intree[addnode] = true; for (unsigned int i=0;i<g[addnode].size (); i++) {int vex = G[ADDNODE][I].V; if (!intree[vex]&&mindist[addnode]+g[addnode][i].len<mindist[vex]) {//dijkstra's minDist is the distance from the starting point. Mindist[vex] = Mindist[addnode]+g[addnode][i].len; Update}}} return mindist[t]; Returns the distance from the ending point to the beginning}int main () {while (cin>>n>>m) {init (); int A,b,len; for (int i=0;i<m;i++) {scanf ("%d%d%d", &a,&b,&len); G[a].push_back (Node (b,len)); G[b].push_back (Node (a,len)); } int s,t; scanf ("%d%d", &s,&t); int Ans=dijktra (S,T); if (ans==inf) printf (" -1\n"); else printf ("%d\n", ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
hdu1874 unblocked Project continuation (Dijkstra algorithm, single source shortest circuit)