pat 1033 To Fill or Not to Fill

本文探讨了一种解决复杂路线规划问题的方法,通过动态规划和算法优化,实现了一个能够有效计算最短路径成本的解决方案。文章详细介绍了输入和输出规范,包括输入参数的解释、算法的主要步骤以及输出结果的格式。此外,还提供了实现该算法的C++代码,以供读者参考和学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目: 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;
}
标题基于SpringBoot+Vue的社区便民服务平台研究AI更换标题第1章引言介绍社区便民服务平台的研究背景、意义,以及基于SpringBoot+Vue技术的研究现状和创新点。1.1研究背景与意义分析社区便民服务的重要性,以及SpringBoot+Vue技术在平台建设中的优势。1.2国内外研究现状概述国内外在社区便民服务平台方面的发展现状。1.3研究方法与创新点阐述本文采用的研究方法和在SpringBoot+Vue技术应用上的创新之处。第2章相关理论介绍SpringBoot和Vue的相关理论基础,以及它们在社区便民服务平台中的应用。2.1SpringBoot技术概述解释SpringBoot的基本概念、特点及其在便民服务平台中的应用价值。2.2Vue技术概述阐述Vue的核心思想、技术特性及其在前端界面开发中的优势。2.3SpringBoot与Vue的整合应用探讨SpringBoot与Vue如何有效整合,以提升社区便民服务平台的性能。第3章平台需求分析与设计分析社区便民服务平台的需求,并基于SpringBoot+Vue技术进行平台设计。3.1需求分析明确平台需满足的功能需求和性能需求。3.2架构设计设计平台的整体架构,包括前后端分离、模块化设计等思想。3.3数据库设计根据平台需求设计合理的数据库结构,包括数据表、字段等。第4章平台实现与关键技术详细阐述基于SpringBoot+Vue的社区便民服务平台的实现过程及关键技术。4.1后端服务实现使用SpringBoot实现后端服务,包括用户管理、服务管理等核心功能。4.2前端界面实现采用Vue技术实现前端界面,提供友好的用户交互体验。4.3前后端交互技术探讨前后端数据交互的方式,如RESTful API、WebSocket等。第5章平台测试与优化对实现的社区便民服务平台进行全面测试,并针对问题进行优化。5.1测试环境与工具介绍测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值