トピックリンク:https://pintia.cn/problem-sets/994805342720868352/problems/994805458722734080
题目描述
可能な高速道路では、他の都市への杭州から車を運転するのは簡単です。車のタンク容量が限られているので、しかし、我々は随時途中でガソリンスタンドを見つける必要があります。別のガソリンスタンドは、異なる価格を与える可能性があります。あなたは慎重に行くための最も安いルートを設計することが求められます。
输入
各入力ファイルは、1つのテストケースが含まれています。各場合について、最初の行は、4つの正数含ま:C MAX(≤100)、タンクの最大容量を、D(≤30000)、杭州と目的地の都市の間の距離。Dの平均
(≤20)、車が実行できることを、単位ガスあたりの平均距離。及びN(≤500)、ガソリンスタンドの合計数。各非負の数の対を含み、追従N行:P I、単位ガス価格、およびD I(≤D)、このステーションと杭州の間の距離、I = 1のため、 ⋯、N。行のすべての数字は、スペースで区切られます。
输出
各テストケースの場合は、ラインで最も安い価格を印刷し、小数点以下2桁の精度アップ。タンクが最初に空であることが想定されます。それは目的地に到達することは不可能であるならば、車Xが可能な最大距離である= Xの最大移動距離を印刷し、小数点以下2桁の精度アップを実行することができます。
サンプル入力
501300128
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000年
6.85 300
サンプル出力
749.17
コード
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 510;
const int INF = 100000000;
struct station{
double price, dis;
}st[maxn];
bool cmp(station a, station b) {
return a.dis < b.dis;
}
int main() {
int n;
double Cmax, D, Davg;
scanf("%lf%lf%lf%d", &Cmax, &D, &Davg, &n);
for(int i = 0; i < n; i++)
scanf("%lf%lf", &st[i].price, &st[i].dis);
st[n].price = 0;
st[n].dis = D;
sort(st, st + n, cmp);
if(st[0].dis != 0)
printf("The maximum travel distance = 0.00\n");
else {
int now = 0;
double ans = 0, nowtank = 0, max = Cmax * Davg;
while(now < n) {
int k = -1; //标记下一个加油站的位置
double pricemin = INF;
for(int i = now + 1; i <= n && st[i].dis - st[now].dis <= max; i++) {
if(st[i].price < pricemin) {
pricemin = st[i].price;
k = i;
if(pricemin < st[now].price)
break;
}
}
if(k == -1) //未找到
break;
double need = (st[k].dis - st[now].dis) / Davg;
if(pricemin < st[now].price) {
if(nowtank < need) {
ans += (need - nowtank) * st[now].price;
nowtank = 0;
}
else
nowtank -= need;
}
else {
ans += (Cmax - nowtank) * st[now].price;
nowtank = Cmax - need;
}
now = k;
}
if(now == n)
printf("%.2f\n", ans);
else
printf("The maximum travel distance = %.2f\n", st[now].dis + max);
}
return 0;
}