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
题目大意:
当路可行的时候,开车从杭州去一个城市非常简单。但是当车的油量有限的时候,在路上时我们必须时不时地寻找加油站点。不同的加油站点有不同的油价,你必须仔细考虑并找到一条最便宜的道路。
题目给出杭州开往某个城市的路上的加油站点信息,要求你求出最省钱的加油方案,输出花费的价格,如果采用任何加油方案都不能到达该城市,则输出The maximum travel distance = X,X为最远到达的距离。
解题思路:
参考柳神博客https://www.liuchuo.net/archives/2461
代码如下:
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct Station{
double price,dis;
};
double Cmax,D,Davg,nowPrice = 0.0,nowDis = 0.0,maxDis = 0.0,sumPrice = 0.0,leftDis = 0;
int N;
const int inf = 99999999;
bool compare(Station a,Station b){
return a.dis < b.dis;
}
int main(){
scanf("%lf%lf%lf%d",&Cmax,&D,&Davg,&N);
vector<Station> stations(N+1);
stations[0].dis = D;
stations[0].price = 0.0;
for(int i = 1 ; i <= N ; i ++){
double price,dis;
scanf("%lf%lf",&price,&dis);
stations[i].price = price;
stations[i].dis = dis;
}
sort(stations.begin(),stations.end(),compare);
if(stations[0].dis != 0){//第一个加油站必须在0的位置,因为油箱是空的
printf("The maximum travel distance = 0.00");
return 0;
}else{
nowPrice = stations[0].price;
}
while(nowDis < D){
maxDis = nowDis + Cmax * Davg;//能到达的最远距离
double minPrice = inf,minPriceDis = 0.0;
bool flag = false;
for(int i = 1 ; i <= N && stations[i].dis <= maxDis; i ++){
if(stations[i].dis <= nowDis)
continue;
if(stations[i].price < nowPrice){//找到了比当前价格更小的
sumPrice += (stations[i].dis - nowDis - leftDis) / Davg * nowPrice;
leftDis = 0.0;
nowPrice = stations[i].price;
nowDis = stations[i].dis;
flag = true;
break;
}
if(stations[i].price < minPrice){
minPrice = stations[i].price;
minPriceDis = stations[i].dis;
}
}
if(flag == false && minPrice != inf){//没有比自己当前价格小的,但找到了其中可行最远距离内的最小价格,加满当前便宜的油过去
sumPrice += nowPrice * (Cmax - leftDis / Davg);
leftDis = Cmax*Davg - (minPriceDis - nowDis);
nowPrice = minPrice;
nowDis = minPriceDis;
}else if(flag == false && minPrice == inf){//在可行距离范围内没有加油站
nowDis += Cmax * Davg;
printf("The maximum travel distance = %.2f",nowDis);
return 0;
}
}
printf("%.2f",sumPrice);
return 0;
}