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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章