【PAT 1033】【codeup 2031】To Fill or Not to Fill

本文探讨了在有限油箱容量下,从杭州出发至任意城市的最经济行驶路线设计问题。通过输入包括最大油箱容量、两地间距离、平均耗油率及总加油站数量等信息,目标是计算出最便宜的行驶费用,或在无法到达目的地时提供最远可行驶距离。文中详细阐述了解决方案,包括判断是否可达终点、遍历加油站寻找最优油站加油策略,以及计算总费用的过程。

摘要生成于 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.

输入

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.

样例输入

59 525 19 2
3.00 314
3.00 0

样例输出

82.89

提示

该题目所要解决的问题是:给定若干加油站信息,问能否驾驶汽车行驶一定的距离。如果能够行驶完全程,则计算最小花费。若不能行驶完全程,则最远能够行驶多长距离。

拿到这一题,首先判断汽车是否能够行驶到终点。什么情况下汽车无法行驶到终点呢?两种情况:起点根本就没有加油站,汽车无法启动;或者中途两个加油站之间的距离大于加满油后汽车能够行驶的最大距离。前者汽车行驶的最大距离为0.00,而后者最大距离为当前加油站的距离加上在这个加油站加满油后能够行驶的最大距离。在这里,需要将加油站按到杭州的距离从小到大排序。

接下来在能够行驶到终点的情况下计算最小花费。我们首先从路程来考虑,如果在路上,我们能够使用最便宜的汽油,当然就在那个加油站加油了。所以从起点开始遍历每个加油站。假设遍历到了第i个加油站,我们现在来判断在加油站i应该加多少油。设当前汽车离杭州的距离为curLen,当前加油站离杭州的距离为nodes[i].dis,加满油以后汽车能够行驶的最大距离为(dis=cmax*len)。这样就有node[i].dis <= curLen <= nodes[i].dis+dis,否则的话第i个加油站的油是不起作用的。于是在第i个加油站的作用范围内寻找有没有更为便宜的加油站,如果有,则下次使用这个加油站的油(j),这次汽车应该行驶到这个加油站,即touch=nodes[j].dis。如果没有找到更为便宜的加油站则可以在第i个加油站加满油,即touch=nodes[i].dis+dis。然后判断下次应该行驶到的距离与当前距离的关系,如果下次应当行驶到的距离大于当前距离,则汽车行驶,否则不动(也就是说上个加油站的油更便宜,后一个加油站也便宜,根本就不需要在当前加油站加油)。

===========================================================================================

我觉得我已经把题目吃的很透了,但却还是过不了,(网上给的样例我全都过了),显示答案错误45%,我的思路是先排除无法到达终点的情况,再去探究可以到终点的情况。接着就是遍历加油站,先考虑是否有车辆范围内更低的油价,再考虑终点是否在车辆范围内,最后考虑车中剩余的油量,下面是代码

#include<stdio.h>
#include<algorithm>
using namespace std;

typedef  struct{
	double unit_pric;
	double gas_dist;
}Gaes;

int cmp(Gaes a,Gaes b)
{
	return a.gas_dist<b.gas_dist;
}

int main()
{
	Gaes station[505];
	double cap=0,dist=0,avg=0;
	int n=0;
	scanf("%lf%lf%lf%d",&cap,&dist,&avg,&n);
	double max=cap*avg;
	//printf("%lf %lf %lf %d\n",max,cap,avg,n);


	int i=0;
	for(;i<n;i++)
	{
		scanf("%lf%lf",&station[i].unit_pric,&station[i].gas_dist);
	}

	sort(station,station+n,cmp);

	int flag=1;
	if(station[0].gas_dist!=0)//如果起点没加油站则到不了终点
	{
		flag=0;
		printf("The maximum travel distance = %.2lf\n",0);
		return 0;
	}

	

	for(i=1;i<n;i++)//有相临两点之间的距离超过车的行程则到不了终点
	{
		if(station[i].gas_dist-station[i-1].gas_dist>max)
		{
			flag=0;
			break;
		}
	}
	if(i=n)
	{
		if(dist-station[i-1].gas_dist>max)
			flag=0;
	}
	if(!flag)
	{
		printf("The maximum travel distance = %.2lf\n",station[i-1].gas_dist+max);
		return 0;
	}
//	printf("可达到终点\n");
	//下面是不会中途没油的情况
	//double curLen=0;//当前离杭州的距离
	//double curgas=0;//当前汽车的汽油量
	double restlen=0;//汽车的当前的汽油可以行进的距离
	double touch=0;//汽车可以接触的范围,离当前的距离算
	double totalM=0;//花费的钱
	for(i=0;i<n;i++)
	{
		//if(touch)
		//	touch-=station[i].gas_dist-station[i-1].gas_dist;
		//if(touch)
		//	continue;
		int flag=0;
		touch=0;
		
		//double min=1000005;

		if(i)
			restlen-=station[i].gas_dist-station[i-1].gas_dist;//剩余距离减去行驶的距离
		//当从加油站可以触及到目的地时
		/*if(dist-station[j].gas_dist<=max)
		{
			if(restlen>dist-station[j].gas_dist)
				break;
			flag=1;
		}*/
		//printf("寻找低价\n");
		int j=i;
		for(;j<n&&station[j].gas_dist-station[i].gas_dist<=max;j++)
		{
			
			if(station[j].unit_pric<station[i].unit_pric)
				//如果在汽车可触及的范围内有比当前的油价低时
			{
				touch=station[j].gas_dist-station[i].gas_dist;
				flag=1;//有找到才退出
				break;//一但有就可以退出循环
			}
			else 
			{
				//if(station[j].unit_pric<min)
				//	min=station[j].unit_pric;
				
				touch=max;//如没有比当前小的就加满油
				//printf("touch=%lf max=%lf\n",touch,max);
			}
		}
		if(!flag&&dist-station[i].gas_dist<=max)//当没有比当前油站油价小时,且要到目的地了
		{	
			
			touch=dist-station[i].gas_dist;
			//printf("可直接到目的地 touch=%lf restlen=%lf\n",touch,restlen);
			//printf("计算价钱\n");
			totalM+=(touch-restlen)/avg*station[i].unit_pric;
			break;
		}
		
		if(touch<=restlen)//如果预计到的距离小于剩余距离
		{
			//printf("油还够 touch=%lf\n",touch);
			continue;
		}
		else//否则就要加油
		{
			//printf("计算价钱\n");
			totalM+=(touch-restlen)/avg*station[i].unit_pric;
			restlen=touch;
		}
	}
	printf("%.2lf\n",totalM);
}

不知道有没有大神帮助看下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值