PAT_A 1033 To Fill or Not to Fill

PAT_A 1033 To Fill or Not to Fill

分析

是一道很像动态规划的贪心问题,通过计算每一段路程的最小花费的到结果,需要判断出发点没有加油站的情况

PAT_A 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: Cmax (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; Davg (≤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

AC的代码

#include<bits/stdc++.h>

using namespace std;

bool cmp(pair<double,double> a, pair<double,double> b){
    return a.second < b.second;    
}

int main(){
    double CMAX,D,DAVG,N;
    scanf("%lf%lf%lf%lf",&CMAX,&D,&DAVG,&N);
    vector< pair<double, double> > inf(N);
    for(int i=0;i<N;i++){
        scanf("%lf %lf",&inf[i].first,&inf[i].second);
    }
    sort(inf.begin(),inf.end(),cmp);

    double totalcost = 0, distance = 0, currentstation = 0, currentgas = 0;
    if(inf[0].second>0.000001){
        //针对测试点2的特判, 即出发点没有加油站
        printf("The maximum travel distance = 0.00\n");
        return 0;
    }
    while(currentstation<N){
        int maxdistance = inf[currentstation].second + CMAX * DAVG,
        minindex = currentstation+1, f = 0;
        //找到之后最实惠的加油站
        for(int i=currentstation+1;i<N;i++){
            if(inf[i].second > maxdistance ){
                
                break;
            }
            else if(inf[minindex].first<inf[currentstation].first){
                f=1;
                break;
            }
            else if(inf[i].first < inf[minindex].first){
                minindex = i;
            }
        }
        
        if(f==0 && maxdistance > D){
            //若之后没有更实惠加油站,且可以跑完全程
            if(currentgas*DAVG < D-inf[currentstation].second)
                totalcost += ((D-inf[currentstation].second) * 1.0/ DAVG - currentgas)*inf[currentstation].first;
            printf("%.2lf\n",totalcost);
            break;
        } else if( currentstation == N-1){
            //若到了最后一个加油站
            distance = maxdistance;
            printf("The maximum travel distance = %.2lf\n",distance);
            break;
        }
        else if(f==0){
            //之后可以跑到的加油站比现在的更贵
            totalcost += (CMAX- currentgas) * 1.0 * inf[currentstation].first;
            currentgas=CMAX - (inf[minindex].second - inf[currentstation].second)*1.0/DAVG ;
            currentstation =minindex;
        }
        else {//if(inf[currentstation].first > inf[minindex].first){
            //可以跑到比当前更实惠的加油站
            distance = inf[minindex].second;
            if(currentgas*DAVG < inf[minindex].second-inf[currentstation].second){
                totalcost += ((inf[minindex].second-inf[currentstation].second)/(1.0*DAVG) - currentgas) * 1.0 * inf[currentstation].first;
                currentgas = 0;
            }else{
                currentgas -= (inf[minindex].second-inf[currentstation].second)/(1.0*DAVG);
            }
            currentstation = minindex;
        }
    }
    return 0;
}
posted @ 2022-09-04 17:33  ghosteq  阅读(21)  评论(0编辑  收藏  举报