算法笔记 贪心算法练习题(3)

算法笔记 贪心算法练习题(3)

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
下面补充两组数据:

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


首先解读一下题意,这是一个汽车从始发地到目的地,途中经过多个加油站,判断如何加油使得最终的花钱数最少。如果加油后不能到达下一个加油站,输出最大行驶距离。
油箱最大容量C<=100
两个目的地之间的间距D<=30000
汽车行驶时单位汽油所能行驶的距离D avg
加油站总数 N<=500
第i个加油站单位油价P i
第i个加油站到起点距离D i

注意:保留两位小数,油箱一开始为空
#include<iostream>
#include<iomanip>
#include<algorithm>
using namespace std;
struct station {
	double price;//在第i个加油站时,单位汽油价格
	double distance;//起点到第i个加油站的距离
}S[501];
bool cmp(station a, station b) {
	if (a.distance != b.distance)
		return a.distance < b.distance;
	else
		return a.price < b.price;
}
int main() {
	int C;//油箱容量
	int car_cityDistance;//车到目的城市之间的距离
	int Davg;//汽车行驶时每单位汽油所能行驶的距离
	int N;//加油站总数
	cin >> C >> car_cityDistance >> Davg >> N;
	double totalLength = car_cityDistance;//记录两城市间距
	for (int i = 0; i < N; i++) {
		cin >> S[i].price >> S[i].distance;
	}
	sort(S, S + N, cmp);
	//如果起点没油,则无法出发
	if (S[0].distance != 0) {
		cout << "The maximum travel distance = " << fixed << setprecision(2) << 0.0;
		system("pause");
		return 0;
	}
	int loc = 0;//当前在第几个加油站
	double cur_oil = 0;//当前油量
	double min_sumSpend = 0;//加油的花费情况

//只要当前的未到达目的地,则继续前往,循环查找
while (car_cityDistance>0) {
	double cur_maxDistance = C*Davg + S[loc].distance;//当前加油站加满油可行驶的最远距离
	if ((S[loc + 1].distance > cur_maxDistance) || (loc == N - 1 && cur_maxDistance < totalLength)) {
		cout << "The maximum travel distance = " << fixed << setprecision(2) << cur_maxDistance;
		system("pause");
		return 0;
	}
	bool findit = false;
	double minPrice = S[loc].price;
	int minloc = loc;
	for (int i = loc + 1; i < N && (S[i].distance <= cur_maxDistance); i++) {
		//找比当前加油站便宜的加油站
		if (S[i].price < minPrice) {
			minPrice = S[i].price;
			minloc = i;
			findit = true;
			break;
		}
	}
	if ((car_cityDistance<C*Davg&&findit == false)) {
	//在最后一段上找不到比当前油价更便宜的加油站则直接到终点
		min_sumSpend += ((double)((totalLength - S[loc].distance) / Davg) - cur_oil)*S[loc].price;
		cout << fixed << setprecision(2) << min_sumSpend;
		system("pause");
		return 0;
	}
	if (findit == true) {
	//如果找得到便宜的,则进行计算上一站到便宜的一路要花多少钱
		min_sumSpend += ((double)(((S[minloc].distance - S[loc].distance) / Davg) - cur_oil))*S[loc].price;
		car_cityDistance = totalLength - S[minloc].distance;
		loc = minloc;
		cur_oil = 0;
	}
	else {
	//没找到便宜的说明之后的都比当前油价贵,那么需要将当前油箱加满,找到下一段中最便宜的加油站停靠加油
		double tempPrice = S[loc + 1].price;
		int tempLoc = loc + 1;
		for (int i = loc + 1; S[i].distance <= cur_maxDistance&&i < N; i++) {
			if (S[i].price < tempPrice) {
				tempPrice = S[i].price;
				tempLoc = i;
			}
		}
		car_cityDistance = totalLength - S[tempLoc].distance;
		min_sumSpend += S[loc].price*(C - cur_oil);
		cur_oil = C - (double)((S[tempLoc].distance - S[loc].distance) / Davg);
		loc = tempLoc;
	}
}
system("pause");
return 0;
}

个人评价一下这道题:细节比较多,让人头疼。还有一定要记得输出格式一个空格都不能错。
基本参考这里的思路
https://blog.csdn.net/matrixa17/article/details/19916995

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值