codeup

问题 C: To Fill or Not to Fill
[命题人 : 外部导入]
时间限制 : 1.000 sec 内存限制 : 32 MB

题目描述
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.

输入
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.

输出
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.

样例输入 Copy
59 525 19 2
3.00 314
3.00 0
样例输出 Copy
82.89

#include <stdio.h>
#include <algorithm>

using namespace std;

struct station{
    double dis_to_HZ;
    double price;
}St[501];

bool cmp(station a, station b){
    if(a.dis_to_HZ != b.dis_to_HZ) return a.dis_to_HZ < b.dis_to_HZ;
    else return a.price < b.price;
}
int main()
{
    double c, d, d_avg;
    int n;

    while(scanf("%lf%lf%lf%d", &c, &d, &d_avg, &n) != EOF){
        for(int i = 0; i < n; i++) scanf("%lf%lf", &St[i].price, &St[i].dis_to_HZ);

        sort(St, St + n, cmp);

        St[n].dis_to_HZ = d;
        St[n].price = 0;

        double full_gas_len = c * d_avg;
        double dis = 0;
        double fee = 0;
        double gas = 0;
        int flag = 0;

        if(St[0].dis_to_HZ != 0){
            dis = 0;
            flag = 1;
        }
        else{
            for(int i = 0; i < n; i++){
                int flag_no_cheap = 1;
                double cheapst_in_range = St[i + 1].price;
                int cheapst_in_range_index = i + 1;


                if(St[i + 1].dis_to_HZ - St[i].dis_to_HZ > full_gas_len){
                    dis += full_gas_len;
                    flag = 1;
                    break;
                }

                for(int j = i + 1; j <= n && St[j].dis_to_HZ - St[i].dis_to_HZ <= full_gas_len; j++){
                    if(St[j].price < St[i].price){
//                        if(St[j].price < cheapst_in_range){
//                            cheapst_in_range = St[j].price;
//                            cheapst_in_range_index = j;
//                        }
                        dis = dis + St[j].dis_to_HZ - St[i].dis_to_HZ;
                        fee += ((St[j].dis_to_HZ - St[i].dis_to_HZ) / d_avg - gas) * St[i].price;
                        gas = 0;
                        i = j - 1;
                        flag_no_cheap = 0;
                        break;
                    }

                    if(St[j].price < cheapst_in_range){
                            cheapst_in_range = St[j].price;
                            cheapst_in_range_index = j;
                        }
                }

                if(flag_no_cheap){
                    dis = dis + St[cheapst_in_range_index].dis_to_HZ - St[i].dis_to_HZ;
                    fee += (c - gas) * St[i].price;
                    gas = c - (St[cheapst_in_range_index].dis_to_HZ - St[i].dis_to_HZ) / d_avg;
                    i = cheapst_in_range_index - 1;

                }

            }
        }


        if(flag) printf("The maximum travel distance = %.2lf\n", dis);
        else printf("%.2lf\n", fee);

    }


    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值