题目链接: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. codeforces 493B.Vasya and Wrestling 解题报告

    题目链接:http://codeforces.com/problemset/problem/493/B 题目意思:给出 n 个 techniques,每个 technique 的值为 ai. ai & ...

  2. SQL Server output经典使用

    output经典使用 分类: sql2012-02-16 18:17 409人阅读 评论(0) 收藏 举报 outputinserttabledeletegonull OUTPUT是SQL SERVE ...

  3. supersr--NSURLSessionConfiguration-下载进度

    ////  ViewController.m//  下载进度 // //  Created by Super on 14/7/4. //  Copyright (c) 2014年 iOS. All r ...

  4. MVC控制下输出图片、javascript与json格式

    /// <summary> /// 输出图片 /// </summary> /// <returns></returns> public ActionR ...

  5. 关于KVC、KVO

    KVC/KVO --------------------KVC--键值编码-------------------作用:通过字符串来描述对象的属性间接修改对象的属性 Student *stu=[[Stu ...

  6. drozer unknown module处理办法

    将目录切换到drozer安装目录,然后在执行:

  7. max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]

    sh- /etc/sysctl.conf vm.max_map_count = #在/etc/sysctl.conf追加上面一条 #并执行命令: sysctl -p

  8. sql server 本地复制订阅 实现数据库服务器 读写分离(转载)

    转载地址:http://www.cnblogs.com/echosong/p/3603270.html 再前段echosong 写了一遍关于mysql 数据同步实现业务读写分离的文章,今天咱们来看下S ...

  9. AR , VR, GVR ...

    虚拟现实与增强现实(眼镜或头盔)的现状与未来-简介http://blog.csdn.net/yanzhanyi/article/details/41982033 Google VR  |  Googl ...

  10. Delphi中DLL的创建和使用

    参考:http://blog.csdn.net/ninetowns2008/article/details/6311663 结合这篇博客:http://www.cnblogs.com/xumenger ...