【PAT】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; 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


分析:

贪心问题。在便宜的站点多加油。

分以下几种情况考虑:

(1)目前剩余的油可以找到最近的比目前加油站便宜的站点。那么不用加油,直接开往较便宜的站点;

(2)若不满足(1),但是如果在目前站点加满油可以到达下一个比目前加油站便宜的站点,那么就加油使其刚好可以开到较便宜的站点;

(3)若不满足(1)与(2),也就是说所能到达的最远站点都比目前加油站要贵,那么就要在目前这个站点加满油,开往所能到达的最远的站点。


Tip: 可以将终点也考虑成为一个加油站。

参考  sunbaigui 的博客:http://blog.csdn.net/sunbaigui/article/details/8657146#reply


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
double maxgas; //最大容量
double Distance; //距离
double ave; //每单位油可开距离

struct Node{
	double price;
	double dis;
	Node(double p,double d):price(p),dis(d){}
	Node(){}
};

int FindCheaper(int i, vector<Node> v, double gas){
	int j;
	int index = i;
	double dis_temp = gas * ave;
	double p_temp = v[i].price;
	for(j = i+1; j<v.size() && (v[j].dis-v[i].dis)<=dis_temp; j++){
		if(v[j].price < p_temp){
			index = j;
			break;
		}			
	}
	return index;
}

int FindFurthest(int i,vector<Node> v,double gas){
	int j;
	int index = i;
	double dis_temp = gas  * ave;
	for(j = i+1; j<v.size() && (v[j].dis-v[i].dis) <= dis_temp; j++ ){
		index = j;
	}
	return index;
}

bool cmp(Node a,Node b){
	return a.dis < b.dis;
}

int main()
{
	int N;
	scanf("%lf %lf %lf %d",&maxgas,&Distance,&ave,&N);
	int i;
	double p;
	double dis;
	vector<Node> vec;
	for(i=0; i<N; i++){
		scanf("%lf %lf",&p,&dis);
		Node n(p,dis);
		vec.push_back(n);
	}
	Node final;
	final.dis = Distance;
	vec.push_back(final);
	sort(vec.begin(),vec.end(),cmp);
	if(vec[0].dis != 0 || N==0){
		printf("The maximum travel distance = 0.00\n");	
		return 0;
	}

	double remain_gas = 0.0;
	int index;
	double money = 0.0;
	for(i=0; i<N; ){
		//用剩下的油寻找最近最便宜的加油站	
		index = FindCheaper(i,vec,remain_gas);
		if(index != i){ //找到了
			remain_gas -= (vec[index].dis - vec[i].dis)/ave;
			i = index; //到达这个更便宜的加油站
			continue;
		}
		//用最大的油寻找最近最便宜的加油站
		index = FindCheaper(i,vec,maxgas);
		if(index != i){
			money += ( (vec[index].dis-vec[i].dis)/ave - remain_gas ) * vec[i].price;
			remain_gas = 0.0; //只要加油刚好到达这个便宜的站点就好,所以剩余油量为0
			i = index;
			continue;
		}
		//找不到更便宜的,所以就在目前站点把油加满
		index = FindFurthest(i,vec,maxgas);
		if(index != i){ 
			money += (maxgas - remain_gas) * vec[i].price;
			remain_gas = maxgas -  (vec[index].dis-vec[i].dis)/ave;
			i = index;
			continue;
		}else {
			printf("The maximum travel distance = %.2lf\n",vec[i].dis + maxgas*ave);
			return 0;
		}
	}
	if(i == N){
		printf("%.2lf\n",money);
	}
	return 0;
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.m或d论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 、1资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值