Bus System
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8294 Accepted Submission(s): 2204
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.
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
题意:t组数据,然后给你四个L和四个C,分别代表一定距离内的花费。让求两点间的最小花费。
思路:这里要注意一下大于L4的时候No ticket是无法支付,无穷大的意思。因为N才100,邻接矩阵,所以明显的Floyd了。还有要进行取绝对值。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const ll INF = 1ll << 50;
const int N = 505;
ll G[N][N];
ll a[N];
ll L[5],C[5];
ll dist(ll d)
{
if(d > 0 && d <= L[1])
return C[1];
else if(d >= L[1] && d <= L[2])
return C[2];
else if(d >= L[2] && d <= L[3])
return C[3];
else if(d >= L[3] && d <= L[4])
return C[4];
else
return INF;
}
void Floyd(int n)
{
for(int k = 1;k <= n;k++)
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
G[i][j] = min(G[i][j],G[i][k]+G[k][j]);
}
int main()
{
int t,n,m;
cin >> t;
for(int ca = 1;ca <= t;ca++)
{
for(int i = 1;i <= 4;i++)
scanf("%lld",&L[i]);
for(int i = 1;i <= 4;i++)
scanf("%lld",&C[i]);
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i++)
scanf("%lld",&a[i]);
printf("Case %d:\n",ca);
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
if(i == j) G[i][j] = 0;
else G[i][j] = INF;
for(int i = 1;i <= n;i++)
for(int j = i+1;j <= n;j++)
G[i][j] = G[j][i] = dist(abs(a[j]-a[i]));
Floyd(n);
int u,v;
while(m--)
{
scanf("%d%d",&u,&v);
if(G[u][v] == INF)
printf("Station %d and station %d are not attainable.\n",u,v);
else
printf("The minimum cost between station %d and station %d is %lld.\n",u,v,G[u][v]);
}
}
return 0;
}