题目
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: C m a x Cmax Cmax (≤ 100), the maximum capacity of the tank; D D D (≤30000), the distance between Hangzhou and the destination city; D a v g Davg Davg (≤20), the average distance per unit gas that the car can run; and N N N (≤ 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: P i P_i Pi, the unit gas price, and D i D_i Di (≤ D D D), the distance between this station and Hangzhou, for i i i=1,⋯, N N 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,没有可以直接输出最远距离为0;
对于途中的加油策略:
- 寻找从当前站currSta出发,能够找到价格比当前站currSta油价低的站nextSta,然后在当前站加满足对应距离的的油过去
- 如果不存在上一步的加油站,就找能到达的加油站里面价格最低的,在当前站加满油过去
- 如果在当前站currSta加满油也没有能到达的下一个加油站,就退出,得到最大距离
代码
#include<bits/stdc++.h>
using namespace std;
struct node{
double price,distance;
};
int main(int argc, const char * argv[]) {
double D, Cmax, Davg, maxDriveDis, cost=0, currGas=0;
int N;
scanf("%lf%lf%lf%d", &Cmax, &D, &Davg, &N);
maxDriveDis = Cmax * Davg;
vector<node> station(N+1);
for(int i=0; i<N; i++)
scanf("%lf%lf",&station[i].price, &station[i].distance);
station[N].price = 0;
station[N].distance = D;
sort(station.begin(), station.end(), [](const node& a, const node& b){
return a.distance < b.distance;
});
if(station[0].distance != 0){
printf("The maximum travel distance = 0.00");
return 0;
}
int currSta = 0;
while(currSta < N){
int nextSta = -1;
double minPrice = INT_MAX;
for(int i=currSta+1; i<=N && station[i].distance <= station[currSta].distance + maxDriveDis; i++){
if(station[i].price <= minPrice){
minPrice = station[i].price;
nextSta = i;
}
if(station[i].price < station[currSta].price)
break;
}
if(nextSta == -1){
double res = station[currSta].distance + maxDriveDis;
printf("The maximum travel distance = %.2lf", res);
return 0;
}
double gasNeed = (station[nextSta].distance - station[currSta].distance) / Davg; // 需要充的油量
if(station[nextSta].price < station[currSta].price){
cost += (gasNeed - currGas) * station[currSta].price;
currGas = 0;
}else{
cost += (Cmax - currGas) * station[currSta].price;
currGas = Cmax - gasNeed;
}
currSta = nextSta;
}
printf("%.2lf", cost);
return 0;
}