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

题意:

容量很大的01背包。

思路:

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

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<bitset>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn=+; int n;
ll t;
ll ans;
ll sum; struct node
{
ll v,w;
bool operator<(const node& rhs) const
{
return v>rhs.v;
}
}a[maxn]; void dfs(int cur, ll vv, ll tot, ll left)
{
if(tot>ans) ans=tot;
if(tot+left<=ans) return;
if(cur==n+) return;
if(vv+a[cur].v<=t) dfs(cur+,vv+a[cur].v,tot+a[cur].w,left-a[cur].w);
dfs(cur+,vv,tot,left-a[cur].w);
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%lld",&n,&t))
{
sum=;
for(int i=;i<=n;i++)
{
scanf("%lld%lld",&a[i].v,&a[i].w);
sum+=a[i].w;
}
ans=;
sort(a+,a+n+);
dfs(,,,sum);
printf("%lld\n",ans);
}
return ;
}

HDU 5887 Herbs Gathering(搜索求01背包)的更多相关文章

  1. HDU - 5887 2016青岛网络赛 Herbs Gathering(形似01背包的搜索)

    Herbs Gathering 10.76% 1000ms 32768K   Collecting one's own plants for use as herbal medicines is pe ...

  2. hdu 5887 Herbs Gathering (dfs+剪枝 or 超大01背包)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5887 题解:这题一看像是背包但是显然背包容量太大了所以可以考虑用dfs+剪枝,贪心得到的不 ...

  3. HDU 5887 Herbs Gathering

    背包,$map$,优化. 和普通背包一样,$map$加一个$erase$优化一下就可以跑的很快了. #pragma comment(linker, "/STACK:1024000000,10 ...

  4. hdu 2639 Bone Collector II (01背包,求第k优解)

    这题和典型的01背包求最优解不同,是要求第k优解,所以,最直观的想法就是在01背包的基础上再增加一维表示第k大时的价值.具体思路见下面的参考链接,说的很详细 参考连接:http://laiba2004 ...

  5. HDU 2126 Buy the souvenirs (01背包,输出方案数)

    题意:给出t组数据 每组数据给出n和m,n代表商品个数,m代表你所拥有的钱,然后给出n个商品的价值 问你所能买到的最大件数,和对应的方案数.思路: 如果将物品的价格看做容量,将它的件数1看做价值的话, ...

  6. HDU 2639 Bone Collector II【01背包 + 第K大价值】

    The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...

  7. HDU 3639 Bone Collector II(01背包第K优解)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  8. HDU 1203 I NEED A OFFER! 01背包

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1203 解题思路:简单的01背包,用dp[i]表示花费不超过i时的最大可能性 状态转移方程 dp[i]= ...

  9. HDU 2955_Robberies 小偷抢银行【01背包】

    <题目链接> 题意: 先是给出几组数据,每组数据第一行是总被抓概率p(最后求得的总概率必须小于他,否则被抓),然后是想抢的银行数n.然后n行,每行分别是该银行能抢的钱数m[i]和被抓的概率 ...

随机推荐

  1. Windows多线程基础

    进程与线程基础 程序: 计算机指令的集合,以文件的形式存储在磁盘上 进程: 正在运行是程序实例,以是一个程序在其自身的地址空间的一次执行活动.进程有一个进程管理的内核对象和地址空间组成. 线程: 程序 ...

  2. 松下 激光位移传感器 API

    一: /* ============================================================================================== ...

  3. react native 示例代码

    https://react.rocks/tag/ReactNative?show=60 https://github.com/ReactNativeNews/React-Native-Apps

  4. ubuntu shell脚本出错 dash

    今天在Ubuntu下调试代码,明明是正确的,却仍然报错,查了错误信息才知道:Ubuntu中默认不是bash,而是为了加快开机速度,使用了dash. dash中需要严格的语法,而且与bash语法不同.例 ...

  5. C++飞机大战

    #include<windows.h> #include"resource.h" #include<stdlib.h> #include<time.h ...

  6. Python: re.compile()

    compile(pattern,flags=0) 1.编译一个正则表达式模式,返回一个模式对象 2.第二个参数flags是匹配模式,可以使用按位或‘|'表示同时生效,也可以在正则表达式字符串中指定 P ...

  7. netty4----日志框架的检查

    https://segmentfault.com/a/1190000005797595 2016年06月25日  ·  4.1k 次阅读 Netty4.x Internal Logger机制 nett ...

  8. centos上安装python3.6

    安装python3.6可能使用的依赖 # yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sql ...

  9. 向大家分享一个shell脚本的坑

    打算在跳板机上写一个shell脚本,批量检查远程服务器上的main进程是否在健康运行中. 先找出其中一台远程机器,查看main进程运行情况 [root@two002 tmp]# ps -ef|grep ...

  10. 来了解一下Ajax是什么?Ajax的原理?Ajax与传统Web比较?Ajax的优缺点?Ajax的Post与Get比较

    一.什么是Ajax Ajax(Asynchronous Java and XML的缩写)是一种异步请求数据的web开发技术,对于改善用户的体验和页面性能很有帮助.简单地说,在不需要重新刷新页面的情况下 ...