问题 C: To Fill or Not to Fill

问题 C: To Fill or Not to Fill

时间限制: 1 Sec  内存限制: 32 MB
提交: 598  解决: 131
[提交][状态][讨论版][命题人:外部导入]

题目描述

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.

输入

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.

输出

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.

样例输入

<span style="color:#333333">59 525 19 2
3.00 314
3.00 0
</span>

样例输出

<span style="color:#333333">82.89
</span>

提示

 

 

 

该题目所要解决的问题是:给定若干加油站信息,问能否驾驶汽车行驶一定的距离。如果能够行驶完全程,则计算最小花费。若不能行驶完全程,则最远能够行驶多长距离。

 

拿到这一题,首先判断汽车是否能够行驶到终点。什么情况下汽车无法行驶到终点呢?两种情况:起点根本就没有加油站,汽车无法启动;或者中途两个加油站之间的距离大于加满油后汽车能够行驶的最大距离。前者汽车行驶的最大距离为0.00,而后者最大距离为当前加油站的距离加上在这个加油站加满油后能够行驶的最大距离。在这里,需要将加油站按到杭州的距离从小到大排序。

 

接下来在能够行驶到终点的情况下计算最小花费。我们首先从路程来考虑,如果在路上,我们能够使用最便宜的汽油,当然就在那个加油站加油了。所以从起点开始遍历每个加油站。假设遍历到了第i个加油站,我们现在来判断在加油站i应该加多少油。设当前汽车离杭州的距离为curLen,当前加油站离杭州的距离为nodes[i].dis,加满油以后汽车能够行驶的最大距离为(dis=cmax*len)。这样就有node[i].dis <= curLen <= nodes[i].dis+dis,否则的话第i个加油站的油是不起作用的。于是在第i个加油站的作用范围内寻找有没有更为便宜的加油站,如果有,则下次使用这个加油站的油(j),这次汽车应该行驶到这个加油站,即touch=nodes[j].dis。如果没有找到更为便宜的加油站则可以在第i个加油站加满油,即touch=nodes[i].dis+dis。然后判断下次应该行驶到的距离与当前距离的关系,如果下次应当行驶到的距离大于当前距离,则汽车行驶,否则不动(也就是说上个加油站的油更便宜,后一个加油站也便宜,根本就不需要在当前加油站加油)

 

首先是审题,从杭州开往目的地,中间有n个加油站,一开始的车没有油,也就是起点必须是加油站,然后寻找加油量最少的站到达,并加油,最后到达目的地,如果两站之间隔得太远不能到,那就在能到的站加满油,走最大距离.

 

根据算法笔记提供的思路

将所有加油站按距离排序,从距离为0的起点出发,并把终点标记为离初始点距离为d,油价为0的点(油价为零可以优先被选成终点)

如果没有距离为0的加油站,说明一开始就加不到油,不能开车

如果可以出发的话有三种情况

1.找得到比当前站油价更低的,那么我们就加正好能到该站的油,去便宜的地方加油,然后到达便宜站后就再次找油价低的

2.找不到比当前站油价更低的,我们就找一个加满油能够走到的,油价是到达的站里面最低的那一站,我们在本站加满油,然后冲到那一站

3.如果我们加满油都到不了下一站,就到不了目的地了,只能在这一战加满油,无脑冲到没油到达最大距离.这个最大距离就是当前站离起点的距离加上满油能冲刺的路程

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

struct station{
	double price;
	double distance;
}st[1010];

bool cmp(struct station a,struct station b){
	return a.distance < b.distance ;
}

int main(){
	double Cmax,d,davg,maxdis;
	int n;
	scanf("%lf%lf%lf%d",&Cmax,&d,&davg,&n);
	maxdis=Cmax*davg;//满箱的油所能走的距离 
	for(int i=0;i<n;i++){
		scanf("%lf%lf",&st[i].price,&st[i].distance);
	}
	sort(st,st+n,cmp);//按到杭州的距离从小到大排序 
	st[n].distance = d;//  终点离杭州的距离为d 
	st[n].price = 0; // 终点不需要加油 
	if(st[0].distance !=0){ //因为一开始没有油,而起始点并没有加油站,开不出去 
		printf("The maximum travel distance = 0.00\n");
	} 
	else{
		int current_station=0;//当前所在的站号
		double sumprice=0,current_oil=0;
		while(current_station < n){
			int k=-1;//初始化油价最低的站号 
			double priMin=1000000000;
			for(int i=current_station+1 ; i<=n && st[i].distance - st[current_station].distance <= maxdis;i++){  //找满油能够到达的油价最低的站 
				if( st[i].price < priMin){  //找到当前的最低油价 
					priMin = st[i].price ;
					k=i; 
					if( st[current_station].price > priMin ){   //如果这个最低油价比当前站的油价还要低就不找了,直接加油,出发去这个站 
						break;
					}
				} 
			}
			if(k==-1)   break;//未能进入for循环即加满油也到不了下一个站,此时不需要继续了,直接在最后输出最长距离 
			double need = (st[k].distance-st[current_station].distance)/davg;//计算从本站到达k站所需要的油量
			
			if(priMin < st[current_station].price ){  //如果当前站的油价比k站的高,我们就加正好到k站的油量need
				if(current_oil < need ){ //查看油缸里的油是否够到k站,如果不够就要加到够 
					sumprice+=(need-current_oil) * st[current_station].price;  // 加正好足need的油
					current_oil=0; //到k站正好用光 
				}
				else{   //如果已经够了,就直接从油缸里减去到k站所需要的油量need
					current_oil=current_oil-need;
				}
			}
			else{  //如果没有比本站便宜的油价,就在本站加满油,并去往已经选出来的相对低价的k站 
				sumprice+=(Cmax-current_oil)*st[current_station].price ;  //加满油 
				current_oil=Cmax-need;  //剩下的油量就是满油减去去往k站的油 
			}
			current_station=k;//将当前站设为k,表示已经到达k站 
		}
		if(current_station == n ){  //到达了终点 
			printf("%.2f\n",sumprice);
		}
		else{      //不能到终点,只能在当前站加满油尽可能的跑远 
			printf("The maximum travel distance = %.2f\n",st[current_station].distance + maxdis);
		}  
				
	}		

	return 0;
}

 

 

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值