PAT A 1033. To Fill or Not to Fill (25)

本文探讨了从杭州出发至任意城市驾驶汽车时,如何通过最优路径规划和合理选择油站,以达到最低成本的目标。具体而言,文章详细介绍了输入参数包括汽车最大油箱容量、行驶距离、平均油耗和总油站数量等,以及如何根据这些信息来设计最经济的行程路线。此外,文章还提供了一个贪心算法的实现,通过分类讨论和循环迭代的方式,确保在有限的油箱容量下,能够以最低价格到达目的地。该算法特别关注了在不同油站之间的选择策略,以及如何在满油状态下,通过选择性价比最高的油站进行加油,从而最大化节约成本。

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

题目

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

 

贪心,循环情况分类:

1、如果加满油后也无法到达最近的站,则结束。

2、如果有可以到达的站,分为2种情况:

2.1、如果可以到达的站中有比当前站便宜的,去第一个比当前站便宜的站,并在此站加刚好可以到那个站的油(因为那站的油比之前的都便宜,可以减小后面那段路的代价)。刷新位置到那个站,清空油箱,刷新价格,重新循环

2.2、如果没有,去其中最便宜的一个站,并在当前站加满油(因为这是这个满油箱路程中最便宜的油)。刷新位置到那个站,刷新油箱,刷新价格,重新循环。

 

代码:

#include <iostream>
#include <algorithm>
using namespace std;

struct way	//站的信息
{
	double dis;	//距离
	double price;	//价格
};

bool Cm_dis(const way &s1,const way &s2);	//排序站

int main()
{
	double cmax,d,davg;	//基本信息
	int n;
	cin>>cmax>>d>>davg>>n;
	
	double tank=0.0;	//油箱油量,初始化为0
	double money=0.0;	//花费,初始化为0
	way station[501];	//站
	int pos=0;	//当前位置,初始化为0(起点)
	const double dmax=cmax*davg;	//满油车程
	
	int i;

	for(i=0;i<n;i++)	//输入信息(这里需要保证没有站在终点及终点以后)
		cin>>station[i].price>>station[i].dis;
	station[n].dis=d;		//!!!在最后增加一个虚拟的终点站
	station[n].price=0.00;	//!!!并把油价设为0,保证使得可以在之后的判断中到达终点站
	sort(station,station+i,Cm_dis);	//按距离排序

	int max_s;	//加满油能到达的最远站
	int cheap_s;
	//!!!加满油能到达站中最便宜的加油点(如果这个站比当前的贵),第一个比当前站便宜的站(如果存在)
	cout<<fixed;
	cout.precision(2);

	if(station[0].dis!=0)	//!!!油箱为空,如果起始站没有油加结束
		cout<<"The maximum travel distance = 0.00";
	else
	{
		while(pos<n)	//没有到达终点
		{
			max_s=pos;
			cheap_s=pos+1;
			while(max_s<n&&station[max_s+1].dis-station[pos].dis<=dmax)	//没有到达终点,且可以到达下一站,刷新可以到达的最远站
			{
				max_s++;
				if(station[cheap_s].price>station[pos].price	//最便宜的可达站价格贵于当前站
					&&station[max_s].price<station[cheap_s].price)	//且下一站的价格比当前的便宜
					cheap_s=max_s;
			}

			if(max_s==pos)	//不能到达下一站
			{
				cout<<"The maximum travel distance = "<<station[pos].dis+dmax;
				return 0;
			}
			else if(station[cheap_s].price<station[pos].price)	//有比当前站便宜的可达站,加刚好的油,去那里
			{
				money+=((station[cheap_s].dis-station[pos].dis)/davg-tank)*station[pos].price;
				tank=0;
				pos=cheap_s;
			}
			else	//没有,加满油,去可达站中最便宜的那个站
			{
				money+=(cmax-tank)*station[pos].price;
				tank=cmax-(station[cheap_s].dis-station[pos].dis)/davg;
				pos=cheap_s;
			}
		}
		cout<<money;
	}

	return 0;
}

bool Cm_dis(const way &s1,const way &s2)
{
	return s1.dis<s2.dis;
}


 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值