九度OJ1437

题目描述:

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.


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------通过分析可以很容易看出是使用贪心算法,本题的难点在于选择贪心算法的策略:汽车到一站之后就选择下一站,如果在最大行程范围内有比本站价钱少的站点,则加到刚好能行驶到下一站即可;如果在最大行程范围内没有比本站价钱更少的站,则选择价钱最少的那个站,且加满;如果在最大行程内没有站点,则表示不能到达目的地!此外,有特殊情况,就是起始点没有站点,此时最大行驶距离就为0.

在确定贪心算法的时候,一定要进行证明!代码如下(很挫)

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

struct gasta
{
	double price;
	double dst;
	bool operator <(const gasta& a)const
	{
		return dst<a.dst;
	}
};

int main()
{
	double cmax,d,davg;
	double max;
	int n;
	int index;
	double cdst,cval;
	double cost;
	while(scanf("%lf%lf%lf%d",&cmax,&d,&davg,&n)!=EOF)//容量、距离、每单位油量能跑的距离、站点 
	{
		max=cmax*davg;
		cost=0;
		cdst=0;//当前距离 
		cval=0;//当前油量能跑的距离 
		index=0;//当前站点 
		gasta gas[n];
		for(int i=0;i<n;i++)
		{
			scanf("%lf%lf",&gas[i].price,&gas[i].dst);
		}
		sort(gas,gas+n);
		if(gas[0].dst!=0)
		{
			index=n;
			cdst=0;
		}
		while(index<n&&cdst<d)
		{
			int min=index+1;
			int indexs=index;
			int i;
			for(i=index+1;(gas[i].dst<=gas[index].dst+max)&&i<n;i++)
			{
				if(gas[i].price<=gas[index].price)
				{
					cdst=gas[i].dst;
					cost+=gas[index].price*(gas[i].dst-gas[index].dst-cval);
					cval=0;
					index=i;
					break;
				}else
				{
					if(gas[min].price>gas[i].price)
						min=i;
				}
			}
			if(index==indexs)
			{
				if(min==n)
				{
					if(gas[index].dst+max<d)
					{
						cost+=(max-cval)*gas[index].price;
						cdst+=max;
						index=n;
					}else
					{
						cost+=(d-cdst-cval)*gas[index].price;
						cdst=d;
						index=n;
					}
				}else
				{
					if(i==index+1)
					{
						cost+=(max-cval)*gas[index].price;
						cdst+=max;
						index=n;
					}else if(gas[index].dst+max>d)
					{
						cost+=(d-cdst-cval)*gas[index].price;
						cdst=d;
						index=n;
					}else
					{
						cost+=(max-cval)*gas[index].price;
						cdst=gas[min].dst;
						cval=max-(gas[min].dst-gas[index].dst);
						index=min;	
					}
				}
			}
		}
		if(cdst<d)
			printf("The maximum travel distance = %.2lf\n",cdst);
		else
			printf("%.2lf\n",cost/davg);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值