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 ...
随机推荐
- WebService 的一些基本概念
一. 1.Endpoint http://www.ttdev.com/SimpleService 这个webservice全名就是所谓的"endpoint" 2.RPC type ...
- CSS For Bar Graphs(maybe old)
Having a working knowledge of XHTML and CSS when developing applications is a big help in knowing wh ...
- UIBarButtonItem变弹簧
UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystem ...
- 随机删除数据库N条记录
delete from table where newsID in(select top 50 newsID from table order by newid())
- URAL 1161 Stripies(数学+贪心)
Our chemical biologists have invented a new very useful form of life called stripies (in fact, they ...
- MVC模型 简介
MVC (Modal View Controler)本来是存在于Desktop程序中的,M是指数据模型,V是指用户界面,C则是控制器.使用MVC的目的是将M和V的实现代码分离,从而使同一个程序可以使用 ...
- 夺命雷公狗ThinkPHP项目之----企业网站25之网站前台面包屑导航URL的完善
如果想取出面包屑导航的url那么就必须在model层里面进行多取一个了: <?php namespace Home\Model; use Think\Model; class CategoryM ...
- 《zw版·delphi与halcon系列原创教程》zw版_THOperatorSetX控件函数列表 v11中文增强版
<zw版·delphi与halcon系列原创教程>zw版_THOperatorSetX控件函数列表v11中文增强版 Halcon虽然庞大,光HALCONXLib_TLB.pas文件,源码就 ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON InpaintingCt2
zw版[转发·台湾nvp系列Delphi例程]HALCON InpaintingCt2 unit Unit1;interfaceuses Windows, Messages, SysUtils, Va ...
- 深入Java核心 探秘Java垃圾回收机制(转自http://edu.21cn.com/java/g_189_859836-1.htm)
垃圾收集GC(Garbage Collection)是Java语言的核心技术之一,之前我们曾专门探讨过Java 7新增的垃圾回收器G1的新特性,但在JVM的内部运行机制上看,Java的垃圾回收原理与机 ...