Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1874
Dijkstra algorithm implementation can refer to this blog learning: http://www.cnblogs.com/biyeymyhjob/archive/2012/07/31/2615833.html
Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 44302 Accepted Submission (s): 16420
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 INPUT3 30 1 10 2 31 2 10 23 10 1 11 2
Sample output2-1
1#include <stdio.h>2#include <iostream>3#include <cstring>4#include <limits.h>5 using namespacestd;6 #defineMAX 100000007 inta[205][205];//Road Read-in8 BOOLvis[205];//whether to deposit9 intdis[205];//path from source to ITen intn,m; One voidDijkstraintv0) A { - //Initialize - inti,j; the for(i=0; i<n;i++) - { -dis[i]=A[v0][i]; -vis[i]=false; + } -vis[v0]=true; +dis[v0]=0; A intX,minn;//ordinal of Shortest path at //start looking for a way - for(i=1; i<n;i++) - { -minn=MAX; - for(j=0; j<n;j++) - { in if(!vis[j]&&dis[j]<Minn) - { tox=J; +minn=Dis[j]; - } the } *vis[x]=true;//put X into a fixed $ //Next Update DataPanax Notoginseng for(j=0; j<n;j++) - { the if(dis[x]+a[x][j]<dis[j]&&!Vis[j]) + { Adis[j]=dis[x]+A[x][j]; the } + } - $ } $ } - intMain () - { the while(cin>>n>>m) - {Wuyi inti,s,e,d,j; the for(i=0; i<n;i++) - for(j=0; j<n;j++) Wu { - if(j==i) a[i][j]=0; About Elsea[i][j]=MAX; $ } - for(i=0; i<m;i++) - { -Cin>>s>>e>>D; A if(D<a[s][e])//There are many roads between two towns + { thea[s][e]=D; -a[e][s]=D; $ } the } thecin>>s>>e;//Start and End the //data read-in completed the Dijkstra (s); - if(Dis[e]==max) cout<<"-1"<<Endl; in Elsecout<<dis[e]<<Endl; the } the return 0; About}
HDU 1874 unblocked Project continuation (initial Dijkstra algorithm implementation)