PAT A1033

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” 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

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "stdio.h"
//#include "math.h"
#include "string.h"
#include "algorithm"
using namespace std;
const int maxn = 510;
const int INF = 1.0e9;
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;//满油无法到达下一个加油站,退出循环
}
//下面为能找到可到达的加油站k,计算转移花费
//need为从now到k所需要的油量
double need = (st[k].dis - st[now].dis) / Davg;
if (priceMin < st[now].price) {//如果加油站k的油价低于当前油价
//只买足够到达加油站k的油
if (nowTank < need) {//当前油量不足need
ans += (need - nowTank) * st[now].price;//补足need
nowTank = 0;//到达加油站k后油量变为0
}else{
nowTank -= need;//油量足够,直接到达加油站k
}
}else{//如果加油站k的油价高于当前油价
ans += (Cmax - nowTank) * st[now].price;//加满油
nowTank = Cmax - need;//加满油后到达加油站k剩余
}
now = k;//到达加油站k
}
if (now == n){
//到达目的地
printf("%.2f\n", ans);
}else{
printf("The maximum travel distance = %.2f\n", st[now].dis + MAX);
}
}
return 0;
}

策略1:优先前往油价更低的加油站
策略2:在没有更低油价的加油站时,往油价尽可能低的加油站
合并在一起,就是在所有满油状态能到达的加油站中,选出油价最低的那个加油站,而一旦在枚举过程中找到了第一个油价低于当前油价的加油站,则退出循环,结束选择过程。