PAT A1033:To Fill or Not to Fill之贪心求解

题目描述

1033 To Fill or Not to Fill (25分)
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:
C ​ m a x ( ≤ 100 ) C​_{max} (≤ 100) Cmax(100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; D ​ a v g ( ≤ 20 ) D_{​avg}(≤20) 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: P ​ i P_​i Pi, the unit gas price, and D ​ i ​ ( ≤ D ) D_​i​ (≤D) 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

求解思路

使用贪心策略,每次选取可达里程范围内最小的油站。

  • 查找当前油站里程范围内的所有油站,找到比当前油站油价便宜的油站next即退出(即下一个油站是当前油站加满油可以到达的所有车站里油价最低的)
  • 如果当前油站的油价比next油价要低,则加满,反之,只需要加到刚好可以到达下一油站next的油量即可
  • 如果第一个油站不在源点,或者当前车站加满油也到不了下一车站则结束算法

代码(AC)

#include<stdio.h>
#include<algorithm>
using namespace std;
struct Station{
	double price;
	double distance;
};
bool cmp(Station a,Station b)
{
	return a.distance<b.distance;
}
int main()
{
	double cmax,d,d_avg;
	int n;
	scanf("%lf %lf %lf %d",&cmax,&d,&d_avg,&n);
	Station s[n+1];
	for(int i=0;i<n;i++)
		scanf("%lf %lf",&s[i].price,&s[i].distance);
	sort(s,s+n,cmp);
	s[n].distance=d;
	s[n].price=0.0;
	if(s[0].distance!=0)	printf("The maximum travel distance = 0.00");	//出发点没有加油站,无法出发 
	else{
		double ans=0;
		double c=0;	//当前油箱容量 
		int now=0;
		while(now<n)
		{
			int next=-1;
			double p=100000000000;
			for(int i=now+1;i<=n&&(s[i].distance-s[now].distance)<=cmax*d_avg;i++)	// 在里程范围内寻找最便宜的加油站
			{
				if(s[i].price<p)	
				{
					p=s[i].price;
					next=i;
					if(p<s[now].price)	break;// 找到比当前加油站更便宜的加油站,退出循环,前往该加油站 
				}
			}
			if(next==-1)	break;//没有符合条件的加油站,退出循环 
			if(p<s[now].price)	//如果 有比当前加油站更便宜的加油站
			{
				if(c<(s[next].distance-s[now].distance)*1.0/d_avg)	//如果油箱的油不够,刚好加到能够达到 
				{
					ans+=((s[next].distance-s[now].distance)*1.0/d_avg-c)*s[now].price;
					c=0;
				}
				else c-= (s[next].distance-s[now].distance)*1.0/d_avg;
			} 
			else	//如果没有则加满
			{
				ans+=(cmax-c)*s[now].price;	
				c=cmax-(s[next].distance-s[now].distance)*1.0/d_avg;
			} 
			now=next;//更新加油站 
		}
		if(now==n)	printf("%.2f",ans);
		else printf("The maximum travel distance = %.2f",s[now].distance+cmax*d_avg); 
	}
	
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值