PAT [1033] To Fill or Not to Fill (30)

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:

1
2
3
4
5
6
7
8
9
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:

1
749.17

Sample Input 2:

1
2
3
50 1300 12 2
7.10 0
7.00 600

Sample Output 2:

1
The maximum travel distance = 1200.00

贪心思想:
0、寻找比自己距离远的,到能够到达的最大距离之间的加油站,看他们的油价。如果找到了更低价格的油价,就加油到刚好能到达那个加油站的距离的油,然后去那个更低价格的加油站(有更低的我一分都不想多花在别的距离上,只加到刚好满足更低价格的加油站的距离就行,那样以后的路程我就可以以更低的价格行驶)
1、如果找不到更低的,就找尽可能低的油价的加油站,在当前加油站加满油之后过去。因为想要让路程上使用的尽可能是低价的油,既然没有比当前更低价格的了,就让油箱加到最大值,这样能保证利益最大化,保证最大的距离使用的是便宜的油。
2、如果往后找我能到的范围内没有车站(把终点也作为一个价格为无穷的车站),就认为不能到达,就要输出最远的行驶距离。

备注:
0、元素有两个属性信息的话可以用pair来做
1、复杂的问题可以先不考虑优化,先把程序框架写出来,再缩减代码优化

code

c++
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;

typedef struct
{
double pos;
double price;
}gasstation;

gasstation gasst[502];

bool cmp(gasstation a,gasstation b)
{
if(a.pos<b.pos)
return true;
return false;
}

int main()
{
double Cmax,D,Davg;
int N;
scanf("%lf%lf%lf%d",&Cmax,&D,&Davg,&N);
int i;
for(i=0;i<N;i++)
scanf("%lf%lf",&gasst[i].price,&gasst[i].pos);
sort(gasst,gasst+N,cmp);
if(D==0)
{
printf("0.00\n");
return 0;
}
if(gasst[0].pos!=0)
{
printf("The maximum travel distance = 0.00\n");
return 0;
}
int curstnum=0; //当前所处的油站编号,即当前的位置
double curgas=0; //当前的油量
double curcost=0; //当前的花费
bool flag=false; //是否达到目的
double maxrundis=Cmax*Davg; //邮箱加满最远能行驶的距离

while(!flag)
{
bool tag=false; //最大距离内是否有加油站
bool ifcheaper=false; //是否有便宜的
double cheapestprice=10000; //找出最便宜的
int cheapestnum; //没有更便宜的情况下,找出最便宜的

for(i=curstnum+1;i<N;i++)
{
if((gasst[i].pos-gasst[curstnum].pos)<=maxrundis) //范围内
{
tag=true; //有加油站
if(gasst[i].price<gasst[curstnum].price) //情况3-a
{ //且有更便宜的
ifcheaper=true;
double dist=gasst[i].pos-gasst[curstnum].pos;
double needgas=dist/Davg-curgas;
curgas=0;
curcost+=(needgas*gasst[curstnum].price);
curstnum=i;
break;
}
if(gasst[i].price<cheapestprice)
{
cheapestprice=gasst[i].price;
cheapestnum=i;
}
}
else
break;
}

if(!ifcheaper&&(maxrundis>=(D-gasst[curstnum].pos))) //说明已经可以到达目的地了,情况1
{
double dist=D-gasst[curstnum].pos;
double needgas=dist/Davg-curgas;
curcost+=needgas*gasst[curstnum].price;
printf("%.2lf\n",curcost);
return 0;
}
if(tag&&!ifcheaper) //情况3-b
{
double needgas=Cmax-curgas;
curcost+=(needgas*gasst[curstnum].price);
double dist=gasst[cheapestnum].pos-gasst[curstnum].pos;
curgas=Cmax-dist/Davg;
curstnum=cheapestnum;
}
else if(!tag) //情况2
{
printf("The maximum travel distance = %.2lf\n",gasst[curstnum].pos+maxrundis);
return 0;
}
}

return 0;
}