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

 

發佈了65 篇原創文章 · 獲贊 3 · 訪問量 9226
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章