贪心算法-PTA-选择车站加油问题

To Fill or Not to Fill
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

这是一个Greedy algorithm的典型题。

Analysis:

题意:起点和终点间分布着若干加油站,油价不一。汽车开始时无油,行进过程中也可能需要中途加油。需要选择一种加油方案,使到达目的地且总费用最少。
由于是总费用要最少,则显然汽车一定要尽可能地加最便宜的油。Greedy algorithm经常是有循环操作的,这里的循环操作就定为“当前在一个station,如何确定下一个station并走到下一个station”。循环不变量即为“车当前在一个station”。

思路:
设车当前在某个station:
① 寻找最近的、油价比当前station便宜的station,并前往该station;
② 若没有(满油范围内的station的油价均比当前station要贵,当前station油价最便宜),则在当前station加满油,并前往范围内最便宜的station;
③ 若没有(满油范围内不存在其他station),则只能原地等死,终点不可达到。

#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
using namespace std;

typedef struct {
	double price;
	double pos;
} Station;

int capacity, destnition_pos, dist_per_gas, n;
vector<Station> sta;

inline double getGasBtwSta(int aim_sta, int cur_sta) 
{ return (sta[aim_sta].pos - sta[cur_sta].pos) / dist_per_gas; }

bool CmpPos(Station a, Station b)
{ return a.pos < b.pos; }

int main(void)
{
	cin >> capacity >> destnition_pos >> dist_per_gas >> n;
	
	 设置站点信息
	Station tmp;
	for (int i = 1; i <= n; i++) {
		cin >> tmp.price >> tmp.pos;
		sta.push_back(tmp);
	}
	tmp.price = 0; tmp.pos = destnition_pos;
	sta.push_back(tmp);
	sort(sta.begin(), sta.end(), CmpPos);
	// sta[0 : n-1]存有n个站点信息 sta[n]为目的地

	 开始行进
	int cur_sta = 0;
	double cur_cost = 0.0, cur_gas = 0.0;

	if (sta[0].pos != 0) {				// 若起点没有加油站 无法开始
		cout << "The maximum travel distance = 0.00"; return 0;
	}
	
	int aim_sta;
	while (cur_sta != n) {
		/// 1. 寻找:在满油距离内 最近的 价格比当前油站便宜的 油站
		///    加刚好能到该站的油量 并前往该站
		aim_sta = -1;
		for (int i = cur_sta + 1; i <= n && sta[i].pos - sta[cur_sta].pos <= capacity * dist_per_gas; i++) {
			if (sta[i].price < sta[cur_sta].price) {
				aim_sta = i; 
				break;
			}
		}
		if (aim_sta != -1) {	// 1.的aim_sta已找到 加刚好能到该站的油量 并前往该站
			double gas_need = getGasBtwSta(aim_sta, cur_sta) - cur_gas;
			cur_cost += gas_need * sta[cur_sta].price;
			cur_gas = 0;
			cur_sta = aim_sta;
			continue;
		}
		/// 2. 在满油距离内 不存在价格比当前油站便宜的 油站(在能力范围内当前油站最便宜)
		///    在当前油站加满油 前往最便宜的油站
		aim_sta = -1; double min_price = 99999;
		for (int i = cur_sta + 1; i <= n && sta[i].pos - sta[cur_sta].pos <= capacity * dist_per_gas; i++) {
			if (sta[i].price < min_price) {
				min_price = sta[i].price; 
				aim_sta = i; 
			}
		}
		if (aim_sta != -1) {	// aim_sta为满油距离内最便宜的油站
			cur_cost += (capacity - cur_gas) * sta[cur_sta].price;
			cur_gas = capacity - getGasBtwSta(aim_sta, cur_sta);
			cur_sta = aim_sta;
			continue;
		}
		/// 3. 在满油距离内 没有其他油站 原地去世
		cout << "The maximum travel distance = " 
			 << fixed << setprecision(2) << (sta[cur_sta].pos + capacity * dist_per_gas);
		return 0;
	}

	 到达目的地 输出结果
	cout << fixed << setprecision(2) << cur_cost;

	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值