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
题目大意
开车从杭州出发要到达目的地,由于油箱容量有限,必须在路上加油。但是途中的加油站提供不同的价格,题目给出一系列数据,表示沿途加油站的油价与其位置,题目要求出最便宜的加油方案所需的价格。如果不能到达终点,那么需要求出最远可达的距离。
思路
首先在目的地设置一个价格为0的虚拟加油站,然后将加油站按距离从小到大排序。如果第一个加油站距离不为0,则车的最大可达距离为0.0。否则在满油状态可达的加油站里按距离遍历,如下考虑
- 发现一个比当前加油站价格更低的加油站,加能到达该加油站的油量,前往该加油站。
- 否则加满在当前加油站加满油,然后前往可达范围内价格最低的加油站。
以上是基于贪心的思想考虑的。
代码
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define MAX_N 500
#define INFINTITY 999999
struct Station{
double D;
double price;
}stations[500