HDU 2544 Shortest circuit: the implementation of various shortest path algorithm

Source: Internet
Author: User

Link:

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

Topic:

Problem Description
Every year in the school game, all the students who enter the finals will get a very beautiful T-shirt. But when our staff to the hundreds clothes from the store back to the game, but is very tired! So now they want to find the shortest route from the store to the arena, can you help them?

Input
Input includes multiple sets of data. The first row of each group of data is two integers n, M (n<=100,m<=10000), n means that there are several junctions in Chengdu Street, marking the intersection of 1 is the location of the store, marking the intersection of N is the venue, M said there are several roads in Chengdu. N=m=0 indicates the end of the input. Next to line m, each row consists of 3 integers a,b,c (1<=a,b<=n,1<=c<=1000), which means that there is a road between junction A and B, and our staff need C minutes to walk this way.
Enter a route that guarantees the presence of at least 1 stores to the arena.

Output
For each set of inputs, the output line indicates the minimum time that a worker walks 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

Source
UESTC 6th Programming Contest Online

The basis of the shortest, do not explain, in fact, is specifically used to verify a variety of the most short-circuit template.

1.Dijkstra Normal Edition

#include <cstdio> #include <cstring> const int n=105, inf=9999999;  
    
int d[n], w[n][n],vis[n],n,m;  
    void Dijkstra (int src) {for (int i=1; i<=n; ++i) d[i] = INF;   
    D[SRC] = 0;  
    memset (Vis, 0, sizeof (VIS));  
        for (int i=1; i<=n; ++i) {int u=-1;  
        for (int j=1; j<=n; ++j) if (!vis[j]) {if (U==-1 | | d[j]<d[u)) u=j;  
        } Vis[u] = 1;  
            for (int j=1; j<=n; ++j) if (!vis[j]) {int tmp = D[u] + w[u][j];  
        if (Tmp<d[j]) d[j] = tmp;  
    int main () {int a,b,c;  
            while (~SCANF ("%d%d", &n,&m) &&n+m) {for (int i=1; i<=n; ++i) {w[i][i] = INF;  
        for (int j=i+1; j<=n; ++j) w[i][j] = w[j][i] = INF;  
            for (int i=0; i<m; ++i) {scanf ("%d%d%d", &a,&b,&c);  
        W[A][B] = w[b][a] = C; } Dijkstra (1);  
    printf ("%d\n", D[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.