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.\n",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.\n",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:\n",k++);
        while (m--)
        {
            int a,b;
            cin>>a>>b;
            Dijkstra(a,b);
        }
    }
}

hdu 1690 Bus System(Dijkstra最短路)

时间: 2024-12-20 23:10:09

hdu 1690 Bus System(Dijkstra最短路)的相关文章

hdu 1690 Bus System(最短路)

问题: 链接:点击打开链接 题意: 思路: 代码: #include <iostream> #include <cstdio> #include <cstring> using namespace std; #define INF 1000000000000 typedef __int64 LL; const int N = 110; __int64 dis[N][N],place[N]; __int64 L1,L2,L3,L4,C1,C2,C3,C4; int n,m

hdu 1690 Bus System (最短路径)

Bus System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6082    Accepted Submission(s): 1560 Problem Description Because of the huge population of China, public transportation is very importa

hdu 1690 Bus System(Floyd)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1690 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

hdu 1690 Bus System 最短路 Floyd算法。。INF一定要很大很大。。。数据类型用long long。

Bus System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7163    Accepted Submission(s): 1853 Problem Description Because of the huge population of China, public transportation is very import

HDU 1690 Bus System 任意点最短路径Floyd

原题: http://acm.hdu.edu.cn/showproblem.php?pid=1690 题目大意: 图中的表是代表不同长度路径的花费,输入所有点的坐标,求任意两点间的最短花费. 因为是求任意两点,这里最好是用floyd算法. 题中几大坑: 数据可能会超int,要用long long int: 坐标可以为负,求距离要用abs绝对值函数. 参考代码如下: #include <iostream> #include"iostream" #include"st

hdu 1690 Bus System

floyd算法.这题目比较操蛋,首先INF需要比较大,我选择了INF = 0xffffffffffffff.还有一点就是%lld会挂掉的,用%I64d才能AC. #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; const int maxn = 105; __int64 INF = 0xffffffffffffff; __in

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 sys

hdu 1960 Bus System

Bus System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6166    Accepted Submission(s): 1580 Problem Description Because of the huge population of China, public transportation is very importa

Bus System 【dijkstra算法】

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 qui