Shortest circuit
Time limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 42716 Accepted Submission (s): 18715
problem Descriptionin the annual school game, all the students who enter the finals will get a beautiful T-shirt. But every time our staff put demonstrating clothes from the store back to the game, it was very tiring! So now they want to find the shortest route from the store to the arena, can you help them?
InputThe input includes multiple sets of data. The first row of each group of data is two integers n, m (n<=100,m<=10000), n indicates that there are several intersections on the streets of Chengdu, the intersection labeled 1 is the location of the store, the intersection labeled N is the location of the stadium, M said there are several roads in Chengdu. N=m=0 indicates the end of the input. The next m line, each line consists of 3 integers a,b,c (1<=a,b<=n,1<=c<=1000), indicating that there is a road between junction A and intersection B, and our staff needs C minutes to walk this road.
Enter a guarantee that there are at least 1 shops to the track.
Outputfor each set of inputs, the output line indicates the shortest time the worker has walked from the store to the Arena
Sample Input
2 11 2 33 31 2 52 3 53 1 20 0
Sample Output
32
the first shortest path, it feels like Dijkstra and prim. Dijkstra algorithm, the code is as follows:
#include <cstdio> #include <cstring> #define MAX 110#define INF 0x3f3f3fint n,map[max][max];//map record the shortest path between two points void Dijkstra () {int dis[max];//the shortest path of the record starting point to the current point int visit[max];//mark has accessed the int i,j,next,min; memset (visit,0,sizeof (visit)); for (i=1;i<=n;i++) dis[i]=map[1][i]; Visit[1]=1; for (i=2;i<=n;++i) {min=inf; for (J=1;J<=N;++J) {if (Visit[j]==0&&min>dis[j]) {min=dis[j]; Next=j; }} visit[next]=1; for (j=1;j<=n;j++) {if (Visit[j]==0&&dis[j]>dis[next]+map[next][j]) Dis[j]=d IS[NEXT]+MAP[NEXT][J]; }} printf ("%d\n", Dis[n]);} int main () {int m,a,b,c; while (scanf ("%d%d", &n,&m) &&n| | m) {memset (map,inf,sizeof (map)); while (m--) {scanf ("%d%d%d", &a,&b,&c); if (map[a][b]>c) map[a][b]=map[b][A]=c; } Dijkstra (); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdoj 2544 Shortest route (shortest path Dijkstra algorithm)