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

    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.
     
     
    题目大意:乘公交车的价格随公交站距离的远近有不同的标准,就是按照每个测试数据第一行的数字,接着是有n个站点有m个提问,接着n行,假设有个原点,所有站点在一条直线上,n行每个数字表示第i个站点距离原点的距离,m个提问,表示出发点和终点;
     
     
    直接用Dijkastra就ok了 稍稍做一点点的变形,要注意的本题数据比较大 在定义最大值常量的时候要注意 一开始还WA了好多遍 结果定义成
    const __int64 inf=0xffffffffffffff;
    就过了,输入输出也要用__int64 !


     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 const __int64 inf=0xffffffffffffff;
     5 __int64 dist[105],node[105],vis[105];
     6 __int64 l[5],c[5],n;
     7 
     8 __int64 ab(__int64 a)
     9 {
    10     return a>0?a:-a;
    11 }
    12 __int64 cost(__int64 dis)
    13 {
    14     if (dis>=0&&dis<=l[1]) return c[1];
    15     if (dis>l[1]&&dis<=l[2]) return c[2];
    16     if (dis>l[2]&&dis<=l[3]) return c[3];
    17     if (dis>l[3]&&dis<=l[4]) return c[4];
    18 }
    19 
    20 void Dijkstra(__int64 start,__int64 end)
    21 {
    22     for(int i=1; i<=n; i++)
    23         node[i]=inf,vis[i]=0;
    24     __int64 tm=start;
    25     node[tm]=0;
    26     vis[tm]=1;
    27     for(int k=1; k<=n; k++)
    28     {
    29         __int64 Min=inf;
    30         for (int i=1; i<=n; i++)
    31             if(!vis[i]&&Min>node[i])
    32             {
    33                 Min=node[i];
    34                 tm=i;
    35                 //cout<<"  "<<tm<<" "<<Min<<endl;
    36             }
    37         if(tm==end)
    38         {
    39             printf("The minimum cost between station %I64d and station %I64d is %I64d.
    ",start,end,node[end]);
    40             return ;
    41         }
    42         vis[tm]=1;
    43         for(int i=1; i<=n; i++)
    44             if(ab(dist[i]-dist[tm])<=l[4]&&!vis[i]&&node[i]>node[tm]+cost(ab(dist[i]-dist[tm])))
    45             {
    46                 //cout<<"  "<<i<<" "<<node[tm]<<" "<<ab(dist[i]-dist[tm])<<" "<<hash[ab(dist[i]-dist[tm])]<<endl;
    47                 node[i]=node[tm]+cost(ab(dist[i]-dist[tm]));
    48             }
    49     }
    50     printf ("Station %I64d and station %I64d are not attainable.
    ",start,end);
    51 }
    52 
    53 
    54 
    55 int main ()
    56 {
    57     int t,k=1;
    58     cin>>t;
    59     while (t--)
    60     {
    61         //int l1,l2,l3,c1,c2,c3,c4;
    62         cin>>l[1]>>l[2]>>l[3]>>l[4]>>c[1]>>c[2]>>c[3]>>c[4];
    63         int m;
    64         cin>>n>>m;
    65         for(int i=1; i<=n; i++)
    66             cin>>dist[i];
    67         printf ("Case %d:
    ",k++);
    68         while (m--)
    69         {
    70             int a,b;
    71             cin>>a>>b;
    72             Dijkstra(a,b);
    73         }
    74     }
    75 }
    
    
    
    
    
     
  • 相关阅读:
    MongoDB 基础
    类加载流程,类加载机制及自定义类加载器详解
    Object中有哪些方法及其作用
    Intellij IDEA 导入Maven项目
    用IDEA开发Spring程序
    深入浅出 Java 8 Lambda 表达式
    UUID.randomUUID()简单介绍
    json字符串转成 json对象 json对象转换成java对象
    字符串转 Boolean 的正确方式
    获取JSON中所有的KEY
  • 原文地址:https://www.cnblogs.com/mis-xiao/p/3928326.html
Copyright © 2011-2022 走看看