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: Cma**x (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; Dav**g (≤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: P**i, the unit gas price, and D**i (≤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
思路
这题一开始以为是背包问题,但是发现,每个加油点存在的选择不仅有加不加的问题,还有加多少的问题。
因此我们要设计一个加油策略,保证每次使用的油最便宜。很直观的,我们可以想到,在便宜的地方我们要多加油,但是贵的地方,如果不加油到不了下一个油站,我们也只能加油,因此利用贪心可以推出以下策略。
前提:将加油站按距离排序,将终点作为一个油价为0的加油站
①当前加油站,是满油状态所能到达的地方当中最便宜的地方,那么就加满
②满油状态所能到达的地方当中,存在另一个便宜的加油站(这里不要求是最便宜,只需要返回离当前位置最近,且比当前便宜的加油站就行),那么只需保证目前油箱的油可以到达那个加油站即可(不够肯定要添一部分)
如果到不了终点,记得输出最大距离。
代码
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
#include <stack>
#include <functional>
#include <limits.h>
using namespace std;
struct Node{
double pos;
double price;
bool operator< (Node b) const{
return pos < b.pos;
}
};
Node node[600];
double D, Da, C;
int N;
int minIndex(int i, double maxD){
double iprice = node[i].price;
double ipos = node[i].pos;
int index = -1;
for(int j = i + 1; j <= N; j++){
// out of distance
if(node[j].pos > ipos + maxD) break;
//index = -1;
if(node[j].price < iprice){
return j;
}
}
return index;
}
int main() {
scanf("%lf%lf%lf%d", &C, &D, &Da, &N);
double maxD = C * Da; // calculate the max distance with full tank
double sum = 0;
for(int i = 0; i < N; i++){
scanf("%lf%lf", &node[i].price, &node[i].pos);
}
node[N] = {D, 0};
sort(node, node + N + 1);
if(node[0].pos != 0){
printf("The maximum travel distance = 0.00");
return 0;
}
double num = 0;
for(int i = 0; i <= N; i++){
//
if(i){
num -= (node[i].pos - node[i - 1].pos) / Da;
if(num < 0.0){
double distance = node[i].pos + num * Da;
printf("The maximum travel distance = %.2lf", distance);
//cout << "The maximum travel distance = " << distance << endl;
return 0;
}
}
if(i == N) break;
// choose the amount of added oil
int index = minIndex(i, maxD);
if(index == -1){
//fill up
sum += (C - num) * node[i].price;
num = C;
}
else{
// cost of going to minIndex
double t = (node[index].pos - node[i].pos) / Da;
if(num < t){
sum += (t - num) * node[i].price;
num = t;
}
}
}
printf("%.2lf", sum);
return 0;
}