PAT A1033 贪心算法

贪心算法

贪心算法主要用来得到一类复杂问题的最优解,主要的思想是从局部入手,当得到了问题之中某个过程的最优解,那么全局的策略也就是最优结果

A1033 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: 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

 

解题思路

本题的局部策略就是解决当汽车在其中的某个加油站时,如何选择要到达的下一个加油站,以及当前应该加多少油,只要解决了这个问题,那么整个的问题也就迎刃而解

首先利用station结构体来接收加油站的信息,之后将接收到的几个加油站信息按照距离远近来重新排序,这样结构体数组的下标就代表了离起点的远近程度

struct Station
{
	double dis,price;
}sta[510];

bool cmp(Station a, Station b)
{
	return a.dis < b.dis;
}

同样将终点也设置为最远的加油站,这样汽车最终是否可以 到达终点的问题就变成了now == n的判断条件

for(int i = 0; i < n; i ++)
    {
        cin>>sta[i].price>>sta[i].dis;
    }  
    sta[n].price = 0; 
    sta[n].dis = D;
    sort(sta, sta + n,cmp);

需要注意的是开始时汽车邮箱为空,必须在起点有加油站,即sta[0].dis == 0,否则就输出当前能到达的距离为0

接收到加油站信息并排序完成之后,就开始进行局部的算法编写

  1. 假设当时汽车在第now加油站,首先要明确的就是汽车在满油状态下最远可以到达多少距离,这个是作为当前汽车能否往下一个加油站走的条件
    	double maxdis = Cmax * Davg;

     

  2. 知道了汽车能够到达的最远距离,就开始利用for循环判断下一次应当去哪一个加油站,显然是应该去汽车可以到达的下一个价格最便宜的加油站,但是当循环时碰到一个价格比当前还要便宜的加油站时,就应该直接过去,不需要再判断是否为最便宜的加油站
    	        int k = -1; //k标识下一个应该去的加油站
    			double minprice = 1000000000;
    			for(int i = now + 1; i <= n && (sta[i].dis - sta[now].dis) < maxdis; i ++)
    			{
    				if(sta[i].price < minprice)
    				{
    					minprice = sta[i].price;
    					k = i;
    					if(minprice < sta[now].price) break;
    				}
    			}

     

  3.  

    通过上面的循环,最终得到了下一个应该去哪一个加油站,如果k的值为-1,就代表无法到达下一个加油站,直接跳出循环。之后就是对当前状况进行判断,如果下一个加油站比当前加油站价格便宜,就加到能到达下一个加油站需要用的油即可,如果当前加油站比下一个加油站的价格便宜,那么就加满油再去下一个加油站,同时令now = k,表示当前已经到了下一个加油站

     

                double need = (sta[k].dis - sta[now].dis) / Davg;
    			
    			if(minprice < sta[now].price)
    			{
    				if(need > nowTank)
    				{
    					sum += (need - nowTank) * sta[now].price;
    					nowTank = 0;
    				}
    				else
    				{
    					nowTank -= need;
    				}
    			}
    			else 
    			{
    				sum += (Cmax - nowTank) * sta[now].price;
    				nowTank = Cmax - need;
    			}
    			now = k;

     

  4.  最后对当前情况进行判断即可,如果当前now == n,代表当前是在最后的终点,输出金额即可,如果不等于,说明最后没有办法到终点,输出sta[now].dis + maxdis,即当前加油站距离起点的距离和最后还能往前走的最远距离
    		if(now == n) printf("%.2f\n",sum);
    		else printf("The maximum travel distance = %.2f",sta[now].dis + maxdis);

     

完整代码

#include<bits/stdc++.h>

using namespace std;

struct Station
{
	double dis,price;
}sta[510];

bool cmp(Station a, Station b)
{
	return a.dis < b.dis;
}

int main()
{
	double Cmax,D,Davg,sum = 0;
	int n;
	
	cin>>Cmax>>D>>Davg>>n;
	
	for(int i = 0; i < n; i ++)
	{
		cin>>sta[i].price>>sta[i].dis;
	}  
	sta[n].price = 0; 
	sta[n].dis = D;
	sort(sta, sta + n,cmp);
	
	double maxdis = Cmax * Davg;
	
	if(sta[0].dis != 0)
	{
		cout<<"The maximum travel distance = 0.00"<<endl;
	}
	else 
	{
		int now = 0;
		double nowTank = 0;
		
		while(now < n)
		{
			int k = -1;
			double minprice = 1000000000;
			for(int i = now + 1; i <= n && (sta[i].dis - sta[now].dis) < maxdis; i ++)
			{
				if(sta[i].price < minprice)
				{
					minprice = sta[i].price;
					k = i;
					if(minprice < sta[now].price) break;
				}
			}
			if(k == -1) break;
			
			double need = (sta[k].dis - sta[now].dis) / Davg;
			
			if(minprice < sta[now].price)
			{
				if(need > nowTank)
				{
					sum += (need - nowTank) * sta[now].price;
					nowTank = 0;
				}
				else
				{
					nowTank -= need;
				}
			}
			else 
			{
				sum += (Cmax - nowTank) * sta[now].price;
				nowTank = Cmax - need;
			}
			now = k;
		}
		
		if(now == n) printf("%.2f\n",sum);
		else printf("The maximum travel distance = %.2f",sta[now].dis + maxdis);
	}
	return 0;
} 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值