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

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

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


【题目大意】
有了高速公路,从杭州开车到其他城市都很容易。但是因为汽车的油箱容量有限,我们在路上不得不不时地寻找加油站。不同的加油站油价可能不同。你被要求仔细地设计最便宜的路线。

【输入】
第一行:Cmax D Davg N
Cmax - 油箱最大容量
D - 杭州到目的地的距离
Davg - 每升油可以行驶的距离
N - 加油站的个数
接下去 N 行:Pi Di
Pi - 汽油单价
Di - 该加油站离杭州的距离
【输出】
最便宜的价格,保留两位小数。
最开始油量为 0,如果无法到达目的地,输出 The maximum travel distance = X
X - 可到达最远距离

【数据范围】
Cmax <=100
D <=30000
Davg <=20
N <=500
0 <= Pi
0 <= Di <= D <= 30000

【解题思路】
贪心。先读入数据,然后按照离起点(杭州)的距离从小至大排序,最后再加上终点的信息(距离 D,油价 0)。
大致思路是,记录当前所在加油站的油价,先假设加满,寻找可到达的距离内能否找到比当前加油站更便宜的油价。
1、如果可以,我们在当前加油站需要的油量足够到达更便宜那个加油站就可以了。
2、如果没有更便宜的,就在当前加油站加满,然后在可到达距离内找出相对较便宜的那个加油站开过去。
起点和终点都不用特殊处理。因为一开始可到达最远距离是 0,如果起点没有加油站,那么最开始就无法到达第一个点。终点的油价是 0,这样在行驶到终点之前的某个加油站时,会找到终点这个最便宜的加油站,然后油量加到恰好可以到达终点。这就是为什么最开始要加上终点信息的原因。
思路其实比较简单,应该都能想到。这题比较抠细节,具体的注释在代码中有写出。好像很久没写注释了。


#include<bits/stdc++.h>
using namespace std;
const int MAXN = 500+10;
const int INF = 0x3f3f3f3f;
int C, D, R, N;
struct Node {
    int dis;
    double price;
}node[MAXN];
bool cmp(Node a, Node b) {
    return a.dis < b.dis;
}
int main() {
//  freopen("in.txt", "r", stdin);
    scanf("%d%d%d%d", &C, &D, &R, &N);
    for (int i = 0; i < N; i++)
        scanf("%lf %d", &node[i].price, &node[i].dis);
    node[N].dis = D;
    node[N].price = 0;
    sort(node, node+N, cmp);

    double cost = 0;    //花费 
    double cur = 0;     //当前油量 
    double poi = 0;     //可到达的最远距离 
    for (int i = 0; i <= N; i++) {
        if (poi < node[i].dis) break;   //如果到不了当前的点,跳出循环 
        int tag = -1;
        for (int j = i+1; j <= N; j++) {    //寻找可到达范围内是否有更便宜的加油站 
            if (node[j].dis > node[i].dis + R * C) break;   //假设加满都找不到更便宜的车站,跳出 
            if (node[j].price < node[i].price) {    //如果有更便宜的,记录一下
                tag = j;
                break;
            }
        }
        if (tag == -1) {    //找不到更便宜的加油站 
            double minn = INF;
            int ttag = -1; 
            for (int j = i+1; j <= N; j++) { //找一个虽然比当前更贵,但是价格相对比较低的 
                if (node[j].dis > node[i].dis + R * C) break;
                if (node[j].price < minn) {
                    minn = node[j].price; 
                    ttag = j;
                }
            }
            if (ttag == -1) {
                poi += R * C; //可到达范围内没有任何车站,在当前加满,然后已经是最远距离了 
                break;
            } else {
                cost += (C - cur) * node[i].price; //先在当前位置加满
                cur = C - (node[ttag].dis - node[i].dis) * 1.0 / R;
                i = ttag - 1; //然后去这个相对便宜的车站
                poi = node[ttag].dis;
            }
        } else {
            cur = (node[tag].dis - node[i].dis) * 1.0 / R - cur; //油量加到可以到达这个更便宜的车站
            cost += cur * node[i].price; 
            poi = node[tag].dis;    //可到达的最远距离就是这个车站 
            i = tag - 1;
            cur = 0;
        } 
    }
    if (poi < D) printf("The maximum travel distance = %.2lf\n", poi); 
    else printf("%.2lf\n", cost);

    return 0;
}

提交时间状态分数题目编译器耗时
2018/7/19 23:53:12答案正确251033C++ (g++)7 ms

这题之前上算法分析课有写过,不过数据没有这个严格。重写一遍发现那个时候的代码大概还是有 bug 的,写得头皮发麻。建议脑子要保持清醒,写注释可以帮自己理一理思路。复杂样例的好处是样例过了就能 AC(大概

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值