zoukankan      html  css  js  c++  java
  • hdu 1690 Bus System(Dijkstra最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1690

    Bus System

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


    Problem Description
    Because of the huge population of China, public transportation is very important. Bus is an important transportation method in traditional public transportation system. And it’s still playing an important role even now.
    The bus system of City X is quite strange. Unlike other city’s system, the cost of ticket is calculated based on the distance between the two stations. Here is a list which describes the relationship between the distance and the cost.



    Your neighbor is a person who is a really miser. He asked you to help him to calculate the minimum cost between the two stations he listed. Can you solve this problem for him?
    To simplify this problem, you can assume that all the stations are located on a straight line. We use x-coordinates to describe the stations’ positions.
     
    Input
    The input consists of several test cases. There is a single number above all, the number of cases. There are no more than 20 cases.
    Each case contains eight integers on the first line, which are L1, L2, L3, L4, C1, C2, C3, C4, each number is non-negative and not larger than 1,000,000,000. You can also assume that L1<=L2<=L3<=L4.
    Two integers, n and m, are given next, representing the number of the stations and questions. Each of the next n lines contains one integer, representing the x-coordinate of the ith station. Each of the next m lines contains two integers, representing the start point and the destination.
    In all of the questions, the start point will be different from the destination.
    For each case,2<=N<=100,0<=M<=500, each x-coordinate is between -1,000,000,000 and 1,000,000,000, and no two x-coordinates will have the same value.
     
    Output
    For each question, if the two stations are attainable, print the minimum cost between them. Otherwise, print “Station X and station Y are not attainable.” Use the format in the sample.
     
    Sample Input
    2
    1 2 3 4 1 3 5 7
    4 2
    1
    2
    3
    4
    1 4
    4 1
    1 2 3 4 1 3 5 7
    4 1
    1
    2
    3
    10
    1 4
     
    Sample Output
    Case 1:
    The minimum cost between station 1 and station 4 is 3.
    The minimum cost between station 4 and station 1 is 3.
    Case 2:
    Station 1 and station 4 are not attainable.
     
    记得上次说不和大神一起玩耍了,这次破例和他一起愉快的a了一题,看来和他还是蛮适合一起a题的,(*^__^*) 嘻嘻……
    题目大意:
    这题的这个是先给出了一个表格,这个表格也就是路程和花费的模板,然后根据这个对下面的问题进行解决,然后第三行给的是n,m,紧接着就是n行,表示的是0到1的距离,0到2的距离,0到3的距离。。。。依次下去。接下来的m行表示的就是要求的起点和终点了,哇哈哈~
    解题思路:
    一个dijkstra的变形就好了,不过不引用map还是这位大神说的,还有一个更奇葩的就是这位竟然用哈希,来记录路和花费的列表,一看数据1,000,000,000.顿时无奈,被我改成了4个if的判断,还是直接点好~~
     
     
    最后还有一个要注意的,我贡献了一次wa,这里要用__int64,还有如果wa了改成const __int64 inf=0xffffffffffffff;就可以ac了!!
     
    详见代码。
     
    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    const __int64 inf=0xffffffffffffff;
    
    __int64 dist[105],node[105],vis[105];
    __int64 l[5],c[5],n;
    
    __int64 ab(__int64 a)
    {
        return a>0?a:-a;
    }
    __int64 cost(__int64 dis)
    {
        if (dis>=0&&dis<=l[1]) return c[1];
        if (dis>l[1]&&dis<=l[2]) return c[2];
        if (dis>l[2]&&dis<=l[3]) return c[3];
        if (dis>l[3]&&dis<=l[4]) return c[4];
    }
    
    void Dijkstra(__int64 start,__int64 end)
    {
        for(int i=1; i<=n; i++)
            node[i]=inf,vis[i]=0;
        __int64 tm=start;
        node[tm]=0;
        vis[tm]=1;
        for(int k=1; k<=n; k++)
        {
            __int64 Min=inf;
            for (int i=1; i<=n; i++)
                if(!vis[i]&&Min>node[i])
                {
                    Min=node[i];
                    tm=i;
                    //cout<<"  "<<tm<<" "<<Min<<endl;
                }
            if(tm==end)
            {
                printf("The minimum cost between station %I64d and station %I64d is %I64d.
    ",start,end,node[end]);
                return ;
            }
            vis[tm]=1;
            for(int i=1; i<=n; i++)
                if(ab(dist[i]-dist[tm])<=l[4]&&!vis[i]&&node[i]>node[tm]+cost(ab(dist[i]-dist[tm])))
                {
                    //cout<<"  "<<i<<" "<<node[tm]<<" "<<ab(dist[i]-dist[tm])<<" "<<hash[ab(dist[i]-dist[tm])]<<endl;
                    node[i]=node[tm]+cost(ab(dist[i]-dist[tm]));
                }
        }
        printf ("Station %I64d and station %I64d are not attainable.
    ",start,end);
    }
    
    int main ()
    {
        int t,k=1;
        cin>>t;
        while (t--)
        {
            cin>>l[1]>>l[2]>>l[3]>>l[4]>>c[1]>>c[2]>>c[3]>>c[4];
            int m;
            cin>>n>>m;
            for(int i=1; i<=n; i++)
                cin>>dist[i];
            printf ("Case %d:
    ",k++);
            while (m--)
            {
                int a,b;
                cin>>a>>b;
                Dijkstra(a,b);
            }
        }
    }
  • 相关阅读:
    五大存储模型关系模型、键值存储、文档存储、列式存储、图形数据库
    UML语言中类之间关系
    hadoop的yarn资源队列
    Scala中下划线的7种用法
    《机器学习》(周志华)西瓜书读书笔记(完结)
    Pytorch DistributedDataParallel简明使用指南
    本地查看SSH远程连接服务器上的TensorBoard
    python max()用法
    实现go并发的三种方式
    docker分阶段构造nginx镜像
  • 原文地址:https://www.cnblogs.com/qq-star/p/3929024.html
Copyright © 2011-2022 走看看