问题 C: 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.

输入

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.

样例输入

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

做题感悟

  1. 寻找较好较复杂的测试用例(codeup的测试用例太垃圾简单了)。
  2. 自己脑海中的计算过程 要在 测试用例上 脑补一遍,边脑补边改进。 然后再编程。脑补 -> 测试用例 -> 改进 -> 程序实现
  3. 多阶段决策问题的最优解求解,用动态规划方法来遍历所有可能,自后往前 求得最优解。动态规划公式V(k)=min{cost(k)+V(k+1)}。贪心法只能求得近似解,因为贪心法 只最小化当前决策的最小代价即min{cost(k)} ,并未遍历所有排列组合,而不是动态规划的 最小代价和。

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#define eps 1e-7
using namespace std;
struct Station{
	double p;
	int l;
};
int cmp(Station a, Station b){
	return a.l < b.l;
}
int main()
{
	double maxOil, Destination, Distance_perOil ;
	Station sta[501];
	int num_gasStation;
	while(scanf("%lf %lf %lf %d", &maxOil, &Destination, &Distance_perOil, &num_gasStation)!=EOF){
		//数据整理
		for(int i=0; i<num_gasStation; i++){
			scanf("%lf %d", &sta[i].p, &sta[i].l);
		}
		sort(sta, sta+num_gasStation, cmp);
		sta[num_gasStation].l=Destination;
		sta[num_gasStation].p=0;
		//计算最小代价
		double sum = 0, cur_remainOil=0;
		int now=0, unreachable=0;
		double Distance_fullOil = maxOil * Distance_perOil;
		while(now < num_gasStation){  //判断是否到达终点
			double expect_remainOil;
			if( fabs(sta[0].l) > eps){
				printf("The maximum travel distance = 0.00\n");
				unreachable = 1;
				break;
			}
			//相邻油站 是否 跨度太长
			if( (sta[now+1].l - sta[now].l) > Distance_fullOil ){
				printf("The maximum travel distance = %.2f\n", sta[now].l+Distance_fullOil);
				unreachable = 1;
				break;
			}
			//寻找 油价最便宜的油站
			int min = now;
			for(int i=now+1; ((sta[i].l - sta[now].l) <= Distance_fullOil) && (i <= num_gasStation); i++){
				if (sta[i].p < sta[now].p ){
					min = i;
					break;
				}
			}
			expect_remainOil = (sta[min].l - sta[now].l)/Distance_perOil; 
			// 找不到,那么加满油
			if( min == now ){expect_remainOil = maxOil;}
			//判断加多少油
			double add_Oil = 0;
			if (cur_remainOil < expect_remainOil) {add_Oil = expect_remainOil - cur_remainOil;}
			//计算油钱
			sum += add_Oil * sta[now].p;
			//printf("expect_remainOil = %.2f, cur_remainOil = %.2f, add_Oil= %.2f, sum = %.2f", expect_remainOil, cur_remainOil, add_Oil, sum);
			//更新当前剩余油量
			cur_remainOil = cur_remainOil + add_Oil - (sta[now+1].l - sta[now].l) / Distance_perOil;
			// printf("%.2f\n", cur_remainOil);
			now ++;
		}
		//输出总油费
		if(unreachable == 0) printf("%.2f\n", sum);
	}
	return 0;
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值