【PAT习题】贪心 A1033 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: C​max​​ (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; D​avg​​ (≤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: P​i​​, the unit gas price, and D​i​​ (≤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.

示例

输入:
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
输出:
749.17

输入:
50 1300 12 2
7.10 0
7.00 600
输出:
The maximum travel distance = 1200.00

题意

已知起点终点之间的距离D、油箱容量Cmax、单位汽油能支持前进的距离Davg、N个加油站的单位油价和到起点的距离,初始时刻处于起点且油箱为空,可以在任意加油站购买任意量的汽油。输出从起点到终点的最小花费,若无法到终点则输出能够行驶的最远距离。

思路

  • 错误:优先选择能去到的范围内的油价最低加油站并每次都加满,这是一开始自己想到的
  • 主要策略(当前加油站为s)
    • 1.可行范围内有比当前油价更低的最近的加油站k,则在s加恰好能到达k的油并前往
    • 2.若没有更低油价的,则在可行范围内找到加油价最低的油站k,在s加满油前往k
    • 3.若满油下最远前进长度内没有加油站,结束
    • 1和2可合并:在所有满油状态下能到达的加油站中,选出油价最低的,若在枚举过程中找到第一个油价低于当前加油站的,则退出循环结束选择
  • 注意
    • 把终点看作油价为0、距起点为D的加油站,这样在选择下一个去往的加油站时可以统一考虑
    • 将所有加油站按距离从小到大排序
    • 若没有距起点为0的加油站,结束
    • 计算满油状态下最远前进长度

代码

#include <cstdio>
#include <algorithm>
using namespace std;
struct station{
	double price,dist;
}S[510];
bool cmp(station a, station b){
	return a.dist<b.dist;
}
int main(){
	double Cmax,D,Davg;
	int N;
	scanf("%lf%lf%lf%d",&Cmax,&D,&Davg,&N);
	double Dmax=Cmax*Davg,cost=0,vol=0;
	for(int i=0; i<N; i++){
		scanf("%lf%lf",&S[i].price,&S[i].dist);
	}
	S[N].price=0;
	S[N].dist=D;
	sort(S,S+N,cmp);
	int i;
	if((int)S[0].dist==0){
	    // 从起点开始,选择下一个将去往的加油站,i为当前
		for(i=0; i<N; ){
			int j, Pmin=i+1;
			// 若当前和下一个相邻加油站距离超过Dmax,结束
			if(S[i+1].dist-S[i].dist>Dmax) break;
			// 找出可行范围内油价最低的加油站Pmin,作为下一个将去往的
			for(j=i+1; S[j].dist-S[i].dist<=Dmax && j<=N; j++){
				// 优先选择比当前油价低的
				if(S[j].price<S[i].price){
					Pmin=j;
					break;
				}
				// 不然选择尽量低的
				else{
					if(S[j].price<S[Pmin].price) Pmin=j;
				}
			}
			// 若有比当前油价低的
			if(S[Pmin].price<S[i].price){
			    // 则在当前加油站应加恰好能到Pmin的油
				cost+=S[i].price*((S[Pmin].dist-S[i].dist)/Davg-vol);
				// 加油后从当前加油站出发时的油量
				vol=(S[Pmin].dist-S[i].dist)/Davg;
			}
			// 没有比当前油价低的
			else{
			    // 则在当前加油站应把油加满
				cost+=S[i].price*(Cmax-vol);
				vol=Cmax;
			}
			// 到选择的Pmin后的油量
			vol-=(S[Pmin].dist-S[i].dist)/Davg;
			// 下一个将到达的加油站(要去的),更新i
			i=Pmin;
		}
		if(i==N) printf("%.2f",cost);
		else printf("The maximum travel distance = %.2f",S[i].dist+Dmax);
		return 0;
	}
	else printf("The maximum travel distance = 0.00");
	return 0;
}
发布了3 篇原创文章 · 获赞 0 · 访问量 115
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览