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

解决行车过程中如何在多个加油站选择加油以达到目的地并使总花费最少的问题。输入包括汽车油箱容量、目的地距离等。

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

1033. To Fill or Not to Fill (25)

时间限制
10 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
ZHANG, Guochuan

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

本题是个行车加油找最小花费问题,解法是贪心。

#include <cstdio>
#include <cstdlib>
#include <climits>


typedef struct{
	double price;
	double dist;
}STATION;

int cmp(const void *a,const void *b){
	int ret;
	STATION *pa = (STATION *)a;
	STATION *pb = (STATION *)b;
	return pa->dist-pb->dist;
}

STATION st[501];

int main(){
	double cmax,dist,davg,currgas,res,minprice;
	int n,i,j,pos;

	scanf("%lf %lf %lf %d",&cmax,&dist,&davg,&n);
	
	for(i=0;i<n;++i){
		scanf("%lf %lf",&st[i].price,&st[i].dist);
	}

	st[n].price = 0;
	st[n].dist = dist;

	qsort(st,n,sizeof(STATION),cmp);


	if(st[0].dist>0){
		printf("The maximum travel distance = 0.00\n");
		return 0;
	}

	currgas = 0.0;
	double limit = cmax * davg;
	res = 0.0;

	for(i=0;i<n;){
		if(st[i+1].dist-st[i].dist>limit){//can't reach the destination
			printf("The maximum travel distance = %.2lf\n",st[i].dist + limit);
			return 0;
		}
		
		pos = i;
		minprice = st[i].price;

		//在当前剩余油所能到的范围内找比现在站油价便宜的站中最便宜的站,可以直接跑过去,而不需要加油
		for(j=i+1;j<=n && st[j].dist-st[i].dist<=currgas*davg;++j){
			if(st[j].price<minprice){
				minprice = st[j].price;
				pos = j;
			}
		}

		if(pos!=i){//现在有油,而且在油能到范围内找到了比现在站更便宜而且是价格最便宜的油站(不加油能到),就直接开过去,不加油,到那再加  
			currgas -= ((st[pos].dist-st[i].dist)/davg);
			i = pos;
			continue;
		}

		//没油或者是有油但是油所能到的范围内没有比现在站价格更便宜的站
		//此时必须在此站加油了,关键是计算要加多少油
		//要找距离次油站最近的比该油站价格便宜的站,然后到了那站再贪心
		for(j=i+1;j<=n && st[j].dist-st[i].dist<=limit;++j){
			if(st[j].price<minprice){
				minprice = st[j].price;
				pos = j;
				break;
			}
		}

		//现在没油或现油箱所能到的油站价格都比现在油站贵:要去最近的比现油站便宜的油站(不加油到不了),得在现油站加上刚好满足的油量
		if(pos!=i){
			res += ((st[pos].dist-st[i].dist)/davg-currgas)*st[i].price;
			currgas = 0;//刚开始这里忘记将currgas清零,一直结果不对
			i = pos;
			continue;
		}

		minprice = INT_MAX;

		//现在有油或没油,且油箱所能到的所有站价格都比现油站贵
		//所以在油箱所能到的所有油站中选择一个最便宜的把车加满油开过去,然后以该站为起点继续贪心即可
		for(j=i+1;j<=n && st[j].dist-st[i].dist<=limit;++j){
			if(st[j].price<minprice){
				minprice = st[j].price;
				pos = j;
			}
		}

		res += (cmax-currgas)*st[i].price;
		currgas = cmax-(st[pos].dist-st[i].dist)/davg;
		i = pos;
	}
	printf("%.2lf\n",res);
	return 0;
}



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值