• 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
    ",ans);
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    Python爬虫 -- 抓取电影天堂8分以上电影
    Kotlin & Vertx 构建web服务
    js promise 风格编程
    Java Config 下的Spring Test方式
    (转)SpringSecurity扩展User类,获取Session
    maven 打包 xml文件
    EL表达式的操作符
    mysql 去除空格
    spring security
    GoogleApis 屏蔽
  • 原文地址:https://www.cnblogs.com/zyb993963526/p/7486844.html
Copyright © 2020-2023  润新知