pat

题目

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; $D
{avg}$ $(≤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: $P_i$, the unit gas price, and $D_i$​ $(≤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 Input1:

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 Output1:

749.17

Sample Input2:

50 1300 12 2
7.10 0
7.00 600

Sample Output2:

The maximum travel distance = 1200.00

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef pair<double, double> dd;
typedef vector<dd> vdd;
const int INF = 0x3f3f3f3f;
int (){
double Cmax, D, Davg, pi, di;
int N;
scanf("%lf %lf %lf %d", &Cmax, &D, &Davg, &N);
vdd V(N+1, dd());
V[0] = make_pair(D, 0.0);
for(int i = 1;i <= N;i++){
scanf("%lf %lf", &pi, &di);
V[i] = make_pair(di, pi);
}
sort(V.begin(), V.end());
if(V[0].first != 0.0){
printf("The maximum travel distance = 0.00");
return 0;
}
double nowDist = 0.0, maxDist = 0.0, nowPrice = V[0].second, totalPrice = 0.0, leftDist = 0.0;
while(nowDist < D){
maxDist = nowDist + Cmax Davg;
double minPrice = INF, minDist = 0.0;
bool flag = false;
for(int i = 1;i <= N && V[i].first <= maxDist;i++){
if(V[i].first <= nowDist) continue;
if(V[i].second < nowPrice){
totalPrice += (V[i].first - nowDist - leftDist)
nowPrice / Davg;

leftDist = 0.0;
nowPrice = V[i].second;
nowDist = V[i].first;
flag = true;
break;
}
if(V[i].second < minPrice){
minPrice = V[i].second;
minDist = V[i].first;
}
}
if(!flag && minPrice != INF){
totalPrice += (nowPrice (Cmax - leftDist / Davg));
leftDist = Cmax
Davg - (minDist - nowDist);
nowPrice = minPrice;
nowDist = minDist;
}
if(!flag && minPrice == INF){
nowDist += Davg * Cmax;
printf("The maximum travel distance = %.2f", nowDist);
return 0;
}
}
printf("%.2f", totalPrice);
return 0;
}