题目要求

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

时间要求

10ms

解题过程

刚开始看到这道题,没有什么思路。看到时间限制这么小,考虑到会不会是使用动归,不过也没有想到应该怎么办。查了网上的资料后,发现是使用贪心算法进行解决。具体的思路如下:

首先要对所有的加油站按照距离升序排列,记加满之后的最大距离为max,从A加油站向后搜索。搜索的情况有以下几种:

  1. 若搜索到一个油价比A小的加油站,则在A点加油到刚好能到该加油站
  2. 若不满足1, 但搜索到终点,则直接加油到终点
  3. 若不满足1、2, 则加满油到能够行驶到所有能到达的加油站里的油价最低的加油站
  4. 若不满足1、2、3,则最大距离里没有任何加油站,则最大行驶距离为A+max

需要注意的地方有两点:

  1. 因为情况3向后继续行驶时,油箱会有剩余油,所以需要记录油箱剩余的油量,而且可能为小数。
  2. 有可能没有距离为0的加油站,此时无法行驶。

代码如下:

#include <cstdio>
#include <cstdlib>
#include <algorithm>

using namespace std;

typedef struct Station
{
    double price;
    int d;
}Station;

Station s[550];

bool cmp(const Station &a, const Station &b) {
    return a.d < b.d;
}

int main() {
    double c;
    int distance, d, n;
    scanf("%lf%d%d%d", &c, &distance, &d, &n);
    for (int i = 0; i < n; i++)    {
        scanf("%lf%d", &s[i].price, &s[i].d);
    }
    sort(s, s + n, cmp);
    int max = c * d;
    if (s[0].d != 0) {
        printf("The maximum travel distance = 0.00\n");
        system("pause");
        return 0;
    }
    double left = 0, fee = 0, maxDistance = -1;
    int cur = 0;
    while (cur >= 0) {
        int lowerStation = -1;
        for (int i = cur + 1; i < n && s[i].d <= s[cur].d + max; i++) {
            if (s[cur].price > s[i].price) {
                lowerStation = i;
                break;
            }
        }
        if (lowerStation != -1) {
            fee += ((s[lowerStation].d - s[cur].d) / (0.0 + d) - left) * s[cur].price;
            left = 0;
            cur = lowerStation;
            continue;
        }
        else {
            if (s[cur].d + max >= distance) {
                fee += ((distance - s[cur].d) / (0.0 + d) - left) * s[cur].price;
                left = 0;
                cur = -1;
            }
            else {
                int minStation = -1;
                double minPrice = 100000000;
                for (int i = cur + 1; i < n && s[i].d <= s[cur].d + max; i++) {
                    if (s[i].price < minPrice) {
                        minStation = i;
                        minPrice = s[i].price;
                    }
                }
                if (minStation != -1) {
                    fee += (c - left) * s[cur].price;
                    left = c - (s[minStation].d - s[cur].d) / (0.0 + d);
                    cur = minStation;
                }
                else {
                    maxDistance = s[cur].d + max;
                    cur = -1;
                }
            }
        }
    }
    if (maxDistance > 0) {
        printf("The maximum travel distance = %.2lf\n", maxDistance);
    }
    else {
        printf("%.2lf\n", fee);
    }
    system("pause");
    return 0;
}