【PAT甲级】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: 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 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

0. 将终点的油价设置为0，距离为D

1.如果没有加油站点在起点处，则输出0

2.每次优先找比当前油价更低的站点，如果没有则以当前油价加满汽油【贪心保证最优】

3.重复步骤2，直到最后

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <sstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define mem(a,n) memset(a,n,sizeof(a))
#define lowbit(i) ((i)&(-i))
typedef long long ll;
typedef unsigned long long ull;
const ll INF=0x3f3f3f3f;
const double eps = 1e-6;
const int N = 5e2+5;

struct Node {
double price,dis;
Node() {}
Node(double x,double y):price(x),dis(y) {}
bool operator < (const Node& m)const {
return dis-m.dis<eps;
}
} station[N];

int main() {
double Cmax,D,Davg;
int n;
scanf("%lf%lf%lf%d",&Cmax,&D,&Davg,&n);
for(int i=0; i<n; i++) {
scanf("%lf%lf",&station[i].price,&station[i].dis);
}
station[n]=Node(0.0, D);
sort(station,station+n);
int nowId=0;
if(station.dis>eps) {
//        puts("BB");
printf("The maximum travel distance = 0.00");
return 0;
}
double ans=0,nowTank=0,minPrice;
double maxDis=Cmax*Davg;
//    printf("%f %f %f\n",Cmax,Davg, maxDis);
while(nowId<n) {
int k=-1;
minPrice=INF;
for(int i=nowId+1; i<=n; i++) {
if(station[i].price<minPrice&&station[i].dis-station[nowId].dis<=maxDis) {
///找到油价更低的站点
minPrice=station[i].price;
k=i;
if(minPrice<station[nowId].price) break;
}
}
if(k==-1) break;///已经无法找到油价更低的站点
if(minPrice<station[nowId].price) { ///找到更低的油价站点
}
} else {///没有更低油价的站点,加满
ans+=(Cmax-nowTank)*station[nowId].price;
}
nowId=k;///到达第k个站点
//        printf("ans=%f\n",ans);
}
if(nowId==n) { ///到达终点站
printf("%.2f",ans);
} else {
printf("The maximum travel distance = %.2f",station[nowId].dis+maxDis);
}
return 0;
} 