hdu 5887 搜索优化背包



链接:戳这里


Herbs Gathering

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description
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个物品和背包的大小V (n=100,V<=1e9),物品有重量w和价值v (v、w <=1e9)

问背包最多能装多大的价值


思路:

具体看这里

搜索剪枝优化,对于普通的数据,这道题就是裸的01背包,但是数据太大了不能dp

那么考虑搜索,每个物品选或不选,怎么剪枝呢?

1:对于物品本身w就>V的,直接不要

2:对于每个物品,考虑性价比,也就是v/w,性价比大的先选

3:选到当前的物品,然后加上后面所有的物品都不能使得价值大于当前的最优值,剪枝

4:加上当前物品的重量>V,剪枝

(比赛的时候为什么没写呢?

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
int n;
ll W;
struct node{
    ll w,v;
    double x;
    node(ll w=0,ll v=0,double x=0):w(w),v(v),x(x){}
    bool operator < (const node &a)const{
        return x>a.x;
    }
}s[110];
int m;
ll ans;
bool check(int k,ll w,ll v){
    for(int i=k;i<=m;i++){
        if(w+s[i].w<=W){
            w+=s[i].w;
            v+=s[i].v;
        }
        else {
            v+=(W-w)*s[i].x;
            w=W;
            break;
        }
    }
    if(v>ans) return true;
    return false;
}
void DFS(int k,ll w,ll v){
    ans=max(ans,v);
    if(check(k,w,v) && k<=m){
        if(w+s[k].w<=W) DFS(k+1,w+s[k].w,v+s[k].v);
        DFS(k+1,w,v);
    }
}
int main(){
    while(scanf("%d%I64d",&n,&W)!=EOF){
        m=0;ans=0;
        for(int i=1;i<=n;i++){
            ll w,v;
            scanf("%I64d%I64d",&w,&v);
            if(w<=W) s[++m]=node(w,v,v*1.0/(w*1.0));
        }
        sort(s+1,s+m+1);
        DFS(1,0,0);
        printf("%I64d\n",ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值