HDU 2544 Shortest Path (Dijkstra algorithm)

Source: Internet
Author: User

Portal:

http://acm.hdu.edu.cn/showproblem.php?pid=2544

Shortest circuit

Time limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 83843 Accepted Submission (s): 36272


Problem description in the annual school game, all the students who enter the finals will get a very 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?

Input inputs include 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.

Output one line for each set of inputs, indicating the shortest time the worker has walked from the store to the arena

Sample Input2 11 2 33 31 2 52 3 53 1 20 0

Sample Output32

SOURCE UESTC 6th Programming Contest Online PS: Before doing a few Dijkstra algorithm of the problem, this time over, happy note: The case of the re-edge code as follows:
#include <bits/stdc++.h>using namespacestd;#defineMAX_V 105#defineINF 99999intEdge[max_v][max_v];intn,m;intUsed[max_v];intDis[max_v];voidinit () {memset (used,0,sizeof(used));  for(intI=1; i<=n; i++)    {         for(intj=1; j<=n; J + +) {Edge[i][j]=INF; } Dis[i]=INF; }}voidDijkstra (ints) {     for(intI=1; i<=n; i++) {Dis[i]=Edge[s][i]; } Dis[s]=0;  for(intI=1; i<=n; i++)    {        intindex,mindis=INF;  for(intj=1; j<=n; J + +)        {            if(used[j]==0&&dis[j]<Mindis) {Mindis=Dis[j]; Index=J; }} Used[index]=1;  for(intj=1; j<=n; J + +)        {            if(dis[index]+edge[index][j]<Dis[j]) {Dis[j]=dis[index]+Edge[index][j]; }        }    }}intMain () { while(~SCANF ("%d%d",&n,&m)) {if(n==0&&m==0)             Break; if(n==1) {printf ("0\n"); Continue;        } init ();  for(intI=0; i<m; i++)        {            intA,b,c; scanf (" %d%d%d",&a,&b,&c); if(EDGE[A][B]&GT;C)//prevention of heavy edges{Edge[a][b]=edge[b][a]=C; }} Dijkstra (1); printf ("%d\n", Dis[n]); }}

HDU 2544 Shortest Path (Dijkstra algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.