题目链接: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. 【leetcode】Rotate List(middle)

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  2. 【C语言】结构体

    不能定义 struct Node { struct Node a; int b; } 这样的结构,因为为了建立Node 需要 建立一个新的Node a, 可为了建立Node a, 还需要再建立Node ...

  3. 【matlab】随意记录

    v = -0.5:0.05:0.5; [x, y] = meshgrid(v); z = sqrt(1.0 - x.^2 - y.^2); mesh(x,y,z); 画一个球的一部分: 2. 求cel ...

  4. The certificate used to sign "XXX" has either expired or has been revoked

    在Xcode真机调试开发过程中,无论是使用个人证书或者是企业证书,经常会遇到这样的问题:The certificate used to sign "XXX" has either ...

  5. <context-param>与<init-param>

    <context-param>的作用: web.xml的配置中<context-param>配置作用1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件w ...

  6. 从键盘输入成绩,找出最高分,并输出学生成绩等级。成绩>=最高分-10,为A,成绩>=最高分-20,为B,成绩>=最高分-30,为C,其余等级为D

    import java.util.Scanner;public class TestStudent { public static void main(String[] args) { //从键盘获得 ...

  7. c语言if语句

    #include<stdio.h>#include<windows.h>#include <limits.h>#include <math.h>int ...

  8. Swift - UIViewController

    UIViewController类详解: 通过Nib文件初始化 init(nibName nibName: String?, bundle nibBundle: NSBundle?) println( ...

  9. python获取指定星期的日期

  10. 数据结构和算法 c#– 1.单项链表

    1.顺序存储结构 Array 1.引用类型(托管堆) 2.初始化时会设置默认值   2.链式存储结构 2.1.单向链表 2.2.循环链表 2.3.双向链表