HDU - 5887 Herbs Gathering (搜索+剪枝解决的01背包)

HDU - 5887 Herbs Gathering (搜索+剪枝解决的01背包)

Collecting one’s own plants for use as herbal medicines is perhaps one of the most self-empowering things a person can do, as it implies that they have taken the time and effort to learn about the uses and virtues of the plant and how it might benefit them, how to identify it in its native habitat or how to cultivate it in a garden, and how to prepare it as medicine. It also implies that a person has chosen to take responsibility for their own health and well being, rather than entirely surrender that faculty to another. Consider several different herbs. Each of them has a certain time which needs to be gathered, to be prepared and to be processed. Meanwhile a detailed analysis presents scores as evaluations of each herbs. Our time is running out. The only goal is to maximize the sum of scores for herbs which we can get within a limited time.
Input
There are at most ten test cases.
For each case, the first line consists two integers, the total number of different herbs and the time limit.
The i-th line of the following n line consists two non-negative integers. The first one is the time we need to gather and prepare the i-th herb, and the second one is its score.

The total number of different herbs should be no more than 100. All of the other numbers read in are uniform random and should not be more than 109.
Output
For each test case, output an integer as the maximum sum of scores.
Sample Input
3 70
71 100
69 1
1 2
Sample Output
3

题目大意:

n个物品,每个物品都有一个时间和价值。在限定的时间内怎么拿才能让价值最大。

解题思路:

题目就时最基本的01背包,但是这个时间最大值是1e9,所以就不能用常规的办法解决了,开不了这么大的数组。
但是物品的个数只有100个,乍一看是 2^100次方的复杂度。但是通过剪枝可以优化时间。我们就用搜索+剪枝解决这个问题。
关键问题就是如何剪枝呢?
每个物品都是拿或者不拿之分。对于拿,我们怎么剪枝呢?就看拿了这个物品,我们只要判断拿了这个物品的时间超不超过给定的时间。对于不拿这个物品,我们就假设把剩余的时间都用完,最多能拿多少价值,如果这个价值小于已经求出的ans,那就不用进入这个枝了。
那怎么把它装满呢?我们一开始按单位时间内的价值从大到小排序,拿到这个物品的时候,我们就往后拿,能拿就拿,一直拿到不能拿为止,然后最后剩下的时间,我们就把下一个物品“拆开”放满这个时间。

为了省时间,还优化了一个地方,就是在输入的时候判断一下这个物品的时间是不是大于给定的时间,如果大于,那这个就直接跳过就行了 因为这个不可能被选中。

AC代码:


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 200;
struct node{
	ll time;
	ll value;
	double avg; 
	bool operator < (const node & a){
		return avg > a.avg ;
	}   
    
}a[maxn];
int n;
ll T;
ll ans = 0;
ll min_time = 0x3f3f3f3f;
int cnt = 0;
bool judge(int x,ll tt,ll va){
	if(x == cnt) return true;
	ll last_time = T-tt;
	ll res = 0;
	int i;
	for(i=x+1;i<=cnt;i++){
		
		if(last_time>a[i].time){
			res += a[i].value;
			last_time -= a[i].time;
		}else break;
	}
	if(i<=cnt){
		res += (ll)(a[i].avg)*last_time;
	}
	if(va+res<ans) return false;
	else return true;	
//	
//	double avg = a[x+1].avg;
//	double max_val = 1.0*avg*last_time;
//	if(va+(ll)max_val>ans) return true;
//	else return true;
}
void dfs(int x,ll va,ll tt){
	ans = max(ans,va);
	if(x == cnt+1){
		ans = max(ans,va);
		//cout<<"@@@@@@@@@"<<ans<<endl;
		return;
	}
	if(T-tt<min_time){
		return ;
	}
	if(tt+a[x].time<=T){
		ans = max(ans,va+a[x].value);
		dfs(x+1,va+a[x].value,tt+a[x].time);
	}
	if(judge(x,tt,va)){
		dfs(x+1,va,tt);
	}
	return ;
}
inline void init(){
	min_time = 0x3f3f3f3f;
	ans = 0;
	cnt = 0;
}
int main(){
	while(~scanf("%d%lld",&n,&T)){
		init();
		for(int i=1;i<=n;i++){
			ll aa,bb;
			scanf("%lld%lld",&aa,&bb);
			if(aa>T) continue;
			a[++cnt].time=aa,a[cnt].value =bb;
			a[cnt].avg = (1.0*a[cnt].value/a[cnt].time);
			if(a[cnt].time<T&&a[cnt].value>ans){
				ans = a[cnt].value;
			}
			min_time = min(min_time,a[cnt].time);
		}
		sort(a+1,a+1+cnt);
		dfs(1,0,0);
		printf("%lld\n",ans);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值