PAT A1033

  • 题目:
    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
参考:https://www.liuchuo.net/archives/2461

  • 题目大意
    汽车从杭州出发可以通过高速公路去任何城市,但是油箱的容量是有限的,路上有很多加油站,每个加油站的价格不同,为汽车设计一个从杭州到终点的最便宜的路线,Cmax表示油箱最大容量,D表示杭州到目的地的距离,Davg表示平均每单位的汽油可以让汽车行驶的距离,N表示汽车的站点数量,每个站点都会给出它的单位油价Pi和汽车站点和杭州的距离Di,求汽车从杭州到终点的最小花费,如果不能够到达,就输出汽车能够行驶的最大距离

  • 分析
    0.假设增加一个目的地处的加油站,距离为目的地的距离,价格为0,考虑从0距离开始能否到达最后一个加油站的问题
    1.因为先开始没有油,所以如果所有的加油站距离都没有等于0的,那么说明车哪也去不了,直接输出并return
    2.将加油站按照距离dis从小到大排序
    贪心思想:
    0.寻找比自己距离远的,到能够到达的最大距离之间的加油站,看他们的油价。如果找到了更低价格的油价,就加油到刚好能到达那个加油站的距离的油,然后去那个更低价格的加油站(有更低的我一分都不想多花在别的距离上,只加到刚好满足更低价格的加油站的距离就行,那样以后的路程我就可以以更低的价格行驶啦)
    1.如果找不到更低的,就找尽可能低的油价的加油站,在当前加油站加满油之后过去。因为想要让路程上使用的尽可能是低价的油,既然没有比当前更低价格的了,就让油箱加到最大值,这样能保证利益最大化,保证最大的距离使用的是便宜的油。

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
const int inf =  1000000000;
using namespace std;
struct station{
    double pri;
    double dis;
};
bool cmp(station a, station b){                //按照距离从小到大排序
    return a.dis < b.dis;
}
int main()
{
    double c, d, d_avg;
    int n;
    scanf("%lf%lf%lf%d", &c, &d, &d_avg, &n);
    vector<station> vec(n+1);
    for(int i = 0; i < n; i++){
        scanf("%lf%d", &vec[i].pri, &vec[i].dis);
    }
    vec[n].pri = 0;                            //将终点视为油价为0,距离为d的油站
    vec[n].dis = d;
    sort(vec.begin(), vec.end(), cmp);          //对油站按照距离排序
    if(vec[0].dis != 0){
        printf("The maximum travel distance = 0.00\n");
        return 0;
    }
    int now  = 0;
    double ans = 0, nowtank = 0, Max = c * d_avg;
    while(now < n){
        int k = -1;                        //油站最低编号
        double pricemin = inf;             //最低油价
        for(int i = now+1; i<=n && vec[i].dis- vec[now].dis <= Max;i++){
            if(vec[i].pri < pricemin){
                pricemin = vec[i].pri;
                k = i;
                if(pricemin < vec[now].pri)
                    break;
            }
        }
        if(k == -1)
            break;
        double need = (vec[k].dis - vec[now].dis)/d_avg;           //计算所需要的油量
        if(pricemin < vec[now].pri){
            if(nowtank < need){
                ans+= (need- nowtank)* vec[now].pri;
                nowtank = 0;
            } else{
                nowtank -= need;
            }
        } else {
            ans+= (c - nowtank )* vec[now].pri;
            nowtank = c - need;
        }
        now = k;
    }
    if(now  == n)
        printf("%.2f\n", ans);
    else
        printf("The maximum travel distance = %.2f\n",vec[now].dis + Max);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值