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: 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

一道贪心算法的题目,贪心算法的思路就是贪心算法不从整体最优上加以考虑,所做出的仅是在某种意义上的局部最优选择。你只要考虑你当前最省钱的操作是什么,且不能浪费一分钱。
给vector一个初始大小n+1,可以直接通过下标赋值,将第一个赋值为终点(把终点看成一个加油站,看最后能不能到终点)。对所有station通过距离排序,如果第一个的dis不为0,证明开始的地方不能加油,那么直接输出0.00。
下面的操作基本全是抄的,虽然懂大概意思,但是完全写不出来。

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int inf = 1000000000;
struct station{
    double price, dis;
};
bool cmp(station a, station b){
    return a.dis<b.dis;
}
int main(){
    double capcity, destination, perGasRun;//和结果有关系的都要设为double
    int n;
    double price,dis;
    scanf("%lf %lf %lf %d",&capcity,&destination,&perGasRun,&n);
    vector<station> sta(n+1);//给vector一个初始大小,就可以通过下标赋值,而不是要push_back
    sta[0] = {0.0,destination};
    for(int i = 1; i <= n; i++){
       scanf("%lf %lf",&price,&dis);
        sta[i] = station{price,dis};
    }
    sort(sta.begin(), sta.end(),cmp);
    if(sta[0].dis != 0){
        printf("The maximum travel distance = 0.00");
        return 0;
    }
    double nowdis = 0.0, nowprice = sta[0].price, totalprice = 0.0;
    double runmax = capcity * perGasRun, leftdis = 0.0;//leftdis是到达i点后还能跑多少距离
    while(nowdis < destination){
        double dismax = runmax + nowdis;
        double minprice = inf, mindis = 0;//价格最小的那个站点的距离
        int flag = 0;
        for(int i = 1; i <= n && sta[i].dis <= dismax; i++){
            if(sta[i].dis <= nowdis) continue;
            if(sta[i].price <= nowprice){
                totalprice += (sta[i].dis - nowdis - leftdis) * nowprice / perGasRun;
                leftdis = 0.0;
                nowdis = sta[i].dis;
                flag = 1;
                nowprice = sta[i].price;
                break;//这里要break,因为上面的dismax要重新赋值
            }
            if(sta[i].price < minprice){
                minprice = sta[i].price;
                mindis = sta[i].dis;
                flag = 2;
            }
        }
        //如果找不到更低的,就找尽可能低的加油站
        //在当前加油站加满油过去
        if(flag == 2){
            totalprice += (capcity - (leftdis/perGasRun))*nowprice;
            leftdis = dismax - mindis;
            nowdis = mindis;
            nowprice = minprice;
        }
        if(flag == 0){//到不了下一个点or终点
            nowdis += runmax;
            printf("The maximum travel distance = %.2f",nowdis);
            return 0;
        }
    }
    printf("%.2f",totalprice);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值