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

 题解:汽车初始油量为0,首先对加油站距离远近从小到大进行排序,如果距离为0的时候没有加油站,则无法到达终点。在能开到的最大范围内寻找油价最低的加油站,仅加满到达该加油站的油。判断当前加油站油价低于可到范围内最低的油价,如果是,则加满,如果不是,则只加满足到下一个油价最低的加油站的油量。

代码:

#include<bits/stdc++.h>
using namespace std;
const int INF=1000000000;
struct station
{
    double price;
    double distance;
}s[505];

bool cmp(station a,station b)
{
    return a.distance<b.distance;  //按照距离远近排序
}

int main()
{
    double Cmax,D,Davg;
    int n;
    scanf("%lf%lf%lf%d",&Cmax,&D,&Davg,&n);
    for(int i=0;i<n;i++)
    {
        scanf("%lf%lf",&s[i].price,&s[i].distance);
    }
    s[n].price=0;
    s[n].distance=D;
    sort(s,s+n,cmp);
    if(s[0].distance!=0)  //如果初始位置为0,无法到达目的地
    {
        printf("The maximum travel distance = 0.00\n");
        return 0;
    }
    else
    {
        int now=0;
        double ans=0,nowTank=0,MAX=Cmax*Davg;
        while(now<n)
        {
            int k=-1;  //记录当前加油站
            double priceMin=INF;
            for(int i=now+1;i<=n&&s[i].distance-s[now].distance<=MAX;i++)
            {
                if(s[i].price<priceMin)
                {
                    priceMin=s[i].price;
                    k=i;    //当前可到范围内油价最低的加油站
                  if(priceMin<s[now].price)  //重要
                  {
                    break;
                  }
              
                }
            }
            if(k==-1)      //汽车无法到达终点
                break;
            double need=(s[k].distance-s[now].distance)/Davg;  //到达下一个油价最低的加油站所需要的油量
            if(priceMin<s[now].price)  //当前油价高于最低价
            {
                if(nowTank<need)  //如果需要的油量大于现在油箱内剩余的
                {
                    ans+=(need-nowTank)*s[now].price;   //在当前加油站加满油
                    nowTank=0;                          //开到加油站油箱油量刚好空了
                }
                else             //油箱内的油量大于需要的
                {
                    nowTank-=need;
                }
            }
            else  //当前油价低于最低价
            {
                ans+=(Cmax-nowTank)*s[now].price;  //在该加油站补满油
                nowTank=Cmax-need;                 //到达加油站后油箱内剩余油量
            }
            now=k;
        }
        if(now==n)
            printf("%.2f\n",ans);
        else
            printf("The maximum travel distance = %.2f\n",s[now].distance+MAX);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42671353/article/details/82184241