题目链接: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. 从inet_pton()看大小端字节序

    #include<stdio.h> #include<netinet/in.h> #include<stdlib.h> #include<string.h&g ...

  2. JS练习题

    1.带参数的函数不能重复调用 2.顺序语句: 3.在重复调用中打断 调用用 return   例如: Interval 时间间隔   先清一下  再接着 return:其他的调用也可以加return打 ...

  3. NIS 报错No such map passwd.byname. Reason: Can't bind to server which serves this domain

    在NIS—client端使用命令:ypcat passwd ,把错如上题, 原因:client端ypbind服务未启动解决方法:当然是启动ypbind了,命令:service ypbind start ...

  4. java常用工具类(java技术交流群57388149)

    package com.itjh.javaUtil;   import java.util.ArrayList; import java.util.List;   /** * * String工具类. ...

  5. spring整合httpclient

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://w ...

  6. IE11 的区别

    http://msdn.microsoft.com/zh-tw/visualc/bg182625

  7. FragmentHelper

    package com.icitymobile.anda.util; import java.lang.ref.SoftReference; import java.util.ArrayList; i ...

  8. xml配置文件

    xml文件的层级结构      configuration 配置  properties 属性     settings 设置     typeAliases 类型命名     typeHa ...

  9. hibernate之处理视图

    近期,我去用hibernate去创建视图, 发现无法进立建立视图, 为啥? 个人去尝试去,却发现无法很好的完成, 因为hibernate的作用类似视图 后解决方案是: 1.用传统的方式去处理 2.写存 ...

  10. Mac系统下使用VirtualBox虚拟机安装win7--第五步 共享文件夹设置

    1.启动virtualbox--选中win7虚拟机--点击设置--点击共享文件夹--点击右侧按钮添加共享文件夹