PAT A1033. To Fill or Not to Fill (25)
PAT A1033. To Fill or Not to Fill (25)
阿豪boy 发表于9个月前
PAT A1033. To Fill or Not to Fill (25)
  • 发表于 9个月前
  • 阅读 3
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

https://www.patest.cn/contests/pat-a-practise/1033

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

 

 

思路:

    起点与终点的距离为d,油箱最大油量为cmax,单位汽油能够支持前进davg

    1,把终点看作单位油价为0, 离起点距离为d的加油站,然后将所有加油站按照离起点的距离从小到大进行排序,排完序后,如果离离起点最近的加油站的距离不是0,则表示汽车无法出发(初始时刻油量为0),如果离起点最近的加油站距离是0,则进入2

    2,假设当前所处的加油站编号为now,接下来将从满油状态下能到达的所有加油站中选出下一个前往的加油站,策略如下

    (1),寻找距离当前加油站最近的油价低于当前油价的加油站记为k,加恰好能够到达加油站k的油,然后前往加油站k,即优先前往更低油价的加油站

    (2),如果找不到油价低于当前油价的加油站,则寻找油价最低的加油站,在当前加油站加满油,然后前往加油站k,即在没有更低油价的加油站时,前往油价尽可能低的加油站

    (3),如果在满油状态下都找不到能到达的加油站,则最远能到达的距离为当前加油站的距离加上满油状态下能前进的距离,结束算法,即没有加油站可以到达时结束算法

    

#include <iostream>
#include <cstdio>
#include <algorithm> 
using namespace std;

const int MAX = 510;
const int INF = 10000000;
struct Station {
	double price, dis;	//价格和距离 
}st[MAX];

//由小到大排序 
int cmp(Station a, Station b) {
	return a.dis < b.dis;
}

int main(int argc, char *argv[]) {
	int n;
	double cmax, d, davg;
	scanf("%lf%lf%lf%d", &cmax, &d, &davg, &n);

	for (int i = 0; i < n; i++)
		scanf("%lf%lf", &st[i].price, &st[i].dis);
	st[n].price = 0;		//终点是最后一站,价格为0,距离为d 
	st[n].dis = d;
	sort(st, st + n, cmp);	//按照距离将所有加油站从小到大排序
	if (st[0].dis != 0) {
		printf("The maximum travel distance = 0.00\n");
		return 0;
	}
	int now = 0;	//当前所处的加油站的编号
					//总花费,当前油量以及满油行驶距离
	double ans = 0, nowTank = 0, MAX = cmax*davg;
	while (now < n) {	//每次循环将选出下一个需要到达的加油站 
					//选出从当前加油站满油能到达范围内的第一个油价低于当前油价的加油站
					//如果没有油价低于当前加油站的,选择价格最低的那个
		int k = -1;		//最低油价的加油站编号
		double priceMin = INF;		//最低油价
		for (int i = now + 1; i <= n && st[i].dis - st[now].dis <= MAX; i++) {
			if (st[i].price < priceMin) {
				priceMin = st[i].price;
				k = i;
				//如果找到第一个油价低于当前油价的加油站,直接中断循环
				if (priceMin < st[now].price)
					break;
			}
		}

		if (k == -1) break; //满油状态下无法找到加油站,退出循环输出结果
							//下面为能找到可到达的加油站k计算转移花费
							//need是需要的油量
		double need = (st[k].dis - st[now].dis) / davg;
		if (priceMin < st[now].price) {//如果加油站k的油价低于当前油价 
									 //只卖足够到达k的油
			if (nowTank < need) {		//如果油量不足need 
				ans += (need - nowTank)*st[now].price; //补足need 
				nowTank = 0; 	//到达加油站k后油箱内油量为0 
			} else { //当前油量超过need 
				nowTank -= need; //直接到达加油站k 
			}
		} else {	//如果加油站k的油价高于当前油价 
			ans += (cmax - nowTank)*st[now].price; //油箱加满
												   //到达加油站k后油箱内油量为cmax-need
			nowTank = cmax - need;
		}
		now = k;	//到达加油站k,进入下一层循环	
	}
	//能到达终点 
	if (now == n) {
		printf("%.2f\n", ans);
	} else {
		printf("The maximum travel distance = %.2lf\n", st[now].dis + MAX);
	}

	return 0;
}

 

共有 人打赏支持
粉丝 3
博文 464
码字总数 352674
×
阿豪boy
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: