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 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

思路:从出发站点能到达的范围内查找去哪个站点,有以下几种情况:

1、能找到比当前站点价格便宜的加油站,加的油量只需能到达那个站点

2、没有比当前站点更便宜的站点,在能到达的范围内的站点中选取价格最便宜的站点K,在当前站点灌满油箱然后去站点K

3、在能到达的范围内一个站点都没有,输出最大行驶距离

注意点:有些变量需要声明为double类型,还有若起始点没有加油站输出0.00

AC参考代码:

#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
using namespace std;
struct Node{
    double price;
    double location;
};
int compare(Node a,Node b){
    return a.location<b.location;
}
vector<Node> station;
int C,D,A,N;
double minTotalprice = 0;
int main()
{
    cin>>C>>D>>A>>N;
    double totalLength = D; //标记总的路程长度
    station.resize(N);
    for(int i=0;i<N;i++){
        cin>>station[i].price>>station[i].location;
    }
    sort(station.begin(),station.end(),compare);
    if(station[0].location!=0){     //特殊点,起点站没有加油站,那就不能前进
        cout<<"The maximum travel distance = "<<fixed<<setprecision(2)<<0.0;
        return 0;
    }
    int loc = 0;    //标记当前在第几个加油站
    double currentOil = 0;      //标记当前油箱中剩下多少油
    while(D>0){     //表示离终点的距离
        double maxLength = station[loc].location+C*A;       //当前站点能跑的最大范围
        if(station[loc+1].location>maxLength || (loc==N-1 && maxLength<totalLength)){       //如果未到达终点就不能前进和到达终前一站点不能前进的2种情况
            cout<<"The maximum travel distance = "<<fixed<<setprecision(2)<<maxLength;
            return 0;
        }
        double minPrice = station[loc].price;
        int minLoc = loc;
        int findIt = 0;     //标记在能到达的范围内是否找到比当前加油站更便宜的加油站
        for(int i=loc+1;station[i].location<=maxLength && i<N;i++){ //找到范围内的第一个小于等于开始位的值
            if(station[i].price<minPrice){
                minPrice = station[i].price;
                minLoc = i;
                findIt = 1;
                break;
            }
        }
        if(D<A*C && (station[minLoc].location>=totalLength || findIt==0)){  //从这个加油站出发能到达终点且该加油站到终点之间没有更便宜的加油站
            minTotalprice += ((double)((totalLength-station[loc].location)/A)-currentOil)*station[loc].price;
            cout<<fixed<<setprecision(2)<<minTotalprice;
            return 0;
        }
        if(findIt==1){
            minTotalprice += (double)((station[minLoc].location-station[loc].location-currentOil*A)/A)*station[loc].price;
            D = totalLength-station[minLoc].location;
            loc = minLoc;
            currentOil = 0;
        }else{      //范围内的都比始发站大,则找范围内的最小站,从始发站灌满油开到该站
            double tempPrice = station[loc+1].price,tempLoc = loc+1;
            for(int i=loc+1;station[i].location<=maxLength && i<N;i++){
                if(station[i].price<tempPrice){
                    tempPrice = station[i].price;
                    tempLoc = i;
                }
            }
            D =totalLength - station[tempLoc].location;
            minTotalprice += station[loc].price*(C-currentOil);
            currentOil = C-(double)((station[tempLoc].location-station[loc].location)/A);
            loc = tempLoc;
        }
    }
    return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值