PAT 甲级 1033 To Fill or Not to Fill (25 分)(贪心,误以为动态规划,忽视了油量问题)*...
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 (≤), the distance between this station and Hangzhou, for ,. 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
我们不妨从第一个加油站开始算,比较可到达范围内,最便宜的站点是哪个 :
① 如果存在比当前站点还便宜的站点,先到最近的一个站,仅加满到该地的油即可;
② 如果不存在比当前更便宜的站点,那么显然目前这个点是最便宜的,先加满再说。然后去范围内相对最便宜的下一个站点;
③ 如果下一站不在可达范围内,如果此时已经临近终点,那么输出总开销,如果此时终点也不可达,那么输出最远距离。
#include<bits/stdc++.h>
using namespace std;
struct sta{
double p,d;
}a[505];
bool cmp(sta x,sta y){
return x.d<y.d;
}
double cm,dist,davg;
int n;
int main(){
cin>>cm>>dist>>davg>>n;
for(int i=1;i<=n;i++){
cin>>a[i].p>>a[i].d;
}
sort(a+1,a+1+n,cmp);
int k=1;
double m_range = cm*davg;
double max_d=0;
double min_p=0;
if(a[1].d!=0){
printf("The maximum travel distance = %.2f",max_d);
return 0;
}
int can_get,nk;
double kd,kp,cheapest;
double oil=0;
while(1){
can_get=0;
kd=a[k].d;
kp=a[k].p;
nk=-1;
cheapest=999999;
if(kd+m_range>=dist){
can_get=1;
}
int cheap=0;
//遍历所能到达的全部站点
for(int i=k+1;i<=n;i++){
if(a[i].d>kd+m_range || a[i].d>=dist){
break;
}
//一旦发现有价格更便宜的就停止
if(a[i].p<kp){
nk=i;
cheap=1;
break;
}
//否则找一个价格相对便宜的
if(!can_get&&a[i].p<cheapest){
cheapest=a[i].p;
nk=i;
}
}
//cout<<nk<<" "<<a[nk].d<<endl;
if(nk==-1){//已经达到最远了
if(can_get){
if(oil<(dist-kd)/davg){
min_p += kp*((dist-kd)/davg-oil);
}
printf("%.2f",min_p);
}else{
max_d=kd+m_range;
printf("The maximum travel distance = %.2f",max_d);
}
break;
}
//只要到nk的油量加了就行
//cout<<"从"<<k<<"到"<<nk<<" 距离:"<<a[nk].d-kd<<" 当前油量:"<<oil<<endl;
if(!cheap){
//cout<<"当前为最小点加满,";
if(oil<cm){
min_p += kp*(cm-oil);
oil=cm;
}
oil-=(a[nk].d-kd)/davg;
//cout<<"用了"<<(a[nk].d-kd)/davg<<" 还剩"<<oil<<endl;
}else{
if((oil<a[nk].d-kd)/davg){
//cout<<"需加"<<(a[nk].d-kd)/davg-oil;
min_p += kp*((a[nk].d-kd)/davg-oil);
oil=(a[nk].d-kd)/davg;
}
oil-=(a[nk].d-kd)/davg;
//cout<<"用了"<<(a[nk].d-kd)/davg<<" 还剩"<<oil<<endl;
}
k=nk;
//cout<<endl;
}
return 0;
}