PAT甲 1033 To Fill or Not to Fill (25)

With highways available, driving a car from Hangzhou to any other cityis easy. But since the tank capacity of a car is limited, we have tofind gas stations on the way from time to time. Different gas stationmay give different price. You are asked to carefully design the cheapestroute to go.

Input Specification:

Each input file contains one test case. For each case, the first linecontains 4 positive numbers: C~max~ (<= 100), the maximum capacity ofthe tank; D (<=30000), the distance between Hangzhou and thedestination city; D~avg~ (<=20), the average distance per unit gasthat the car can run; and N (<= 500), the total number of gasstations. Then N lines follow, each contains a pair of non-negativenumbers: P~i~, the unit gas price, and D~i~ (<=D), the distancebetween this station and Hangzhou, for i=1,...N. All the numbers in aline are separated by a space.

Output Specification:

For each test case, print the cheapest price in a line, accurate up to 2decimal places. It is assumed that the tank is empty at the beginning.If it is impossible to reach the destination, print "The maximum traveldistance = 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、有个位置比我目前位置更加便宜,就不比较其他的了,直接去那个更便宜的点。依次下去,直到结束。


注意的细节:1、为了判断结束方便,当保存所有的点的时候,把最后一个点也保存,将价格记录为0,

                    2、当我发现我能前进的最大距离仍然无法支持我到下一个点,那就跳出,输出最大距离

The maximum travel distance =  

这句话最好是复制啊,之前自己手打,不小心有个字母写错,一直显示有错!!!

                    3、当我当前位置价格最低时候,加满,但要考虑一个问题,如果马上就到终点了,那就不要加满,加到需要的就可以,不然会偏大的哦。

以下就是代码啦

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

typedef struct
{
    double pi;
    double di;
}gasstation;
bool cmp(gasstation a,gasstation b)
{
    if(a.di!=b.di)
        return a.di<b.di;
    else
        return a.pi<b.pi;
}
int main()
{
    gasstation st[550];
    int  i,fp,mynum=0,n;
    double cmax,d,davg,mytank=0;
    double price =0.0f,distant=0.00f;
    scanf("%lf%lf%lf%d",&cmax,&d,&davg,&n);
    for(i=0;i<n;i++)
    {
        scanf("%lf%lf",&st[i].pi,&st[i].di);
    }
    st[i].pi=0.00f;
    st[i].di=d;
    sort(st,st+n,cmp);
    //以上为所有排序
    if(st[0].di!=0)
    {
        printf("The maximum travel distance = %.2f",distant);
        return 0;
    }
    while(1)
    {
        fp=mynum+1;
        int k=0;
        for(i=mynum+1;i<=n;i++)
        {

            if(st[i].di<=distant+cmax*davg)
                {
                    k++;
                    if(st[i].pi<st[mynum].pi)
                    {
                        fp=i;
                        break;              //存在比我小的,直接跳出
                    }
                    if(st[i].pi<st[fp].pi)
                    {
                        fp=i;
                    }
                }
            else
                break;

        }
        if(k==0)             //zhaobudao
        {
                    distant+=cmax*davg;
                    printf("The maximum travel distance = %.2lf",distant);
                    return 0;
        }
        double need=(st[fp].di-st[mynum].di)/davg;       //我需要的汽油
        if(st[fp].pi<st[mynum].pi)       //存在更便宜的加油站
            {
                if(need<= mytank)
                    mytank-=need;
                else
                    {
                        price+=st[mynum].pi *(need-mytank);
                        mytank=0;
                    }
                distant+=(st[fp].di-st[mynum].di);
                mynum=fp;
            }
        else                        //我最便宜
        {
            if(fp!=n)
               {
                price +=st[mynum].pi*(cmax-mytank);
                distant+=(st[fp].di-st[mynum].di);
                mytank=cmax-(st[fp].di-st[mynum].di)/davg;//jiaman;
                mynum=fp;
               }
               else
               {
                   price+=st[mynum].pi*(st[fp].di-st[mynum].di)/davg;
                   mynum=n;

               }

        }


        if(mynum==n)
            break;






    }

    printf("%.2lf",price);
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_36834256/article/details/80509577