Codeup2031 To Fill or Not to Fill 汽车加油问题(贪心算法)

汽车加油问题

  在学习贪心算法的时候遇到了这道题,看了Codeup上面这道题的提示,但还是不知道该怎么写,于是求助了网上这篇博客,

https://blog.csdn.net/wzy_1988/article/details/8607208

  因为这道题感觉还是有点复杂的,所以写下这篇博客供大家参考一下!

问题描述

  • 题目描述

  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.

  • 输入

  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.

  • 输出

  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.

  • 样例输入

59 525 19 2
3.00 314
3.00 0

  • 样例输出

82.89

  • 提示

  该题目所要解决的问题是:给定若干加油站信息,问能否驾驶汽车行驶一定的距离。如果能够行驶完全程,则计算最小花费。若不能行驶完全程,则最远能够行驶多长距离。

  拿到这一题,首先判断汽车是否能够行驶到终点。什么情况下汽车无法行驶到终点呢?两种情况:起点根本就没有加油站,汽车无法启动;或者中途两个加油站之间的距离大于加满油后汽车能够行驶的最大距离。前者汽车行驶的最大距离为0.00,而后者最大距离为当前加油站的距离加上在这个加油站加满油后能够行驶的最大距离。在这里,需要将加油站按到杭州的距离从小到大排序

  接下来在能够行驶到终点的情况下计算最小花费。我们首先从路程来考虑,如果在路上,我们能够使用最便宜的汽油,当然就在那个加油站加油了。所以从起点开始遍历每个加油站。假设遍历到了第i个加油站,我们现在来判断在加油站i应该加多少油。设当前汽车离杭州的距离为curLen,当前加油站离杭州的距离为nodes[i].dis,加满油以后汽车能够行驶的最大距离为(dis=cmax*len)。这样就有node[i].dis <= curLen <= nodes[i].dis+dis,否则的话第i个加油站的油是不起作用的。于是在第i个加油站的作用范围内寻找有没有更为便宜的加油站,如果有,则下次使用这个加油站的油(j),这次汽车应该行驶到这个加油站,即touch=nodes[j].dis。如果没有找到更为便宜的加油站则可以在第i个加油站加满油,即touch=nodes[i].dis+dis。然后判断下次应该行驶到的距离与当前距离的关系,如果下次应当行驶到的距离大于当前距离,则汽车行驶,否则不动(也就是说上个加油站的油更便宜,后一个加油站也便宜,根本就不需要在当前加油站加油)。

思路分析

  根据提示,我们知道,汽车无法到达的情况是很好判断的,关键在于汽车可以到达的情况下计算花费:
  首先有N个加油站,所以有N段路(两个相邻的站之间为1段路),那么就需要有N+1个点(最后一个点是目的地),在定义结构体nodes的时候就需要注意nodes[N]指的就是终点城市。按照加油站到杭州的距离进行排序。
  对于第i(0<=i<=N)个站,我们要判断下一步的操作(在此需要知道车内剩余油量):
情况1、若在车内剩余油量的行驶范围内可以找到更便宜的加油站A,然后就去A并更新i和剩余油量;

情况2、若在油量范围内找不到比当前加油站更便宜的加油站,则扩大范围,找到在满油范围最近的那个比当前油价便宜的加油站B,此时便需要在当前加油站加油至刚好行驶到B,然后更新i和剩余油量。在这里需要注意的是为什么不找到最便宜的那个,而是最近的那个,假设最便宜那个为C,C要比B更远,所以直接去C的话就要用i的价格走C-i的距离,而先到B的话就只以i的价格走B-i的距离,而C-B的距离是以更低的B的价格走的,这就更便宜一些;

情况3、若在满油范围内找不到更便宜的加油站,那就在i加满油,然后跑到能到达的站里最便宜的那个站D,接着更新i和剩余油量。这里要注意为什么要加满油,因为没有比当前站更便宜的油了,而要保证油箱里的油是最便宜的,所以要在此时加满油,在到达下一个相对较便宜的站的时候,油箱剩余的油就是最便宜的,那它就应该越多越好。

  从i=0,剩余油量为0开始判断这3种情况,并更新i和剩余油量,直到到达终点

代码主体

  为此代码中应该有除了题目给出的几个变量外,还需要有结构体nodes用来表示加油站的距离和油价,这里要注意一下,油价和距离最好都设为浮点数,这样便于后面的除法,因为收费可能是小数。
  其中可以先判断起点是否有加油站,来确定第一种不能到达的情况,若是有,则再判断第二种不能到达的情况,之后再是正常情况,完全按照上面的思路分析就行,这个题在OJ上也没有什么坑点。

AC代码

  代码如下,重要的地方已经有注释,应该能看得懂。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>

#define Expensive 110

using namespace std;

struct Nodes {
    double dis;
    double price;
}nodes[510];
//按照距离从小到大排序
bool cmp(Nodes a,Nodes b){
    return a.dis<b.dis;
}

int main(){
    int Davg,N;
    double D,Cmax;//Cmax油箱最大容量,D行驶路程,Davg每单位油行驶距离,N加油站数目
    while(scanf("%lf%lf%d%d",&Cmax,&D,&Davg,&N)!=EOF){
        double maxD=Cmax*Davg;//加满油行驶距离
        int index,flag;//当前最便宜的加油站,flag用来判断是否能到达终点
        double min_price;//最低油价
        double sum=0.00,remind_gas=0.00;//总费用,剩余油量

        for(int i=0;i<N;i++){
            scanf("%lf %lf",&nodes[i].price,&nodes[i].dis);
        }
        nodes[N].dis=D;
        nodes[N].price=0;
        sort(nodes,nodes+N,cmp);
        //若起点没有加油站
        if(nodes[0].dis!=0) {
            printf("The maximum travel distance = 0.00\n");
            continue;
        }else{
            flag=1;
            sum=0.00;
            remind_gas=0.00;
            for(int i=0;i<N;){
                //中途两站距离大于满油行驶距离
                if(nodes[i+1].dis-nodes[i].dis>maxD) {
                    flag=0;
                    printf("The maximum travel distance = %.2f\n",nodes[i].dis+maxD);
                    break;
                }else{
                    //正常情况
                    index=i;
                    min_price=nodes[i].price;
                    //找出当前油箱的油能到达的所有加油站里,油最便宜的那个
                    for(int j=i+1;nodes[j].dis-nodes[i].dis <= remind_gas*Davg&&j<=N;j++){
                        if(nodes[j].price<min_price){
                            index=j;
                            min_price=nodes[j].price;
                        }
                    }
                    if(index != i){//如果能找到更便宜的
                        remind_gas -= (nodes[index].dis-nodes[i].dis)/Davg;
                        i = index;
                        continue;
                    }
                    //若找不到,则加油找到最近的一个能加油的最便宜的站
                    index =i;
                    for(int j=i+1;nodes[j].dis-nodes[i].dis <= maxD&&j<=N;j++){
                        if(nodes[j].price<min_price){
                            index=j;
                            break;
                        }
                    }
                    if(index != i){
                        sum += ((nodes[index].dis-nodes[i].dis)/Davg - remind_gas)*nodes[i].price;
                        remind_gas=0;
                        i=index;
                        continue;
                    }
                    //或行驶范围内找不到比当前站更便宜的站,则在当前站加满油,
                    //然后跑到能跑到的所有的站里最便宜的那个
                    index=i;
                    min_price=Expensive;
                    for(int j=i+1;nodes[j].dis-nodes[i].dis <= maxD&&j<=N;j++){
                        if(nodes[j].price<min_price){
                            index=j;
                            min_price=nodes[j].price;
                        }
                    }
                    sum += (Cmax-remind_gas)*nodes[i].price;
                    remind_gas=Cmax-(nodes[index].dis-nodes[i].dis)/Davg;
                    i=index;
                }
            }

        }
        if(flag) printf("%.2f\n",sum);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值