题目
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
max(≤ 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
题目大意
给出很多个加油站和加油站的相关信息。要求求出如何加油整个路径上花费最短
代码和思路
贪心策略:
- 从当前加油站开始往前看,寻找可到达的加油站中最便宜的一个
- 如果在可到达加油站中有比当前还便宜的加油站,那么就只加到那个最便宜的加油站
Coding问题:
- 一定要在所有加油站都录入以后把终点也当做加油站录入进去。让终点加油站的单价为0,这样最后到达的一定是题目要求的终点距离
- 以所到达的加油站编号为循环变量,直到到达最后一个加油站循环跳出,中间设置如果无法到达下一个加油站的情况下人为中断。
- 每次循环结束,都计算出要到达的下一个加油站,同时更新花费,即之前的花费加当前加油站的花费,和更新油箱的剩余容量(这是指到达下一个加油站后能剩余的)。不需要更新距离,因为如果无法到达,跳出时只需要加上当前加油站距离起点的距离和车所能行使的最大距离即可。
#include<cstdio>
#include<algorithm>
using namespace std;
const int INF = 10000000;
const int MAX = 510;
struct station {
double price, dis;
}sta[MAX];
bool cmp(station a, station b) {
return a.dis < b.dis;
}
int main() {
int n;
double Cmax, Davg, D;
scanf("%lf %lf %lf", &Cmax, &D, &Davg);
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%lf %lf", &sta[i].price, &sta[i].dis);
}
sta[n].dis = D;
sta[n].price = 0.0;
sort(sta, sta + n, cmp);
if (sta[0].dis != 0) {
printf("The maximum travel distance = 0.00\n");
}
else {
int now = 0;
double nowTank = 0, MAX = Cmax * Davg, ans = 0;
while (now < n) {
int index = -1;
double min = INF;
for (int i = now + 1; i <= n && sta[i].dis - sta[now].dis <= MAX; i++) {
if (sta[i].price < min) {
min = sta[i].price;
index = i;
if (min < sta[now].price) {
break;
}
}
}
if (index == -1) break;
double need = (sta[index].dis - sta[now].dis) / Davg;
if (min < sta[now].price) {
if (nowTank < need) {
ans += (need - nowTank) * sta[now].price;
nowTank = 0; //到达便宜加油站的时候油刚刚好用完
}
else {
nowTank -= need;
}
}
else {
ans += (Cmax - nowTank) * sta[now].price;
nowTank = Cmax - need;
}
now = index;
}
if (now == n) {
printf("%.2f\n", ans);
}
else {
printf("The maximum travel distance = %.2f\n", sta[now].dis + MAX);
}
}
return 0;
}