zoukankan      html  css  js  c++  java
  • HDU

    在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的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
    #include <cstdio>
    #include <iostream>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    #define ll long long
    const int inf = 0x3f3f3f3f;
    const int maxn = 1e4+8;
    int n, m, cost[maxn][maxn], time[maxn];
    bool sign[maxn];
    void dij(int s)
    {
        fill(time, time+n+1, inf);
        fill(sign, sign+n+1, 0);
        time[1] = 0;
        for(int i = 1; i <= n; i++)
        {
            int miao, ying = inf;
            for(int j = 1; j <= n; j++)
                if(!sign[j] && time[j] <= ying)
                    ying = time[miao = j];
            sign[miao] = 1;
            for(int j = 1; j <= n; j++)
                if(time[j]>time[miao]+cost[miao][j])
                    time[j] = time[miao]+cost[miao][j];
        }
    }
    int main()
    {
        while(~scanf("%d%d", &n, &m) && (n+m))
        {
            for(int i = 1; i <= n; i++)
                for(int j = 1; j <= n; j++)
                    cost[i][j] = inf;
            int a, b, c;
            while(m--)
            {
                scanf("%d%d%d", &a, &b, &c);
                if(cost[a][b]>c)
                    cost[a][b] = cost[b][a] = c;
            }
            dij(1);
            printf("%d
    ", time[n]);
        }
        return 0;
    }
  • 相关阅读:
    POJ 1659 Frogs' Neighborhood
    zoj 2913 Bus Pass(BFS)
    ZOJ 1008 Gnome Tetravex(DFS)
    POJ 1562 Oil Deposits (DFS)
    zoj 2165 Red and Black (DFs)poj 1979
    hdu 3954 Level up
    sgu 249 Matrix
    hdu 4417 Super Mario
    SPOJ (BNUOJ) LCM Sum
    hdu 2665 Kth number 划分树
  • 原文地址:https://www.cnblogs.com/RootVount/p/10561021.html
Copyright © 2011-2022 走看看