PAT (Advanced Level) Practice 1033 To Fill or Not to Fill(貪心)

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.
之後按距離升序排序
我們可以先求出從一個加油站出發所到達的最遠距離,然後從能到達的加油站裏面挑選下一個目的地。這裏分兩種情況。

  1. 能到達的加油站中有比所在加油站價格便宜的
    這種情況下,我們優先選擇距離最近的比所在加油站價格便宜的加油站
    爲什麼不選擇最小价格的加油站呢,下面一張圖解釋
    在這裏插入圖片描述
    這種情況下,我們把油加到正好到達目標加油站即可。
  2. 能到達的加油站都比起點加油站貴
    這裏我們選擇將油都加滿,然後選擇能到達加油站中價格最便宜的作爲目的地。
    我們可以將目的地也當成一個加油站,油價爲0,當我們到達這個加油站的時候就跳出循環,輸出最少花費錢數。

代碼如下

#include <bits/stdc++.h>
using namespace std;
const int maxn=505;
const int INF=0x3f3f3f3f;
int n;
double d,c,davg;
struct node
{
    double dis;
    double p;
};
node a[maxn];
int compare (node x,node y)
{
    if(x.dis!=y.dis) return x.dis<y.dis;
    return x.p<y.p;
}
int main()
{
    scanf("%lf%lf%lf%d",&c,&d,&davg,&n);
    for (int i=0;i<n;i++)
    {
        scanf("%lf%lf",&a[i].p,&a[i].dis);
    }
    a[n].dis=d;
    a[n].p=0;
    sort(a,a+n+1,compare);
    if(a[0].dis!=0)
    {
        printf("The maximum travel distance = 0.00\n");
        return 0;
    }
    int now_loc=0;
    double now_c=0;
    double now_cost=0;
    int per_maxdis=davg*c; //每次行駛的最大距離
    while(now_loc<n)
    {
        int min_pri=INF,min_loc=-1,tflag=0;
        //沒有到達的加油站了,輸出最大距離
        if(a[now_loc+1].dis>a[now_loc].dis+per_maxdis)
        {
            printf("The maximum travel distance = %.2lf\n",a[now_loc].dis+per_maxdis);
            return 0;
        }
        for (int i=now_loc+1;i<=n;i++)
        {
            if(a[i].dis>a[now_loc].dis+per_maxdis) break;
            if(a[i].p<=a[now_loc].p)
            {
                min_pri=a[i].p;
                min_loc=i;
                break;
            }
        }
        //對應着情況2
        if(min_loc==-1)
        {
            min_loc=now_loc+1;
            for (int i=now_loc+2;i<=n;i++)
            {
                if(a[i].dis>a[now_loc].dis+per_maxdis) break;
                if(a[i].p<a[min_loc].p)
                {
                    min_loc=i;
                }
            }
            now_cost+=(c-now_c)*a[now_loc].p;
            now_c =c-(a[min_loc].dis-a[now_loc].dis)/davg;
            now_loc=min_loc;
        }
        //對應着情況1
        else
        {
            now_c=(a[min_loc].dis-a[now_loc].dis)/davg-now_c;
            now_cost+=now_c*a[now_loc].p;
            now_c=0;
            now_loc=min_loc;
        }
        if(now_loc==n) printf("%.2lf\n",now_cost);
    }

    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章