To Fill or Not to Fill

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

Solution

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

class station {
private:
	int location;//从起点到加油站的距离
	float price;//加油站的油价
public:
	station() :location(0), price(0) {};
	void set_loc(int loc);
	void set_price(float pri);
	int get_loc();
	float get_price();
};

void sort(station* p, int n);//排序,这里为了实现简单采用了N方的的算法,可以用更优化的排序算法,n为总个数

int main(void) {
	int capacity, distance, number, max_move, sum_move;
	int i, j, di, c_loc, m_loc, cheap_station, first_station;
	float sum, pi, cheap_gas, first_gas, gas, rate;
	station* p;
	sum = 0;
	sum_move = 0;
	gas = 0;
	cin >> capacity >> distance >> rate >> number;
	max_move = capacity * rate;//加满油后的最大行驶距离
	p = new station[number + 1];
	for (i = 0; i < number; i++) {//读入数据
		cin >> pi >> di;
		p[i].set_loc(di);
		p[i].set_price(pi);
	}
	p[i].set_loc(distance);//将终点视为一个加油站
	p[i].set_price(0);//终点加油站的价格设置为0,这样在贪心下就一定会来到这个加油站
	sort(p, number + 1);//进行排序
	if (p[0].get_loc() != 0) {//如果起点没有加油站,就直接结束
		cout << "The maximum travel distance = " << fixed << setprecision(2) << (float)sum_move << endl;
		return 0;
	}
	c_loc = 0;//记录当前是第几个加油站(按照距离顺序)
	while (1) {
		if (c_loc == number) {//如果已经到终点了就停止循环
			break;
		}
		j = c_loc + 1;//从下一个加油站开始
		cheap_gas = 100;
		first_station = -1;
		m_loc = -1;
		while (1) {//寻找从这个加油站开始的最大行驶距离内最便宜的加油站和第一个价格低于当前加油站的加油站
			if (p[j].get_loc() > p[c_loc].get_loc() + max_move) {//判断是否是在最大行驶范围内
				m_loc = j - 1;//记录下最远能到的加油站并退出循环
				break;
			}
			if (p[j].get_price() < cheap_gas) {//记录最便宜的加油站
				cheap_station = j;
				cheap_gas = p[j].get_price();
			}
			if (p[j].get_price() < p[c_loc].get_price()) {//记录下第一家比当前加油站更便宜的加油站
				first_station = j;
				first_gas = p[j].get_price();
				break;
			}
			j++;
		}
		if (c_loc == m_loc) {//如果在最大行驶距离内都没有加油站,则结束循环
			break;
		}
		if (first_station == -1) {//如果有比当前更便宜的加油站就加适量的油,使汽车恰好能到达该加油站
			sum += (capacity - gas) * p[c_loc].get_price();
			sum_move += (p[cheap_station].get_loc() - p[c_loc].get_loc());
			gas = (float)capacity - (p[cheap_station].get_loc() - p[c_loc].get_loc()) / rate;
			c_loc = cheap_station;
		}
		else {//如果没有,那就直接加满油去范围内最便宜的加油站
			sum += ((p[first_station].get_loc() - p[c_loc].get_loc()) / rate - gas) * p[c_loc].get_price();
			sum_move += (p[first_station].get_loc() - p[c_loc].get_loc());
			gas = 0;
			c_loc = first_station;
		}
	}
	if (c_loc == number) {//如果到了终点
		cout << fixed << setprecision(2) << sum << endl;
	}
	else {//没到终点
		cout << "The maximum travel distance = " << fixed << setprecision(2) << (float)sum_move + max_move << endl;
	}
	return 0;
}

void station::set_loc(int loc) {
	location = loc;
}

void station::set_price(float pri) {
	price = pri;
}

int station::get_loc() {
	return location;
}

float station::get_price() {
	return price;
}

void sort(station* p, int n) {
	int i, j, l;
	station temp;
	for (i = 0; i < n; i++) {
		l = i;
		for (j = i + 1; j < n; j++) {
			if (p[j].get_loc() < p[l].get_loc()) {
				l = j;
			}
		}
		temp = p[l];
		p[l] = p[i];
		p[i] = temp;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值