题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3448

Description

0/1 bag problem should sound familiar to everybody. Every earth man knows it well. Here is a mutant: given the capacity of a bag, that is to say, the number of goods the bag can carry (has nothing to do with the volume of the goods), and the weight it can carry. Given the weight of all goods, write a program that can output, under the limit in the above statements, the highest weight. 

Input

Input will consist of multiple test cases The first line will contain two integers n (n<=40) and m, indicating the number of goods and the weight it can carry. Then follows a number k, indicating the number of goods, k <=40. Then k line follows, indicating the weight of each goods The parameters that haven’t been mentioned specifically fall into the range of 1 to 1000000000. 

Output

For each test case, you should output a single number indicating the highest weight that can be put in the bag. 

Sample Input

5 100
8
8 64 17 23 91 32 17 12
5 10
3
99 99 99

Sample Output

99
0

01背包,但是整个空间状态太大,开不了那么大是数组,就算能开那么大的数组也会超时。

二题目中的物品件数又比较少,所以我们可以用搜索写。

//还是写的题目太少了,见的也太少了,遇到这样的题目根本想不到用搜索。anyway加油吧!

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std; int n,m,k,ans;
int a[]; void dfs(int t,int i,int x)
{
ans=max(ans,x);
if(i>k) return;
if(t+<=n && x+a[i]<=m)
dfs(t+,i+,x+a[i]);
dfs(t,i+,x);
} bool cmp(int a,int b){return a>b;} int main()
{
while(scanf("%d%d",&n,&m)==)
{
scanf("%d",&k);
memset(a,,sizeof(a));
for(int i=; i<=k; i++)scanf("%d",&a[i]);
sort(a+,a++k,cmp);
int sum=;
for(int i=;i<=n;i++)sum+=a[i];
if(m>=sum){printf("%d\n",sum);continue;}
ans=;
dfs(,,); ///0---n,1---数组脚标,0---ans;
printf("%d\n",ans);
}
return ;
}

hdu3448 01背包+dfs的更多相关文章

  1. [Swust OJ 465]--吴奶奶买鱼(0-1背包+dfs)

    题目链接:http://acm.swust.edu.cn/problem/465/ 还有一道题只是描述不一样,方法一模一样(http://acm.swust.edu.cn/problem/644/) ...

  2. POJ3628 Bookshelf 2(01背包+dfs)

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8745   Accepted: 3974 Descr ...

  3. HDU_2079_(01背包)(dfs)

    选课时间(题目已修改,注意读题) Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. codeforces 842C Ilya And The Tree (01背包+dfs)

    (点击此处查看原题) 题目分析 题意:在一个树中,有n个结点,记为 1~n ,其中根结点编号为1,每个结点都有一个值val[i],问从根结点到各个结点的路径中所有结点的值的gcd(最大公约数)最大是多 ...

  5. noj [1479] How many (01背包||DP||DFS)

    http://ac.nbutoj.com/Problem/view.xhtml?id=1479 [1479] How many 时间限制: 1000 ms 内存限制: 65535 K 问题描述 The ...

  6. 九度OJ 1123:采药 (01背包、DP、DFS)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2705 解决:1311 题目描述: 辰辰是个很有潜能.天资聪颖的孩子,他的梦想是称为世界上最伟大的医师. 为此,他想拜附近最有威望的医师为师 ...

  7. POJ1112 Team Them Up![二分图染色 补图 01背包]

    Team Them Up! Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7608   Accepted: 2041   S ...

  8. hdu 2955 01背包

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 如果认为:1-P是背包的容量,n是物品的个数,sum是所有物品的总价值,条件就是装入背包的物品的体积和不能 ...

  9. hihoCoder#1055 : 刷油漆 (树形DP+01背包)

    题目大意:给一棵带点权的树,现在要从根节点开始选出m个连通的节点,使总权值最大. 题目分析:定义状态dp(u,m)表示在以u为根的子树从根节点开始选出m个点连通的最大总权值,则dp(u,m)=max( ...

随机推荐

  1. ClassLoad的加载过程及分析

    -Xbootclasspath:bootclasspath 让jvm从指定路径(可以是分号分隔的目录.jar.或者zip)中加载bootclass,用来替换jdk的rt.jar:若非必要,一般不会用到 ...

  2. C#string类;math类;datetime类

    String类: .Length字符的长度   .Trim()去掉开头以及结尾的空格 .TrimStart()去掉字符串开头的空格 .TrimEnd()去掉字符串后面的空格   .ToUpper()全 ...

  3. 【linux】学习7

    鸟哥 22章内容 单个源代码编译运行 设有一个hello.c 可以用下面方式运行 生成可执行文件a.out [localhost scripts]$ gcc hello.c [localhost sc ...

  4. HDU 5901 Count primes (1e11内的素数个数) -2016 ICPC沈阳赛区网络赛

    题目链接 题意:求[1,n]有多少个素数,1<=n<=10^11.时限为6000ms. 官方题解:一个模板题, 具体方法参考wiki或者Four Divisors. 题解:给出两种代码. ...

  5. codeforces gym 100286 H - Hell on the Markets (贪心算法)

    题目链接 题意:n个数分别为a[i],问是否存在一组对应的b[i],b[i]=1 || b[i]=-1,使得ai*bi的n项和为0. 题解: 先证明一个结论吧,对于1≤ai≤i+1,前面ai个数一定可 ...

  6. 完美解决:Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=6&arch=x

    如题: 原因:没有配置resolv.conf 解决方法: 到/etc目录下配置resolv.conf加入nameserver IP,如: nameserver 8.8.8.8nameserver 8. ...

  7. UIButton 的点击事件详解

    UIControlEventTouchDown 单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候. UIControlEventTouchDownRepeat 多点触摸按下事件,点触计数大于1 ...

  8. DropDownList如何添加一个空白的选项

    ddl_class.Items.Insert(0,new ListItem("",""));

  9. TortoiseSVN中Branching和Merging实践

    转自:http://blog.csdn.net/eggcalm/article/details/6606520 使用svn几年了,一直对分支和合并敬而远之,一来是因为分支的管理不该我操心,二来即使涉及 ...

  10. iOS 开发多线程篇—GCD的常见用法

    iOS开发多线程篇—GCD的常见用法 一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) ...