pat贪心算法实例1033 To Fill or Not to

2020-04-21  本文已影响0人  qwerdf0929

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: 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: Pi, the unit gas price, and Di(≤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


题目大意:距离为D的两地之间有n个加油站,每个加油站油价Pi不同,问是否能够到达以及能够到达算出最低的油价。规定一辆车的邮箱容量Cmax以及每升油能跑多少公里Da,默认刚开始车没油。

分析:
将各个加油站按与起始点的距离由小到大排列
1.如果最近的加油站不在起始点,则车无法启动,输入最大行驶距离为0
2.选择下一个加油站(优先级由高到低排序)
-加满油能到达并且价格比此站更低的,如果存在,在此车站是需要把油加到能够到下一站的量(因为在下一站加的油花的钱更少)
-加满能到达但是没有比此站油价更低的加油站,如果存在,将油加满(因为下一站油价更高而且满油的情况下从此站出发找不到油价更低的站了)
-加满都不能到达的,打印到最远距离
代码如下:

#include <iostream>
#include <string>
#include <algorithm>
#include <stdio.h>
using namespace std;
typedef struct{
    float price;
    float distance;
}Station;
int CM,D,DA,N;
bool compare(const Station &s1,const Station &s2){
    return s1.distance<s2.distance;
}
int main()
{
    cin>>CM>>D>>DA>>N;
    Station s[N+1];
    for(int i=0;i<N;i++){
        cin>>s[i].price>>s[i].distance;
    }
    s[N].price=0;
    s[N].distance=D;
    sort(s,s+N+1,compare);
    float allPrice=0;
    float maxDriveDistance=CM*DA;
    float nowGas=0;
    if(s[0].distance!=0){
        printf("The maximum travel distance = %.2f\n",0);
        return 0;
    }
    for(int i=0;i<N;){
        //不能到达下一个加油站
        if((s[i+1].distance-s[i].distance)>maxDriveDistance){
            printf("The maximum travel distance = %.2f\n",s[i].distance+maxDriveDistance);
            return 0;
        }
        Station next=s[i];
        int nextNumber=i;
        for(int j=i+1;j<N+1;j++){
            //找出能到达的更低的加油站
            if((s[j].distance-s[i].distance)<=maxDriveDistance&&s[j].price<next.price){
                next=s[j];
                nextNumber=j;
                break;
            }
        }
        //未找到更低的加油站
        if(nextNumber==i){
            next=s[i+1];
            nextNumber=i+1;
            for(int j=i+1;j<N+1;j++){
                //找出能到达的最低的加油站
                if((s[j].distance-s[i].distance)<=maxDriveDistance&&s[j].price<next.price){
                    next=s[j];
                    nextNumber=j;
                }
            }
            //加满
            allPrice+=(CM-nowGas)*s[i].price;
            nowGas=CM;
        }else{
            //加到能够到达下一个站的油量
            //油不够
            if(nowGas<(next.distance-s[i].distance)/DA){
                allPrice+=((next.distance-s[i].distance)/DA-nowGas)*s[i].price;
                nowGas=(next.distance-s[i].distance)/DA;
            }
        }
        nowGas-=(next.distance-s[i].distance)/DA;
        i=nextNumber;
    }
    printf("%.2f\n",allPrice);
    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读