九度OJ 1437 To Fill or Not to Fill ( 贪心算法)

题目链接:http://ac.jobdu.com/problem.php?pid=1437


题目描述:

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.

输入:

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.

样例输入:
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
50 1300 12 2
7.10 0
7.00 600
样例输出:
749.17
The maximum travel distance = 1200.00

此题为贪心算法,而且算是比较复杂的贪心算法的运用,他的复杂之处在于需要分类讨论,我们的目标当然是找油价最低的加油站加油,当处在一个加油站时我们面临三种处境,第一,就算加满油也到不了下一个站,这时当然就是计算距离然后退出循环。第二,能够找到在当前油量能力范围内的一个比当前油价便宜而且是最便宜的加油站,那么开到那个站,如果找不到那么就只能加点油往前开直到找到比自己油价低的那个站。第三,就算加满油也找不到比当前加油站便宜的加油站了,那么此时要加满油并开到所能开到的站中油价最小的站。这就算主要的框架,当然还有很多细节要判断。具体见下面程序清单。 ps.程序中比较多大的小的循环一定要搞清楚哪里用break 哪里用continue,我就犯了这个小错误,调了非常久。

#include <stdio.h>
#include <algorithm>
using namespace std;

struct station{
	double price;
	double dist;
	bool operator < (const station &a) const{
			return dist<a.dist;
	}
}buf[510];

int main(){
	double avg,distance,capacity,minPrice;
	int n;
	while(scanf("%lf%lf%lf%d",&capacity,&distance,&avg,&n)!=EOF){
		for(int i=0;i<n;i++){
			scanf("%lf%lf", &buf[i].price,&buf[i].dist);
		}
		sort(buf,buf+n);
		buf[n].price=0;
		buf[n].dist=distance;
		if(buf[0].dist>0){
			printf("The maximum travel distance = 0.00\n");
			continue;
		}

        int k=0,j,index;
	    int ok=1;
	    double curgas=0;
	    double sum=0;
	    while(k<n){
			if((buf[k+1].dist-buf[k].dist)>capacity*avg){
				printf("The maximum travel distance = %.2lf\n", buf[k].dist+capacity*avg);
				ok=0;
				break;
			}else{
				index=k;
				minPrice=buf[k].price;
				for(j=k+1;buf[j].dist-buf[k].dist<=curgas*avg && j<=n; j++){
					if(buf[j].price<minPrice){
						minPrice=buf[j].price ;
						index=j;
					}//寻找当前油量能够到达的比当前站价格低的最低的加油站
				}
				if(index!=k){
					curgas-=(buf[index].dist-buf[k].dist)/avg;
					k=index;
					continue; //找到了
				}
				index=k;
				for(j=k+1;buf[j].dist-buf[k].dist<=capacity*avg && j<=n;j++){
					if(buf[j].price<buf[k].price){
						index=j;
						break;
					}//没有找到,所以转向找最近一个比当前站便宜的加油站
				}
				if(index!=k){
					sum+=((buf[index].dist-buf[k].dist)/avg-curgas)*buf[k].price;
					k=index;
					curgas=0; 
					continue;//找到了
				}
				index=k;
				minPrice=1e18; //将最低价格设为最大
				for(j=k+1;buf[j].dist-buf[k].dist<=capacity*avg && j<=n;j++){
					if(buf[j].price<minPrice){
						minPrice=buf[j].price;
						index=j;
					}  // 没有找到,说明满油情况下也找不到比当前油价低的站了,此时应该加满油然后寻找
					   //接下来所以能到达的加油站中最低的站
				}
				sum+=(capacity-curgas)*buf[k].price;
				curgas=capacity-(buf[index].dist-buf[k].dist)/avg;
				k=index;
			}
		}
			if(ok){
				printf("%.2lf\n",sum);
			}
		 
		}
			return 0;
	}
  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值