zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    【转】java面试题与答案
    【Appium】移动端自动化测试,触摸(TouchAction) 与多点触控(MultiAction)
    【转载】appium+python自动化-首次打开app权限弹窗问题
    【转载】视频直播平台测试
    关于http协议
    🍖MVC 与 MVT
    🍖Django框架之视图层(CBV源码剖析) ⭐
    🍖Django之settings源码分析
    🍖CGI、FastCGI、WSGI、uWSGI、uwsgi关系
    🐍Python内置函数
  • 原文地址:https://www.cnblogs.com/RootVount/p/11536063.html
Copyright © 2011-2022 走看看