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.\n",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.\n",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:\n",k++);
68         while (m--)
69         {
70             int a,b;
71             cin>>a>>b;
72             Dijkstra(a,b);
73         }
74     }
75 }



hdu1690 Bus System(最短路 Dijkstra)

时间: 2024-12-19 23:59:52

hdu1690 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 最短路 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(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 popula

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

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

POJ 2394 Checking an Alibi (最短路+Dijkstra)

Checking an Alibi Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6217   Accepted: 2257 Description A crime has been comitted: a load of grain has been taken from the barn by one of FJ's cows. FJ is trying to determine which of his C (1

poj 1135 最短路 dijkstra

传送门 http://poj.org/problem?id=1135 建模分两部分:1.如果最后是关键牌倒下,那么找最短路中最长的就行--最远的倒下,其他的牌一定倒下,所以找最远的最短路 2.如果最后是普通牌倒下,那么找三角形,三角形周长的一半就是倒下的位置 到底是情况1还是情况2,自己在脑子模拟一下就能想到,还是那句话,最难倒下的倒下了,其他的一定都倒下了,若第二种情况的时间比第一种长,那么就是第二种,反之,第一种 上代码 /**********************************

POJ1062 Expensive dowry 【最短路dijkstra】

详细看:http://blog.csdn.net/lyy289065406/article/details/6645852 简单说一下:每个物品是一个结点,边的权值是,edge[u][v]的值表示用物品u换物品v的价格 一开始所有物品都置为原价,即所有dist[i]为原价,用dijkstra算法,算出0点(啥物品都没有)到各点的最短距离,求出dist[1]即为花费 枚举每个物品的等级为这条交易链的最高等级,把所有等级高于它的或者比它小超过等级限制的点都剔除,可以用bool数组剔除,然后用上述的d

POJ 2387 Til the Cows Come Home (最短路+Dijkstra)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29550   Accepted: 9935 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the