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. 在ios上时间无法parse返回 "Invalid Date"

    百度搜索:" ios 上面 new date" 关键字 BUG 描述直接上原码 var psrseDate = Date.parse("2010-03-15 10:30: ...

  2. [LeetCode] 458. Poor Pigs_Easy tag: Math

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  3. .NET RSA解密、签名、验签

    using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Sec ...

  4. strus2 框架介绍

    strus2 执行过程: ActionMapper会去找ActionMapping查找URL请求的映射 1:ActionMapping这个类用name+namespace确定请求的映射, (但是仍然不 ...

  5. LeetCode-MinimumDepthOfBinaryTree

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

  6. GNU

    1983年,理查德.斯托曼提出GNU计划(革奴计划),希望发展出一套完整的开放源代码操作系统来取代Unix,计划中的操作系统,名为GNU. 1989年,发表GNU通用公共许可协议(GPL).GPL条款 ...

  7. <<Joint Deep Modeling of Users and Items Using Reviews for Recommendation>> 评论打分预测

    综述: 本文将 CNN 与 FM(Factorization Machine) 结合,基于评论文本来进行评分预测. 简介: 目前将神经网络应用推荐系统的研究工作中,有一类思路是把如CNN等神经网络作为 ...

  8. yii2模板

    GridView 小部件在开发中常用的功能及技巧.持续更新中.... 数据网格或者说 GridView 小部件是Yii中最强大的部件之一.它有一个属性名叫 dataProvider ,这个属性能够提供 ...

  9. QImage与QPixmap完全解析

    转载自http://www.civilnet.cn/bbs/browse.php?topicno=4691 用Qt程序在手机上显示一幅图片对编程人员来说是再基础不过的一件事情了.那么先让大家看两段代码 ...

  10. C/C++笔记 #035# Makefile

    相关资料: Understanding roles of CMake, make and GCC GCC and Make ( A simple tutorial, teaches u how to ...