zoukankan      html  css  js  c++  java
  • hdu 2544 最短路问题

    题目链接:

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

     

    Problem Description

     

    在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗?

     

     


    Input

     

    输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。
    输入保证至少存在1条商店到赛场的路线。

     

     


    Output

     

    对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间

     

     


    Sample Input

     

    2 1
    1 2 3
    3 3
    1 2 5
    2 3 5
    3 1 2
    0 0

     

     


    Sample Output

     

    3
    2

     

     

     

     

     

    //起始为0,终点为n-1
    #include <stdio.h>
    #include <limits.h>
    #include <string.h>
    #define MAXN 102
    #define INF (1<<20) //如果很小的话。是不能AC的。
    int v[MAXN], d[MAXN], w[MAXN][MAXN];
    void Dijkstra(int n){
        memset(v, 0, sizeof(v));
        int i, y;
        for(i=0; i<n; i++) d[i] = (i == 0 ? 0 : INF); 
        for(i=0; i<n; i++){
            int x, m = INF;
            for(y=0; y<n; y++) if(!v[y] && d[y]<=m) m = d[x = y];
            v[x] = 1;
            for(y=0; y<n; y++){
                if(!v[y] && d[y] > m + w[x][y]) d[y] = d[x] + w[x][y];
            }
        }
    }
    int main(){
        int n, m, a, b, c, i, j;
        while(scanf("%d %d", &n, &m) == 2 && (n != 0 || m != 0)){
            for(i=0; i<n; i++){
                for(j=0; j<n; j++)
                    w[i][j] = INF;
            }
            for(i=0; i<m; i++){
                scanf("%d %d %d", &a, &b, &c);
                w[a-1][b-1] = w[b-1][a-1] = c;
            }
            Dijkstra(n);
            printf("%d
    ", d[n-1]);
        }
        return 0;
    }
    

     

      

     

  • 相关阅读:
    gaia 开源多语言的pipeline 平台
    vernemq 集群 docker-compose 搭建简单试用
    nginx http2 push 试用
    几个方便进行micro frontend 开发的工具&&类库
    Compoxure example 应用说明
    Compoxure 微服务组合proxy 中间件
    SCS Characteristics
    How_Require_Extensions_Work
    nodejs-hook 开发
    microcks 微服务mocks 工具&&运行时
  • 原文地址:https://www.cnblogs.com/ccccnzb/p/3399998.html
Copyright © 2011-2022 走看看