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.
Input Specification:
Each input file contains one test case. For each case, the first line contains 4 positive numbers: C m a x ( ≤ 100 ) C_{max}(≤ 100) Cmax(≤100), the maximum capacity of the tank; D ( ≤ 30000 ) D (≤30000) D(≤30000), the distance between Hangzhou and the destination city; D a v g ( ≤ 20 ) D_{avg}(≤20) Davg(≤20), the average distance per unit gas that the car can run; and N ( ≤ 500 ) N (≤ 500) N(≤500), the total number of gas stations. Then N N N lines follow, each contains a pair of non-negative numbers: P i P_i Pi, the unit gas price, and D i ( ≤ D ) D_i(≤D) Di(≤D), the distance between this station and Hangzhou, for i = 1 , ⋯ , N i=1,⋯,N i=1,⋯,N. All the numbers in a line are separated by a space.
Output Specification:
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.
Sample Input 1:
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
Sample Output 1:
749.17
Sample Input 2:
50 1300 12 2
7.10 0
7.00 600
Sample Output 2:
The maximum travel distance = 1200.00
题意
从起点到终点上有许多加油站。车的油箱有最大油量。假设开始没有油,给出每个加油站的距离和油价,计算从起点到终点所需的最小的油量,如果不能到达就计算能走的最远距离。
思路
有一种特殊情况:第一个加油站不在起点,那么无法行走。
贪心思想:总是在尽可能便宜的加油站加尽可能多的油。在终点设置一个油价为0的加油站,排除上述特殊情况,问题变为从第1个加油站走到最后一个加油站。在每个加油站只需考察所有当前能到达的加油站,选一个油价最低的,如果这个油价比当前油价还要大,那么就要在当前加油站加满油,否则只需要保证能够行驶到这个加油站就可以了。
代码
#include <iostream>
#include <algorithm>
using namespace std;
struct st {
double p, d;
} station[600];
int main() {
int n;
double cm, d, da, dx, go = 0, left_c = 0, sum = 0;
/*
cm 油箱的最大容量
d 起点到终点的距离
da 单元油量可以走的距离
dx 加满油后能走多远
go 已经走了多远
left_c 油箱里的油剩多少
sum 油费
*/
cin >> cm >> d >> da >> n;
dx = cm * da;
for (int i = 0; i < n; ++i)
cin >> station[i].p >> station[i].d;
sort(station, station + n, [](st &a, st&b){ return a.d < b.d;});
if (station[0].d != 0) { // 第一个加油站不在起点
cout << "The maximum travel distance = 0.00";
return 0;
}
station[n].d = d; // 在终点放一个假的加油站,油价为0
int pos = 0;
while (pos != n) {
if (station[pos + 1].d > go + dx) { // 无法到达下一个加油站
go += dx;
break;
}
int next_pos = pos + 1; // 下一个应该到达的加油站
for (int i = next_pos; i <= n && station[i].d <= go + dx; ++i) { // 遍历所有能到达的加油站
if (station[i].p <= station[next_pos].p) { // 找到一个油价不超过选定站的油价的加油站
if (station[i].p <= station[pos].p) { // 油价不超过当前的油价
next_pos = i; // 可以确定这个站就是下一个应该到达的加油站
break;
} else if (station[i].p < station[next_pos].p) { // 油价大于当前的油价但是小于选定站的油价
next_pos = i; // 更新选定的站
}
}
}
double len = station[next_pos].d - station[pos].d; // 到达选定站的距离
if (station[next_pos].p <= station[pos].p) { // 比较当前和选定站的油价,如果选定站更便宜
if (left_c < len / da) { // 必须要在当前站加油才能走到选定站
sum += (len / da - left_c) * station[pos].p; // 在当前站加正好能走到选定站的油量
left_c = 0;
} else left_c -= len / da; // 否则不加油
} else { // 否则在当前站加满油
sum += (cm - left_c) * station[pos].p;
left_c = cm - len / da;
}
go += len;
pos = next_pos;
}
if (pos == n)
printf("%.2f", sum);
else
printf("The maximum travel distance = %.2f", go);
}