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.
输入:
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

思路分析

这是一道汽车加油行驶的题目,我这里提几处关键点,大家可以参考一下:
  • 首先,初始化数组,将终点站当成是最后一个加油站,初始化的数据为加油单价为0,加油站离起点的距离为终点的距离
  • 将数组按照距离从小到大排序(类似于问题调度按照结束时间排序一样)
  • 找出当前油箱里的油能到达的所有加油站里,油价最便宜的那个站a,然后去a
  • 或找不到,则找到最近一个能加油的站b,在当前站只加够到b站的油
  • 或找不到比当前站更便宜的站,则在当前站需要加满油,然后跑到能跑到所有站里最便宜的那个

AC代码

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

#define MAX 502
#define EXPENSIVE 300001

struct tank
{
	double price;
	double length;
};

int compare(const void *a, const void *b);

int main()
{
	int n , i, index, j, flag, davg;
	double len, current_gas, sum, min_price, cmax, distance;
	struct tank tanks[MAX];

	while(scanf("%lf %lf %d %d", &cmax, &distance, &davg, &n) != EOF)
	{
		//接收输入参数
		for(i = 0; i < n; i ++)
		{
			scanf("%lf %lf", &tanks[i].price, &tanks[i].length);
		}

		//初始化最后一个加油站,也就是终点,作为判定贪心结束的重要标志
		tanks[n].price = 0;
		tanks[n].length = distance;

		//一箱油可以跑多远
		len = cmax * davg;

		//距离排序
		qsort(tanks, n, sizeof(tanks[i]), compare);

		if(tanks[0].length > 0)
		{	//初始油箱为空
			printf("The maximum travel distance = 0.00\n");
			continue;
		}else
		{	//贪心选择
			flag = 1;
			current_gas = sum = 0;

			for(i = 0; i < n;)
			{
				if((tanks[i + 1].length - tanks[i].length > len))
				{	//某两个加油站之间的距离大于汽车油箱装满油的最大行程
					flag = 0;
					printf("The maximum travel distance = %.2lf\n", tanks[i].length + len);
					break;
				}else
				{
					index = i;
					min_price = tanks[i].price;
					//找出当前油箱里的油能到达的所有加油站里,油价最便宜的那个
					for(j = i + 1; tanks[j].length - tanks[i].length <= current_gas * davg && j <= n; j ++)
					{
						if(tanks[j].price < min_price)
						{
							index = j;
							min_price = tanks[j].price;
						}
					}

					if(index != i)
					{
						current_gas -= (tanks[index].length - tanks[i].length) / davg;
						i = index;
						continue;
					}

					//或找不到,则找到最近一个能加油的站,加些油跑到那个最便宜的那个站
					index = i;
					for(j = i + 1; tanks[j].length - tanks[i].length <= len && j <= n; j ++)
					{
						if(tanks[j].price < min_price)
						{
							index = j;
							break;
						}
					}

					if(index != i)
					{
						sum += ((tanks[index].length - tanks[i].length) / davg  - current_gas) * tanks[i].price;
						current_gas = 0;
						i = index;
						continue;
					}

					//或找不到比当前站更便宜的站,则在当前站需要加满油,然后跑到能跑到所有站里最便宜的那个
					index = i;
					min_price = EXPENSIVE;
					for(j = i + 1; tanks[j].length - tanks[i].length <= len && j <= n; j ++)
					{	
						if(tanks[j].price < min_price)
						{
							index = j;
							min_price = tanks[j].price;
						}	
					}
					sum += (cmax - current_gas) * tanks[i].price;
					current_gas = cmax - (tanks[index].length - tanks[i].length) / davg;	
					i = index;
				}
			}
		}
		if(flag)
			printf("%.2lf\n", sum);

	}

	return 0;
}

int compare(const void *a, const void *b)
{
	const struct tank *p = a;
	const struct tank *q = b;

	if(p->length > q->length)
		return 1;
	else if(p->length < q->length)
		return -1;
	else
		return 0;
}

后记

贪心算法相比动态规划还是简单很多,关键是做题时的思路,如何进行贪心选择!

参考链接

posted @ 2013-02-24 18:16  java程序员填空  阅读(189)  评论(0编辑  收藏  举报