1033 To Fill or Not to Fill (25 分)
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: 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.
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
一,注意点
1,本题我的卡壳之处在于如何处理到达终点的那段里程,正确的方法是在原有的加油站的基础上加一个价钱为0,距离为到终点距离的加油站。
2,另外我还存在思路不清的问题,实际上本题的关键之处就在于找加油站,两种情况,找到价钱比当前小的加油站(相等不可),或者是所有的价钱比当前加油站大的话找相对最小的那一个。然后对每一种情况分别处理即可。
3,本题我最后纠结的点是为什么若是以后的加油站的价钱都比当前加油站大的话,要在当前加油站加满油,其实是由于距离的原因无法探索到更小的加油站,若想探索到就必须在以后的那些价钱大的加油站加油,能加的少一些就加的少一些。
二,我的代码
#include<cstdio>
#include<algorithm>
using namespace std;
const int INF = 1000000000;
const int max_n = 600;
struct GasStation {
double price;
double distance;
}gas_station[max_n];
bool cmp(GasStation A, GasStation B) {
return A.distance < B.distance;
}
int main() {
int Cmax = 0, D = 0, Davg = 0, N = 0;
scanf("%d %d %d %d", &Cmax, &D, &Davg, &N);
int id_of_station = 0;
double max_running = Cmax*Davg, now_running = 0;
double need_oil = 0, now_oil = 0;
double ans = 0;
for (int i = 0; i < N; i++) {
scanf("%lf %lf", &gas_station[i].price, &gas_station[i].distance);
}
gas_station[N].price = 0;
gas_station[N].distance = D;
sort(gas_station, gas_station + N, cmp);
if (gas_station[0].distance != 0) {
printf("The maximum travel distance = 0.00");
return 0;
}
else {
while (id_of_station < N) {
int k = -1;
double min_price = INF;
for (int i = id_of_station + 1; (i <= N)&&(gas_station[i].distance - gas_station[id_of_station].distance <= max_running);i++) {
if (gas_station[i].price < min_price) {
min_price = gas_station[i].price;
k = i;
if (min_price < gas_station[id_of_station].price) {
break;
}
}
}
if (k == -1)break;
now_running = gas_station[k].distance;
if (min_price < gas_station[id_of_station].price) {
need_oil = (gas_station[k].distance - gas_station[id_of_station].distance) / Davg;
if (need_oil > now_oil) {
ans += (need_oil - now_oil)*gas_station[id_of_station].price;
now_oil = 0;
}
else {
now_oil -= need_oil;
}
}
else {
need_oil = (gas_station[k].distance - gas_station[id_of_station].distance) / Davg;
ans += (Cmax - now_oil)*gas_station[id_of_station].price;
now_oil = Cmax - need_oil;
}
id_of_station = k;
}
if (id_of_station == N) {
printf("%.2f", ans);
}
else {
printf("The maximum travel distance = %.2f", now_running + max_running);
}
}
return 0;
}