With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.
Each input file contains one test case. For each case, the first line contains 4 positive numbers: Cmax(≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; Davg(≤20), the average distance per unit gas that the car can run; and N (≤ 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: Pi , the unit gas price, and Di(≤D), the distance between this station and Hangzhou, for i=1,⋯,N. All the numbers in a line are separated by a space.
For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print The maximum travel distance = X where X is the maximum possible distance the car can run, accurate up to 2 decimal places.
50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300
749.17
50 1300 12 2
7.10 0
7.00 600
The maximum travel distance = 1200.00
从A到B的距离为D,路程中有N个加油站,油箱的容积为C,每升油可以行驶Davg。给出每个加油站距离起点A的距离以及每升油的单价。如果能到达B则输出最少加油需要多少钱,如果不能到达终点输出最长的行驶距离。
0号加油站:7.10 0
1号加油站:7.00 150
2号加油站:7.20 200
3号加油站:6.85 300
4号加油站:7.50 400
5号加油站:7.00 600
6号加油站:7.30 1000
7号加油站:6.00 1250
终点:0.00 13000
#include
#include
using namespace std;
struct station
{
double price;
double distance;
}a[1000];
bool cmp(struct station a,struct station b)
{
return a.distance < b.distance;
}
int main(void)
{
double money = 0;
double release;
double C,sum_dis,avg_run;
int S;
scanf("%lf %lf %lf %d",&C,&sum_dis,&avg_run,&S);
double run_max = C * avg_run;
for(int i = 0;i < S;i++)
{
scanf("%lf %lf",&a[i].price,&a[i].distance);
}
a[S].distance = sum_dis;
a[S].price = 0;
sort(a,a+S,cmp);
for(int i = 0;i < S;i++)
{
printf("%d号加油站:%.2f %.0f\n",i,a[i].price,a[i].distance);
}
if(a[0].distance!=0)
{
printf("The maximum travel distance = 0.00");
return 0;
}
int flag = 0; //若找到比当前加油站价格低的置为1
double min = 10000;
int i,j,temp;
for(i = 0;i < S;)
{
flag = 0;
min = 10000;
for(j = i+1;a[i].distance+run_max >= a[j].distance&&j <= S;j++)
{
if(a[j].price < a[i].price) //找到比当前加油站价格低的直接跳出循环
{
money += ((a[j].distance - a[i].distance)/avg_run - release)*a[i].price;
release = 0;
i = j;
flag = 1;
break;
}
else if(a[j].price < min) //没有比当前加油站价格低的寻找价格最低的加油站
{
temp = j;
min = a[j].price;
}
}
if(j == i + 1) //如果当前加油站加满油后不能行使到下一加油站,不能行使到终点
{
printf("The maximum travel distance = %.2f",a[i].distance+run_max);
return 0;
}
if(flag == 0)
{
money += (C - release) * a[i].price;
release = C - (a[temp].distance - a[i].distance)/avg_run;
i = temp;
}
}
printf("%.2f",money);
return 0;
}