题目链接: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
whereX
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,考虑从0距离开始能否到达最后一个加油站的问题
1.因为先开始没有油,所以如果所有的加油站距离都没有等于0的,那么说明车哪也去不了,直接输出并return
2.将加油站按照距离dis从小到大排序
3.先去第一个加油站,设置变量nowdis表示当前所在的距离,maxdis是能够到达的最大距离,nowprice是当前的站点的价格,totalPrice是总的价格。
贪心思想:
0.寻找比自己距离远的,到能够到达的最大距离之间的加油站,看他们的油价。如果找到了更低价格的油价,就加油到刚好能到达那个加油站的距离的油,然后去那个更低价格的加油站(有更低的我一分都不想多花在别的距离上,只加到刚好满足更低价格的加油站的距离就行,那样以后的路程我就可以以更低的价格行驶啦)
1.如果找不到更低的,就找尽可能低的油价的加油站,在当前加油站加满油之后过去。因为想要让路程上使用的尽可能是低价的油,既然没有比当前更低价格的了,就让油箱加到最大值,这样能保证利益最大化,保证最大的距离使用的是便宜的油。
————————————————
版权声明:这部分为优快云博主「柳婼」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/liuchuo/article/details/52497109
*/
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 5e2 + 5;
struct station{//车站结构体
double price, dis;
}sta[maxn];
bool cmp(station &a, station &b){//自定义比较方式
return a.dis < b.dis;
}
int main()
{
// 读入数据
double cmax, d, davg; int n;
cin >> cmax >> d >> davg >> n;
for(int i = 0; i < n; i++){
scanf("%lf %lf", &sta[i].price, &sta[i].dis);
}
// 哨兵,也即额外结点--终点加油站
sta[n] = {0.0, d};
sort(sta, sta + n + 1, cmp);
// 起始点没有加油站则直接结束了
double nowdis = 0.0, maxdis = 0.0, nowprice = 0.0, leftdis, consume = 0.0;
if(sta[0].dis){
printf("The maximum travel distance = 0.00\n");
return 0;
}
else nowprice = sta[0].price;
int index = 0;// 记录已到达加油站的下标
while(nowdis < d){
maxdis = nowdis + cmax * davg;
double minPrice = 1e7;
bool flag = false;
for(int i = index + 1; i <= n && sta[i].dis <= maxdis; i++){
// 如果范围内有更低价格的加油站,则换用该加油站的油接着走
if(sta[i].price < nowprice){
consume += (sta[i].dis - nowdis - leftdis) * nowprice / davg;
nowdis = sta[i].dis;
nowprice = sta[i].price;
index = i;
leftdis = 0.0;
flag = true;
break;
}
// 获取接下来最低价格的加油站
if(sta[i].price < minPrice){
minPrice = sta[i].price;
index = i;
}
}
// 没有更低价格的加油站,则到最低加油站加满油凑合,但之前更划算的油保留接着用
if(!flag && minPrice != 1e7){
consume += nowprice * (cmax - leftdis / davg);
leftdis = cmax * davg - (sta[index].dis - nowdis);
nowdis = sta[index].dis;
nowprice = minPrice;
}
// 没有加油站了,则只能跑完当前油结束旅程
// 因为终点位置已经被设置成哨兵加油站,这种情况说明到不了终点
if(!flag && minPrice == 1e7){
printf("The maximum travel distance = %.2lf\n", maxdis);
return 0;
}
}
// 到达终点,输出最低价格
printf("%.2lf\n", consume);
return 0;
}