PAT (Advanced Level) 刷题记录 1033 To Fill or Not to Fill (25分) 贪心算法

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.

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; 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.

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<string>
#include<vector>
#include<map>
#include <queue>
#include<algorithm>
using namespace std;
double Cmax, D, Davg, N;
struct GasStation {
	double price;
	double distance;
	GasStation(double _price, double _distance) :price(_price), distance(_distance){}
};
bool cmp(GasStation& a, GasStation& b) {
	return a.distance < b.distance;
}
bool vis[500];
vector < GasStation> gasS;
void search(int start,int maxdistance) {
	double cost = 0.0;//花费
	double nowtank = 0.0;
	int nowarrive = start;
	while (nowarrive !=gasS.size()-1) {//未到达目的地之前不断进行
		double mincost = 1000;				
		int next=-1;
		int i;
		for (i = nowarrive + 1; i <= N; i++) {
			if (gasS[i].distance <= gasS[nowarrive].distance + maxdistance) {
				if (gasS[i].price< mincost) {
					next = i;
					mincost = gasS[i].price;
					if (mincost < gasS[nowarrive].price)//找到了比现在加油站便宜的加油站要立刻退出
            //保证不多买贵的油
            //这里第一遍的时候没有写,因为我想的是找的里程范围内最便宜的油,
            //可是这样的话,就可能出现那个最便宜的加油站比较远,你就要在现在这个较贵的加油站买更多的油,就会花更多的钱
            //因此我们要保证少买贵的油,只要发现离当前加油站最近的,比他便宜的加油站,立刻出发
						break;
				}
			}
		}
		if (next == -1)//如果在最大里程范围内没有找到加油站直接退出
			break;
		double need = (gasS[next].distance - gasS[nowarrive].distance) / Davg;//到达下一个加油站需要的油量
		if (mincost < gasS[nowarrive].price) {//如果要去的加油站比当前的贵,立刻出发
			if (need <= nowtank) { //现有的油够到达下一个加油站
				nowtank -= need;
			}
			else {//如果不够,就加能够到达的量,保证少买贵的油
				cost += (need - nowtank) * gasS[nowarrive].price;
				nowtank = 0;
			}
		}
		else {//如果历程范围内的油都比当前加油站的贵,就先把油箱加满,再出发,保证买更多便宜的油
			cost += (Cmax - nowtank) * gasS[nowarrive].price;
			nowtank = Cmax - need;				
		}	
		nowarrive = next;//更新当前位置
	}
	if (nowarrive != gasS.size() - 1) { //到不了目的地
		printf("The maximum travel distance = %.02f", gasS[nowarrive].distance + maxdistance);
	}
	else//到了
		printf("%.02f", cost);
}
int main() {
	cin >> Cmax >> D >> Davg >> N;
	for (int i = 0; i < N; i++)
	{
		double a;
		double x;
		cin >> a >> x;
		gasS.push_back(GasStation(a, x));
	}
	
	gasS.push_back(GasStation(0.0,D));
	sort(gasS.begin(), gasS.end(), cmp);
	if (gasS[0].distance != 0){ //因为初始油箱是0,所以如果最近的距离不是0,就直接输出
		cout << "The maximum travel distance = 0.00" << endl;
		return 0;
	}
	search(0, Cmax * Davg);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值