• SDNU 1224.Tom'problem B(迪杰斯特拉)


    Description

    Tom is a student in Shan Dong Normal University! his University in the suburbs,this day,Tom wanted to go to the downtown to visit his old friend Jerry,But tom faced a problem ,he want to know how to reach the city center fastest,now the bus stations of city are n[2,100], there are m[1,1000] bus routes in the city

    Tom in the 1st bus station,Jerry in the Nth bus station

    Input

    The fist line is m,n;

    Next m lines is a,b,c (a,b is the name of bus station , c is the time you cost from station a to station b)

    Output

    The shortest time tom reach city center

    Sample Input

    1 2
    1 2 10
    3 4
    1 2 10
    2 3 10
    3 4 10

    Sample Output

    10
    30

    Source

    Unknown
    #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 = 1e3+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", &m, &n) && (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;
    }
  • 相关阅读:
    BZOJ 2154 Crash的数字表格 莫比乌斯反演
    BZOJ 3529 SDOI2014 数表 莫比乌斯反演+树状数组
    bzoj 3527 [Zjoi2014]力
    【bzoj2194】快速傅立叶之二
    bzoj3160 万径人踪灭
    高精度乘法(FFT)
    【网络流24题】太空飞行计划
    奶牛通信
    关于点分治的理解
    0924解题报告
  • 原文地址:https://www.cnblogs.com/RootVount/p/11536063.html
Copyright © 2020-2023  润新知