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

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