Bag Problem
Bag Problem
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/131072 K (Java/Others)
Total Submission(s): 1449 Accepted Submission(s): 405
Problem 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背包问题,由于数据比较大,所以只能是搜索来模拟01背包
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int,int>p;
const int INF = 0x3f3f3f3f;
int n,k;
int w[55];
bool vis[55];
int sum,m;
int M;
void DFS(int s,int num,int va)
{
if(s>=k||num>n)
{
return ;
}
M=max(M,va);
for(int i=s; i<k; i++)
{
if(!vis[i])
{
if(w[i]+va>m)//由于序列是递增的,所以当质量大于m时,后面的都不符合
{
return ;
}
vis[i]=true;
DFS(i,num+1,va+w[i]);
vis[i]=false;
}
}
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
scanf("%d",&k);
sum=0;
for(int i=0; i<k; i++)
{
scanf("%d",&w[i]);
}
sort(w,w+k);
for(int i=k-1; i>k-1-n; i--)
{
sum+=w[i];
}
if(w[0]>m)//特殊情况处理就是所有的货物都比背包容量大,
{
sum=0;
}
if(sum<m)
{
printf("%d\n",sum);
continue;
}
M=0;
memset(vis,false,sizeof(vis));
DFS(0,0,0);
printf("%d\n",M);
}
return 0;
}
Bag Problem的更多相关文章
- HDU 3448 Bag Problem
这是一道搜索的背包题目 题意: 有n件物品从中最多选m件,使其总重量不超过v,求能获得的最大重量 有一个很重要的剪枝(是数据的问题还是这个剪枝本身很高效?): 如果重量最大m件物品都不超过v,则答案就 ...
- hdu3448 01背包+dfs
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3448 Description 0/1 bag problem should sound f ...
- Can peel peel solve pesticide problem
Can peel peel solve pesticide problem? Middle peasants medicinal modern agriculture more and more, t ...
- [Tom and Bag][需要记录过程的dp]
http://acm.beihua.edu.cn/problem/1007 Tom and Bag Description Tom is the most handsome CCPC contes ...
- Codeforces Round #105 (Div. 2) D. Bag of mice 概率dp
题目链接: http://codeforces.com/problemset/problem/148/D D. Bag of mice time limit per test2 secondsmemo ...
- Codeforces 148 D Bag of mice
D. Bag of mice http://codeforces.com/problemset/problem/148/D time limit per test 2 seconds memory l ...
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
随机推荐
- FTP规范
FTP协议命令+返回值+返回值解析 FTP message format:FTP commands are Telnet strings terminated by the Telnet end of ...
- jquery 操作select
jQuery("#select_id").change(function(){}); // 1.为Select添加事件,当选择其中一项时触发 var checkValue = jQ ...
- Android根据baidu Android定位SDK实现定位
参考: http://www.open-open.com/lib/view/open1346982366162.html http://api.map.baidu.com/lbsapi/cloud/g ...
- CheckBox的用法
if (window.pageConfig["IsCommend"] == "True") { $("#IsCommend").v ...
- PHP 小谈静态用法
1.静态的表现形式,在PHP中定义一个静态变量需要加入一个关键字——static,静态对象是属于类的,非静态对象是属于对象的 class Fenbi { public $changdu;//属于对象的 ...
- SQL查询一个表的总记录数的方法
一.简单查询语句 1. 查看表结构 SQL>DESC emp; 2. 查询所有列 SQL>SELECT * FROM emp; 3. 查询指定列 SQL>SELECT empmo, ...
- Codeforce Round #221 Div2
每次的CF都是一把辛酸泪! 什么时候能打破这局面,昨天做着睡着了! 有时候有的题目也就差一线! 哎,! A:杠杆原理! B:算最后负的和! B:没弄出来当时就脑短路... C:事后写了个n*log(n ...
- 树形DP +01背包(HDU 1011)
题意:有n个房间,有n-1条道路连接着n个房间,每个房间都有若干个野怪和一定的能量值,有m个士兵从1房间入口进去,到达每个房间必须要留下若干士兵杀死所有的野怪,然后其他人继续走,(一个士兵可以杀死20 ...
- [原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- 开发系统时候运行程序突然报出“WebDev.WebServer40.exe已停止工作”的错误
已经解决,问题描述:在开发系统时候运行程序突然报出“WebDev.WebServer40.exe已停止工作”的错误,程序调试运行,发现程序在打开数据库时候报错,也就是Connection.Open() ...