Bus System
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10903 Accepted Submission(s): 2982
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.
由于中国人口众多,公共交通非常重要。公交车是传统公交系统中一种重要的交通方式。即使在现在,它仍在发挥重要作用。
X城市的公交系统很奇怪。与其他城市的系统不同,车票的费用是根据两个车站之间的距离计算的。这是一个描述距离和成本之间关系的列表。
你的邻居是一个真正的苦难者。他请您帮助他计算列出的两个电台之间的最低费用。你能为他解决这个问题吗?
为了简化此问题,可以假设所有测站都位于一条直线上。我们使用x坐标来描述车站的位置。
输入值
输入包含几个测试用例。首先,案件数是一个数字。不超过20个案例。
每个案例在第一行包含八个整数,分别是L1,L2,L3,L4,C1,C2,C3,C4,每个数字均为非负数且不大于1,000,000,000。您还可以假定L1 <= L2 <= L3 <= L4。
接下来给出两个整数n和m,分别代表站点和问题的数量。接下来的n行中的每行包含一个整数,表示第i个站的x坐标。接下来的m行中的每行包含两个整数,分别代表起点和终点。
在所有问题中,起点将与目的地不同。
对于每种情况2 <= N <= 100,0 <= M <= 500,每个x坐标在-1,000,000,000到1,000,000,000之间,并且没有两个x坐标具有相同的值。
输出量
对于每个问题,如果两个站均可以到达,请在两个站之间打印最低费用。否则,打印“无法获得X站和Y站。”使用示例中的格式。
解题思路:这道题是HDU上最短路的题,题面很长,需要仔细读题才能读懂,我一开始没有读懂题。关于题目大意:输入一个T,表示有T组测试样例,对于每组测试样例,先输入L1 L2 L3 L4 C1 C2 C3 C4八个整数,根据图片中的信息,可以表示距离和钱数的关系,他们是一一对应的,L1对C1… 即距离在0-L1 时,他的费用时C1。然后输入n和m,表示一共有n个站,m个搜索。接下来输入n行数字,表示第 i 个站的x坐标,思路是先开n*n的地图数组,把地图存进去并初始化,再开一个站的数组,然后根据输入的站来判断彼此之间的距离,根据距离和金钱的关系填充地图,然后用Floyd算法松弛,最后判断能否从一个站到达另一个站,如果可以输出钱数,不可以则输出对应的信息(注意一下格式),这里由于和可能会很大,所以mp定义Long long类型。贴AC代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=105;
const ll inf=0x3f3f3f3f3f3f3f3f;//最大值一定要够大
ll mp[N][N];//地图数组
int t,n,m;
int l1,l2,l3,l4,c1,c2,c3,c4;
int station[N];//站数组,用来求距离
int main()
{
void floyd();
int cnt=1;//case i
cin>>t;
while(t--)
{
cin>>l1>>l2>>l3>>l4>>c1>>c2>>c3>>c4;
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
if(i==j)
mp[i][j]=0;
else
mp[i][j]=mp[j][i]=inf;//初始化地图
for(int i=1;i<=n;i++)
cin>>station[i];
for(int i=1;i<=n;i++)//根据站与站之间的距离来决定钱数
for(int j=i+1;j<=n;j++)
{
int l=abs(station[i]-station[j]);
if(l>0&&l<=l1)
mp[i][j]=mp[j][i]=c1;
else if(l>l1&&l<=l2)
mp[i][j]=mp[j][i]=c2;
else if(l>l2&&l<=l3)
mp[i][j]=mp[j][i]=c3;
else if(l>l3&&l<=l4)
mp[i][j]=mp[j][i]=c4;
}
floyd();//松弛操作
printf("Case %d:
",cnt++);
while(m--)
{
ll a,b;
cin>>a>>b;
if(mp[a][b]==inf)
printf("Station %d and station %d are not attainable.
",a,b);
else
printf("The minimum cost between station %d and station %d is %lld.
",a,b,mp[a][b]);//一定注意这里的格式是lld,因为这个WA了10次...我肯定记住了
}
}
return 0;
}
void floyd()
{
for(int j=1;j<=n;j++)
for(int i=1;i<=n;i++)
for(int k=1;k<=n;k++)
mp[i][k]=min(mp[i][k],mp[i][j]+mp[j][k]);
}