算法笔记 贪心算法练习题(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
#include
#include
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

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

你可能感兴趣的:(小菜鸡学算法,算法,贪心)