【PAT】【贪心】加油还是不加,是个问题,开车从杭州到其他城市,路上有些加油站,设计最便宜的路线

https://pintia.cn/problem-sets/994805342720868352/problems/994805458722734080
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.

在这里插入图片描述

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 <iostream>
#include<vector>
#include<algorithm>
using namespace std;

struct fillOil {
    
    
	double gasPrice;
	int distance;
};

bool cmpStaion(fillOil a, fillOil b) {
    
    
	if (a.distance == b.distance)
	{
    
    
		return a.gasPrice < b.gasPrice;
	}
	else {
    
    
		return a.distance < b.distance;
	}
}

double fillOrNotFill(int maxCapacity, int distance, int avgrun, fillOil* stations, int nStations) {
    
    
	sort(stations, stations + nStations, cmpStaion);//按照距离从近到远排序
	if (stations[0].distance != 0)
	{
    
    
		// 车没油,也加不了油,哪里也去不了了
		cout << "The maximum travel distance = 0.00";
		return 0.0;
	}


	int canRun = maxCapacity * avgrun; //加满油最远到的距离
	int nowIndex = 0;  // 当前所在的位置距离杭州距离
	int stationIndex = 1, nowStationIndex = 0; //现在在第一个加油站,

	double leftOil = 0; //在便宜的油站,加到的多余的油
	double cheaperPrice = stations[nowStationIndex].gasPrice;//便宜价格
	int toNextDistance = 0; //到下一个站的距离
	double needOil = 0;//到下一个站需要的油

	double cost = 0;
	bool findCheap = false;//是否找到更便宜的

	//还没有到最后一个站,而且也没到终点
	while (stationIndex < nStations && nowIndex < distance)
	{
    
    
		cheaperPrice = stations[nowStationIndex].gasPrice;
		toNextDistance = 0;
		findCheap = false;
		while (stationIndex < nStations && (nowIndex + canRun) >= stations[stationIndex].distance)
		{
    
    
			//查找加满油能跑到的距离里,是否有更便宜的油
			if (cheaperPrice > stations[stationIndex].gasPrice)
			{
    
    
				toNextDistance = stations[stationIndex].distance - stations[nowStationIndex].distance; //距离
				needOil = toNextDistance / (double)avgrun; // 加到下一站需要的油
				if (leftOil - needOil > -1e-9) //剩下的油还够用
				{
    
    
					//cost += 0;//不花钱了
					leftOil -= needOil;
				}
				else {
    
    
					needOil = needOil - leftOil; //更新需要的油量
					leftOil = 0; // 剩下的油量
					cost += needOil * stations[nowStationIndex].gasPrice; //保存到下一站花销
				}
				findCheap = true;
				stationIndex++;
				break; // 找到更便宜的,就不用再找了
			}
			stationIndex++;
		}	

		stationIndex--;
		//没找到更便宜的,直接加满到下一个最远能到的点
		if (stationIndex < nStations && findCheap == false)
		{
    
    
			// 如果可以到终点,直接去; 
			if (distance - nowIndex <= canRun)
			{
    
    
				toNextDistance = distance - nowIndex;
				needOil = toNextDistance / (double)avgrun; // 加到能到的最远的一站需要的油
				if (leftOil - needOil > -1e-9) //剩下的油还够用
				{
    
    
					//cost += 0;//不花钱了
					leftOil -= needOil;
				}
				else {
    
    
					needOil = needOil - leftOil; //更新需要的油量
					leftOil = 0; // 剩下的油量
					cost += needOil * stations[nowStationIndex].gasPrice; //保存到下一站花销
				}
			}
			else {
    
    
				// 否则加满油,并且跑到当前能到达的最后一站去加油
				toNextDistance = stations[stationIndex].distance - stations[nowStationIndex].distance; //距离

				needOil = maxCapacity; // 因为当前已经是最便宜了,直接加满
				if (leftOil - needOil > -1e-9) //剩下的油还够用
				{
    
    
					//cost += 0;//不花钱了
					leftOil -= needOil;
				}
				else {
    
    
					needOil = needOil - leftOil; //更新需要的油量
					leftOil = 0; // 剩下的油量
					cost += needOil * stations[nowStationIndex].gasPrice; //保存到下一站花销
				}
				// 只有没找到更便宜的油站时,才需要加满油
				needOil = maxCapacity;
				leftOil = maxCapacity - toNextDistance / (double)avgrun; //到当前能到达的最后一站后,还有油
			}
		}

		nowIndex += toNextDistance;
		nowStationIndex = stationIndex;
		stationIndex++;//从下个站开始找比当前站便宜了
	}

	if (nowIndex >= distance)
	{
    
    
		printf("%.2f\n", cost);
		return cost;
	}
	else if (nowStationIndex < nStations)
	{
    
    
		//已经到了最后一站,能跑多远跑多远吧
		toNextDistance = canRun + nowIndex < distance ? canRun : distance - nowIndex; //距离
		needOil = toNextDistance / (double)avgrun; // 加到能到的最远的一站需要的油
		cost += needOil * stations[nowStationIndex].gasPrice; //保存花销
		nowIndex += toNextDistance;
		if (nowIndex >= distance)
		{
    
    
			printf("%.2f\n", cost);
			return cost;
		}
		else {
    
    
			printf("The maximum travel distance = %.2f\n", (double)nowIndex);
		}
	}
	else {
    
    
		cout << "出现异常情况";
	}

	return cost;
}


void Test_fillOrNotFill() {
    
    
	int maxCapacity = 0, distance = 0, avgRun = 0, stations = 0;
	cin >> maxCapacity >> distance >> avgRun >> stations;
	fillOil* fillStations = new fillOil[stations];
	for (int i = 0; i < stations; i++)
	{
    
    
		cin >> fillStations[i].gasPrice >> fillStations[i].distance;
	}
	fillOrNotFill(maxCapacity, distance, avgRun, fillStations, stations);
}

int main()
{
    
    
	Test_fillOrNotFill();
}

猜你喜欢

转载自blog.csdn.net/qinglingLS/article/details/124053099
今日推荐