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. 在当前站的加满油范围内,后面油价都没有比当前站便宜的,这时需要再分一次情况:

2.1 如果在当前站可以到达终点,那么就加到达终点所需要的油(需要考虑到达这一站时现存的油)

2.2 如果不能到达终点,就在当前站加满油,然后在行驶范围内找油价最低的站,停到那一站然后在考虑(此时可能会有油用不完)

3. 在当前站加满油的范围内,都找不到加油站,那么记录下最远行驶距离,并且打印.

注意如果第一站的距离不为0,那么最大行驶距离就是0.

距离和汽油存量的变量需要选择浮点数,否则有可能消耗的汽油不是整数或者行驶的距离不是整数,这样会影响到最后的结果。

最后最后再说的是!!!!!如果到达不了终点,那么输出The maximum travel distance = 1200.00最后那个1200.00一定要输出的是浮点数啊,即便最大行驶距离为0也是0.00!!!!就这一个错误坑了我1个半小时。我真心弱爆了!

代码如下

#include <stdio.h>
#include <stdlib.h>

#define PRINT_I(x) printf("%s = %d\n",#x,x);
#define PRINT_F(x) printf("%s = %f\n",#x,x);
#define PRINT_S(x) printf("%s = %s\n",#x,x);

typedef struct _Station
{
	float price;
	int dist;
}Station;

int cmp(const void *a,const void *b);
int find_next(Station *s,int n,int start,int max,int rest_dist,int rest_gas);

int main(int argc,char* argv[])
{
	int i,c,d,n,nowp,nextp;
	float cost,davg,rest_gas,full_dist,rest;
	Station *sta;

	//freopen("input","r",stdin);

	scanf("%d%d%f%d",&c,&d,&davg,&n);
	full_dist = c * davg;
	sta = (Station *)malloc(sizeof(Station)*n);

	for(i=0;i<n;++i)
		scanf("%f%d",&sta[i].price,&sta[i].dist);

	qsort(sta,n,sizeof(Station),cmp);

	if(sta[0].dist != 0)
	{
		printf("The maximum travel distance = 0.00\n");
		return 0;
	}

	nowp = 0;
	rest_gas = cost = 0.0f;
	rest = d;
	while(rest > 0)
	{
		nextp = find_next(sta,n,nowp,full_dist,rest,rest_gas);
		if(nextp == -2)
		{
			printf("The maximum travel distance = %.02f\n",d-rest+full_dist);
			return 0;
		}
		else if(nextp > 0)
		{
			if(sta[nextp].price < sta[nowp].price)
			{
				cost += ((sta[nextp].dist-sta[nowp].dist)/davg - rest_gas) * sta[nowp].price;
				rest -= sta[nextp].dist-sta[nowp].dist;
				nowp = nextp;
				rest_gas = 0;
			}
			else
			{
				if(rest > full_dist)
				{
					cost += (c-rest_gas) * sta[nowp].price;
					rest -= sta[nextp].dist-sta[nowp].dist;
					rest_gas = c - (sta[nextp].dist-sta[nowp].dist)/davg;
					nowp = nextp;
				}
				else
				{
					cost += (rest/davg - rest_gas) * sta[nowp].price;
					rest = 0;
				}
			}
		}
		else
		{
			cost += (rest/davg - rest_gas) * sta[nowp].price;
			rest = 0;
		}
	}

	printf("%.02f\n",cost);
	
	free(sta);
	return 0;
}

int cmp(const void *a,const void *b)
{
	return ((Station *)a)->dist - ((Station *)b)->dist;
}

int find_next(Station *s,int n,int start,int max,int rest_dist,int rest_gas)
{
	int i,minp;
	float nprice,lprice;
	nprice = s[start].price;

	minp = 0;
	lprice = 1000000.0f;

	if(start+1 == n)   //if now is the last station
	{
		if(rest_dist > max)
			return -2;     //-2 means the car can't arrive
		else
			return -1;    //-1 means the next is end
	}

	if(s[start+1].dist-s[start].dist > max)
		return -2;

	for(i=start+1;i<n;i++)
	{
		if(s[i].dist-s[start].dist > max)
			break;
		if(s[i].price < nprice)
			return i;
		else
		{
			if(s[i].price < lprice)
			{
				lprice = s[i].price;
				minp = i;
			}
		}
	}
	return minp;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值