HDU 5887 Herbs Gathering(搜索求01背包)

http://acm.hdu.edu.cn/showproblem.php?pid=5887

题意:

容量很大的01背包。

 

思路:

因为这道题目背包容量比较大,所以用dp是行不通的。所以得用搜索来做,但是需要一些剪枝,先按体积排序,优先考虑体积大的物品,这样剪枝会剪得多一些(当然按照性价比排序也是可以的)。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<vector>
 6 #include<stack>
 7 #include<queue>
 8 #include<cmath>
 9 #include<map>
10 #include<set>
11 #include<bitset>
12 using namespace std;
13 typedef long long ll;
14 typedef pair<int,int> pll;
15 const int INF = 0x3f3f3f3f;
16 const int maxn=100+5;
17 
18 int n;
19 ll t;
20 ll ans;
21 ll sum;
22 
23 struct node
24 {
25     ll v,w;
26     bool operator<(const node& rhs) const
27     {
28         return v>rhs.v;
29     }
30 }a[maxn];
31 
32 void dfs(int cur, ll vv, ll tot, ll left)
33 {
34     if(tot>ans)  ans=tot;
35     if(tot+left<=ans)  return;
36     if(cur==n+1) return;
37     if(vv+a[cur].v<=t) dfs(cur+1,vv+a[cur].v,tot+a[cur].w,left-a[cur].w);
38     dfs(cur+1,vv,tot,left-a[cur].w);
39 }
40 
41 int main()
42 {
43     //freopen("in.txt","r",stdin);
44     while(~scanf("%d%lld",&n,&t))
45     {
46         sum=0;
47         for(int i=1;i<=n;i++)
48         {
49             scanf("%lld%lld",&a[i].v,&a[i].w);
50             sum+=a[i].w;
51         }
52         ans=0;
53         sort(a+1,a+n+1);
54         dfs(1,0,0,sum);
55         printf("%lld\n",ans);
56     }
57     return 0;
58 }

 

转载于:https://www.cnblogs.com/zyb993963526/p/7486844.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值