pat 1033 To Fill or Not to Fill

题目: http://pat.zju.edu.cn/contests/pat-a-practise/1033

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
思路:最初以为只是一个简单的DP,结果发现没那么简单,自己纠结了好几个小时才磕磕碰碰的写出来了,代码比较繁琐。

代码

#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>

using namespace std;

struct station
{
        int distance_to_hangzhou;
        double price;
        station(int d, double p): distance_to_hangzhou(d), price(p){}
};

bool cmpl_by_dis(const station &a, const station &b)
{
        return a.distance_to_hangzhou < b.distance_to_hangzhou;
}

double solution_fill_not_fill(const vector<station> &vs, int station_number, double &cost, int distance_to_dist, double tank_capacity, double distance_per_unit)
{
        double max_dist_between_stations = tank_capacity * distance_per_unit;

        vector<int> next_min_price(station_number + 1, -1); // 当前station之后再max_dist_between_stations 范围内的最小比当前值小的节点所在下标, 如果没有则为-1,即范围的所有station price均大于当前节点

        if (vs[0].distance_to_hangzhou != 0) // 无法离开杭州
                return 0;

        // 求所有范围的下一个小的price O(n*n)
        for (int i = 0; i < station_number; ++ i)
        {
                int j;
                bool has_smaller = false;
                for (j = i + 1; j < station_number && vs[j].distance_to_hangzhou - vs[i].distance_to_hangzhou <= max_dist_between_stations; ++ j)
                {
                        if (vs[j].price < vs[i].price)
                        {
                                has_smaller = true;
                                break;
                        }
                }

                if (has_smaller) // 从i到j-1的所有节点的next_min_price均为j
                {
                        for (; i < j; ++ i)
                        {
                                next_min_price[i] = j;
                        }
                        -- i;
                }
        }

        // 显示每个站之后要加油的站
        /*
        for (int i = 0; i <= station_number; ++ i)
        {
                cout << "index = " << i << ", " <<  std::fixed << setprecision(2) << vs[i].price << " " << vs[i].distance_to_hangzhou << " , next smaller = " << next_min_price[i] << ", price [ current, next smaller ] = [ " << vs[i].price << ", " << (next_min_price[i] == -1? -1 : vs[next_min_price[i]].price ) << "]" << endl;
        }
        */

        // 求最小的cost
        cost = 0;
        int current_index = 0;
        double remain_gas = 0; // 当前剩余的汽油

        // 确定每个站要加的油量
        for (; current_index < station_number;)
        {
                //cout << "current_index = " << current_index << ", cost = " << cost << endl;
                if (next_min_price[current_index] == -1) // 当前范围内没有比当前值小的加油站,则以当前价格加满
                {
                        // 如果不到终点
                        if (distance_to_dist - vs[current_index].distance_to_hangzhou > max_dist_between_stations)
                        {
                                cost += (tank_capacity - remain_gas) * vs[current_index].price; // 加满

                                if (vs[current_index+1].distance_to_hangzhou - vs[current_index].distance_to_hangzhou > max_dist_between_stations) // 无法到达终点
                                {
                                        return vs[current_index].distance_to_hangzhou + max_dist_between_stations;
                                }

                                remain_gas = tank_capacity - (vs[current_index+1].distance_to_hangzhou - vs[current_index].distance_to_hangzhou) / distance_per_unit; //  到达下一站之后剩余汽油
                                ++ current_index;
                        }
                        else // 如果到终点,只需加满够到终点即可
                        {
                                double need_more_gas = (distance_to_dist - vs[current_index].distance_to_hangzhou) / distance_per_unit;
                                cost += (need_more_gas -remain_gas) * vs[current_index].price;
                                return distance_to_dist; // 到达终点
                        }
                }
                else // 当前范围内有比当前值小的加油站,只需要加到够到下一站的油即可
                {
                        double need_more_gas = (vs[next_min_price[current_index]].distance_to_hangzhou - vs[current_index].distance_to_hangzhou) / distance_per_unit;
                        if (need_more_gas < remain_gas)         // 前面加的油够当前使用,在当前站不加油
                        {
                                remain_gas -= need_more_gas;    // 到达下一个要加油的站所剩的油量
                        }
                        else
                        {
                                cost += (need_more_gas -remain_gas) * vs[current_index].price;
                                remain_gas = 0;                                 // 到达下一个要加油点之后剩余0
                        }

                        // 确定下一个站
                        current_index = next_min_price[current_index];
                }
        }

        return distance_to_dist;
}

int main()
{
        double tank_capacity, distance_per_unit;
        int distance_to_dist, station_number;
        vector<station> vs;

        // read in the data
        cin >> tank_capacity >> distance_to_dist >> distance_per_unit >> station_number;

        vs.reserve(station_number+1);

        for (int i = 0; i < station_number; ++ i)
        {
                double p;
                int d;

                cin >> p >> d;
                vs.push_back(station(d, p));
        }
        vs.push_back(station(distance_to_dist, 0)); // 最后一个站,即为终点

        // sort by distance to hangzhou
        sort(vs.begin(), vs.end(), cmpl_by_dis);

        // get the min cost or the max distance can reached
        double cost = 0;
        double max_dist = solution_fill_not_fill(vs, station_number, cost, distance_to_dist, tank_capacity, distance_per_unit);

        if (max_dist < distance_to_dist)
        {
                cout << "The maximum travel distance = " << std::fixed << setprecision(2) << max_dist << endl;
        }
        else // min cost
        {
                cout << std::fixed << setprecision(2) << cost << endl;
        }

        return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值