Bus System
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4973 Accepted Submission(s): 1239
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.
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.
![](https://i-blog.csdnimg.cn/blog_migrate/3ded0715437d1f93a616d55d648f779f.jpeg)
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.
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.
Source
Recommend
wangye
题目意思就是说。做公交车收费安距离收费。l1到l2之间收c2元...............。
注意是是总距离不是l1到l2的部分。开始想到初中数学题的算利息了。。。汗。。
#include <stdio.h>
__int64 dis[250][250];//分析得数据较大要用int64存
__int64 INF=1111111111111;
int n;
__int64 abs(__int64 x)//貌似数学库函数里没有所以自己写了
{
return x<0?-x:x;
}
void floyd()
{
int k,i,j;
for(k=1; k<=n; k++)
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
if(dis[i][k]+dis[k][j]<dis[i][j])
dis[i][j]=dis[i][k]+dis[k][j];
}
int main()
{
int m,i,j,k,s,e,d,p,t;
__int64 l[5],c[5],x[150];
scanf("%d",&t);
for(k=1; k<=t; k++)
{
for(i=1; i<=4; i++)//记录距离区间
scanf("%I64d",&l[i]);
for(i=1; i<=4; i++)//记录费用区间
scanf("%I64d",&c[i]);
scanf("%d%d",&n,&m);
for(i=1; i<=n; i++)//记录各点坐标
scanf("%I64d",&x[i]);
for(i=1; i<=n; i++)//初始化
for(j=1; j<=n; j++)
{
if(i==j)
dis[i][j]=0;
else
dis[i][j]=INF;
}
for(i=1; i<=n-1; i++)
for(j=i+1; j<=n; j++)
{
p=1;
d=abs(x[j]-x[i]);//算出两点距离
if(d>l[4])//特殊情况优先处理
dis[i][j]=dis[j][i]=INF;
else if(d==0)
{
dis[i][j]=dis[j][i]=0;
}
else
{
while(d>l[p])//算出费用
p++;
dis[i][j]=dis[j][i]=c[p];
}
}
floyd();
printf("Case %d:\n",k);
for(i=0; i<m; i++)
{
scanf("%d%d",&s,&e);
if(dis[s][e]<INF)
printf("The minimum cost between station %d and station %d is %I64d.\n",s,e,dis[s][e]);
else
printf("Station %d and station %d are not attainable.\n",s,e);
}
}
return 0;
}