PAT甲级1033 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.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive numbers: C​max​(≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; D​avg​​(≤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: P​i​​ , the unit gas price, and D​i​​(≤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

题目大意:

从A到B的距离为D,路程中有N个加油站,油箱的容积为C,每升油可以行驶Davg。给出每个加油站距离起点A的距离以及每升油的单价。如果能到达B则输出最少加油需要多少钱,如果不能到达终点输出最长的行驶距离。

解题思路:

  • 首先需要对输入的加油站信息根据距起点的距离进行排序:
0号加油站:7.10 0
1号加油站:7.00 150
2号加油站:7.20 200
3号加油站:6.85 300
4号加油站:7.50 400
5号加油站:7.00 600
6号加油站:7.30 1000
7号加油站:6.00 1250
终点:0.00 13000
  • 具体的过程为从当前位置开始到加满油后所能到达的最远距离中存在的加油站中寻找价格比当前的加油站单价低的加油站temp,把油加到正好行驶到temp,此时剩余的油release = 0;如果没有比当前加油站单价低的则寻找其中价格最低的加油站作为下一加油点,同时在当前加油站把油加满 **(因为当前的价格在下一行驶范围内价格最低,所以加的油越多越好,做题时没有考虑这一点导致算不对)**此处要算剩余的油release。如果当前加满油后不能行驶到最近的下一加油站表明不能行驶到终点,最大行驶距离为当前加油站距起点距离加上加满油后最大行驶距离。
  • 范例1的具体行驶过程为从0号加油站->1号->3号(因为下一加油站单价比此处高所以加满油)->5号(此处同上也加满油)->6号->7号->终点
  • 排序后需要判断第一个加油站到起点距离是否为零,不为零则直接输出最远行驶距离为0.00.

代码:

#include<cstdio>
#include<algorithm>
using namespace std;
struct station
{
	double price;
	double distance;
}a[1000];
bool cmp(struct station a,struct station b)
{
	return a.distance < b.distance;
}
int main(void)
{
	double money = 0;
	double release;
	double C,sum_dis,avg_run;
	int S;
	scanf("%lf %lf %lf %d",&C,&sum_dis,&avg_run,&S);
	double run_max = C * avg_run;
	for(int i = 0;i < S;i++)
	{
		scanf("%lf %lf",&a[i].price,&a[i].distance);
	}
	a[S].distance = sum_dis;
	a[S].price = 0;
	sort(a,a+S,cmp);

	for(int i = 0;i < S;i++)
	{
		printf("%d号加油站:%.2f %.0f\n",i,a[i].price,a[i].distance);
	}

	if(a[0].distance!=0)
	{
		printf("The maximum travel distance = 0.00");
		return 0;
	}
	int flag = 0;      //若找到比当前加油站价格低的置为1 
	double min = 10000;
	int i,j,temp;
	for(i = 0;i < S;)
	{
		flag = 0; 
		min = 10000;
		for(j = i+1;a[i].distance+run_max >= a[j].distance&&j <= S;j++)
		{
				if(a[j].price < a[i].price)   //找到比当前加油站价格低的直接跳出循环 
				{
					money += ((a[j].distance - a[i].distance)/avg_run - release)*a[i].price;
					release = 0;
					i = j;
					flag = 1;
					break;  
				}
				else if(a[j].price < min)    //没有比当前加油站价格低的寻找价格最低的加油站 
				{
					temp = j;
					min = a[j].price;	
				}
		}
		if(j == i + 1)                    //如果当前加油站加满油后不能行使到下一加油站,不能行使到终点 
		{
			printf("The maximum travel distance = %.2f",a[i].distance+run_max);
			return 0;
		}
		if(flag == 0)
		{
			money += (C - release) * a[i].price;
			release = C - (a[temp].distance - a[i].distance)/avg_run;
			i = temp;
		}
	}
	printf("%.2f",money);
	return 0;
}
发布了24 篇原创文章 · 获赞 1 · 访问量 511

猜你喜欢

转载自blog.csdn.net/lovingcgq/article/details/104199936