hdu-2544 Shortest Path Dijkstra algorithm introductory exercise

Source: Internet
Author: User
Tags first row
Shortest Way Time limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 32111 Accepted Submission (s): 13941


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 Input
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
Sample Output
3 2
#include <stdio.h> #include <iostream> #include <algorithm> using namespace std;
#define N #define INF 0x3f3f3f3f int map[n][n];
int dis[n];
int vis[n];
int n,m,a,b,t;
        void Dijkstra () {for (int i=1;i<=n;i++) {dis[i]=map[1][i];
    vis[i]=0;
    } dis[1]=0;
    Vis[1]=1;
    int min,pos;
        for (int j=1;j<n;j++) {min=inf; for (int i=1;i<=n;i++) {if (!vis[i]&&dis[i]<min) {min=dis[i]
                ;
            Pos=i;
        }} vis[pos]=1; for (int i=1;i<=n;i++) {if (!vis[i]&&dis[i]>dis[pos]+map[pos][i]) dis[i]=
        Dis[pos]+map[pos][i]; 
            }}} int main () {while (scanf ("%d%d", &n,&m) &&n&&m) {for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++) Map[i][j]=inf;
        for (int i=1;i<=n;i++) Dis[i]=inf; For(int i=1;i<=m;i++)
            {scanf ("%d%d%d", &a,&b,&t);
        map[a][b]=map[b][a]=t;
        } Dijkstra ();
    printf ("%d\n", Dis[n]);
} return 0;
 }


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.