码迷,mamicode.com
首页 > 其他好文 > 详细

POJ-1042 Gone Fishing (贪心法求最佳钓鱼方案

时间:2014-05-07 08:48:52      阅读:515      评论:0      收藏:0      [点我收藏+]

标签:算法   贪心   

Gone Fishing
Time Limit: 2000MS   Memory Limit: 32768K
Total Submissions: 28075   Accepted: 8371

Description

John is going on a fishing trip. He has h hours available (1 <= h <= 16), and there are n lakes in the area (2 <= n <= 25) all reachable along a single, one-way road. John starts at lake 1, but he can finish at any lake he wants. He can only travel from one lake to the next one, but he does not have to stop at any lake unless he wishes to. For each i = 1,...,n - 1, the number of 5-minute intervals it takes to travel from lake i to lake i + 1 is denoted ti (0 < ti <=192). For example, t3 = 4 means that it takes 20 minutes to travel from lake 3 to lake 4. To help plan his fishing trip, John has gathered some information about the lakes. For each lake i, the number of fish expected to be caught in the initial 5 minutes, denoted fi( fi >= 0 ), is known. Each 5 minutes of fishing decreases the number of fish expected to be caught in the next 5-minute interval by a constant rate of di (di >= 0). If the number of fish expected to be caught in an interval is less than or equal to di , there will be no more fish left in the lake in the next interval. To simplify the planning, John assumes that no one else will be fishing at the lakes to affect the number of fish he expects to catch. 
Write a program to help John plan his fishing trip to maximize the number of fish expected to be caught. The number of minutes spent at each lake must be a multiple of 5.

Input

You will be given a number of cases in the input. Each case starts with a line containing n. This is followed by a line containing h. Next, there is a line of n integers specifying fi (1 <= i <=n), then a line of n integers di (1 <=i <=n), and finally, a line of n - 1 integers ti (1 <=i <=n - 1). Input is terminated by a case in which n = 0.

Output

For each test case, print the number of minutes spent at each lake, separated by commas, for the plan achieving the maximum number of fish expected to be caught (you should print the entire plan on one line even if it exceeds 80 characters). This is followed by a line containing the number of fish expected. 
If multiple plans exist, choose the one that spends as long as possible at lake 1, even if no fish are expected to be caught in some intervals. If there is still a tie, choose the one that spends as long as possible at lake 2, and so on. Insert a blank line between cases.

Sample Input

2 
1 
10 1 
2 5 
2 
4 
4 
10 15 20 17 
0 3 4 3 
1 2 3 
4 
4 
10 15 50 30 
0 3 4 3 
1 2 3 
0 

Sample Output

45, 5 
Number of fish expected: 31 

240, 0, 0, 0 
Number of fish expected: 480 

115, 10, 50, 35 
Number of fish expected: 724 


本题在《算法艺术与信息学竞赛》一书中被作为贪心法的例题,题目大意如下:

bubuko.com,布布扣


解题思路:

还是先放书上的讲解,再说说我的理解:

bubuko.com,布布扣


接下来补充说明一下:

按贪心法的原则,要每次都选择能够得到最大收益的湖泊来垂钓,即钓一次鱼之后,就要再次寻找最优的湖前去垂钓,这样看起来的话,如何计算路上花费的时间就是一个很纠结的问题。所以,我觉得本题思路的一个关键点在于:究竟需要在路上花费多少时间。

通过例子来说明。

假设现在有3个湖,编号为1、2、3,现提出两种钓鱼方案:

(1)1->3->2->3->1

(2)1->1->2->3->3

这两种方案,单看路上花费的时间,显然是方案2更优。而这两种方案所得的鱼量呢?仔细想想,事实上是没有区别的。也就是说,只要确定了你最远要走到哪个湖,并按照贪心法的原则作出了第一种方案,那么完全可以将路线转换为方案二,鱼量不会有任何损失。那么你在路上所花费的总时间就已经确定了,也就是一条直线走到最后所需的时间。


Ps:对本题,若所有湖都已经空了,并且时间还有剩余,默认要把剩余时间全部计入第一个湖的时间。


代码:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

int ans[30][30],f[30],d[30],t[30];
int main()
{
	int h,n;
	cin>>n;
	while(true)
	{
		if(n==0)	break;

		cin>>h;
		h*=12;
		memset(ans,0,sizeof(ans));
		memset(f,0,sizeof(f));
		memset(t,0,sizeof(t));
		memset(d,0,sizeof(d));

		for(int i=1;i<=n;++i)
		{
			scanf("%d",&f[i]);
		}
		for(int i=1;i<=n;++i)
		{
			scanf("%d",&d[i]);
		}
		for(int i=1;i<n;++i)
		{
			scanf("%d",&t[i]);
		}

		int ht,ft[30];
		for(int ed=1;ed<=n;++ed)//枚举最远会走到哪个湖
		{
			memset(ft,0,sizeof(ft));
			for(int i=1;i<=ed;++i)
			{
				ft[i]=f[i];
			}//ft作为f的临时记录
			ht=h;//记录剩余时间
			for(int i=1;i<ed;++i)
			{
				ht-=t[i];//减去路上的时间花费
			}
			//接下来开始模拟钓鱼过程
			int k,emp=1;//emp标记连续的已经空了的湖
			while(ht>0&&emp<=ed)//时间用完或湖空为止
			{
				k=1;
				for(int j=emp;j<=ed;++j)
				{
					if(ft[j]>ft[k])
					{
						k=j;
					}
				}//找出最优的湖

				ans[ed][0]+=ft[k];//此次收获+ft[k]
				++ans[ed][k];//记录在k湖花费了1单位时间
				--ht;//时间消耗1单位
				ft[k]-=d[k];
				ft[k]=ft[k]>0?ft[k]:0;
				for(int j=emp;j<=ed;++j)
				{
					if(ft[j]==0)	++emp;
					else			break;
				}//检查是否ed前的湖都已空
			}
			if(ht>0)	ans[ed][1]+=ht;//若时间有剩余
		}

		int a=1;
		for(int i=2;i<=n;++i)
		{
			if(ans[i][0]>ans[a][0])	a=i;
		}//找出收益最大的方案

		for(int i=1;i<=n;++i)
		{
			cout<<ans[a][i]*5;
			if(i!=n)	cout<<", ";
		}
		cout<<endl;
		cout<<"Number of fish expected: "<<ans[a][0]<<endl;
		
		cin>>n;
		if(n!=0)	cout<<endl;
	}
	return 0;
}



POJ-1042 Gone Fishing (贪心法求最佳钓鱼方案,布布扣,bubuko.com

POJ-1042 Gone Fishing (贪心法求最佳钓鱼方案

标签:算法   贪心   

原文地址:http://blog.csdn.net/harrypoirot/article/details/25073835

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!