zoukankan      html  css  js  c++  java
  • HDU 2680 Choose the best route

    Choose the best route

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5783    Accepted Submission(s): 1866

    Problem Description
    One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s home as soon as possible . Now give you a map of the city’s traffic route, and the stations which are near Kiki’s home so that she can take. You may suppose Kiki can change the bus at any station. Please find out the least time Kiki needs to spend. To make it easy, if the city have n bus stations ,the stations will been expressed as an integer 1,2,3…n.
     
    Input
    There are several test cases. Each case begins with three integers n, m and s,(n<1000,m<20000,1=<s<=n) n stands for the number of bus stations in this city and m stands for the number of directed ways between bus stations .(Maybe there are several ways between two bus stations .) s stands for the bus station that near Kiki’s friend’s home. Then follow m lines ,each line contains three integers p , q , t (0<t<=1000). means from station p to station q there is a way and it will costs t minutes . Then a line with an integer w(0<w<n), means the number of stations Kiki can take at the beginning. Then follows w integers stands for these stations.
     
    Output
    The output contains one line for each data set : the least time Kiki needs to spend ,if it’s impossible to find such a route ,just output “-1”.
     
    Sample Input
    5 8 5
    1 2 2
    1 5 3
    1 3 4
    2 4 7
    2 5 6
    2 3 5
    3 5 1
    4 5 1
    2
    2 3
    4 3 4
    1 2 3
    1 3 4
    2 3 2
    1
    1
     
    Sample Output
    1
    -1
     
    Author
    dandelion
     
    Source
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  2112 1874 1217 1142 1385 
     
    思路:dijkstra
     
    代码:
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #include <cstdlib>
    #include <algorithm>
    using namespace std;
    const int MAXN = 100000000;
    int map[1010][1010];
    int dist[1010];
    int n,m,end;
    int w;
    int first;
    int the_last_flag;
    int a,b,tim;
    void Dijkstra(int n,int dist[1010],int map[1010][1010],int s)
    {
        int minn;
        bool p[1010];
        for(int i = 0;i <= n;i ++)
        {
            if(i != s)
            {
                dist[i] = map[s][i];
                p[i] = false;
            }
        }
        p[s] = true;
        dist[s] = 0;
        for(int i = 1;i <= n;i ++)
        {
            the_last_flag = 0;
            minn = MAXN;
            for(int j = 1;j <= n;j ++)
            {
                if(!p[j] && dist[j] < minn)
                {
                    minn = dist[j];
                    the_last_flag = j;
                }
            }
            if(the_last_flag == 0)
                break ;
            if(the_last_flag == end)
                break ;
            p[the_last_flag] = true;
            for(int j = 1;j <= n;j ++)
            {
                if(!p[j] && map[the_last_flag][j] != MAXN &&
                   dist[j] > dist[the_last_flag] + map[the_last_flag][j])
                   {
                       dist[j] = dist[the_last_flag] + map[the_last_flag][j];
                   }
            }
        }
    }
    int main()
    {
        while(~scanf("%d%d%d",&n,&m,&end))
        {
            for(int i = 0;i <= n;i ++)
                for(int j = 0;j <= n;j ++)
                {
                    if(i == j)
                       map[i][j] = 0;
                    else
                       map[i][j] = MAXN;
                }
            while(m --)
            {
                scanf("%d%d%d",&a,&b,&tim);
                if(map[a][b] > tim)
                   map[a][b] = tim;
            }
            scanf("%d",&w);
            while(w --)
            {
                scanf("%d",&first);
                map[0][first] = 0;
            }
            Dijkstra(n,dist,map,0);
            if(dist[end] == MAXN)
                 printf("-1
    ");
            else
                 printf("%d
    ",dist[end]);
        }
        return 0;
    }
  • 相关阅读:
    深圳沙龙《QTP自动化测试的技术心得》资料已上传
    TIB自动化测试快讯 自动化测试空间一周精选(20111225)
    TIB自动化测试快讯 自动化测试空间一周精选(20111127)
    ATI自动化测试杂志2011年9月刊
    广州沙龙 Selenium自动化测试实施经验分享,现已接受报名!
    广州沙龙 《测试开发》即将举行!
    北京沙龙《socket性能测试脚本的开发、监控和调优》
    AutomationQA的Selenium栏目新添加不少Selenium资料
    活动 【在线专家问答】 QA专家 张志会 与您分享QA实战经验
    广州 Selenium自动化测试 沙龙筹备中...
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3407406.html
Copyright © 2011-2022 走看看