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 ...
随机推荐
- Java线程同步和线程通信
一.线程同步 当多个线程访问同一个数据时,非常容易出现线程安全问题.这时候就需要用线程同步. 不可变类总是线程安全的,因为它的对象状态是不可改变的,但可变类对象需要额外的方法来保证线程安全. 1.同步 ...
- tableview在第一次显示时会自动relodata
tableview在第一次显示时会自动加载数据
- PostgreSQL Obtaining the Result Status
There are several ways to determine the effect of a command. The first method is to use the GETDIAGN ...
- PostgreSQL Replication之第十二章 与Postgres-XC一起工作(5)
12.5 创建表和发送查询 介绍了Postgres-XC以及其底层的思想之后,是时候创建我们的第一个表,看看集群将如何表现.下面的例子演示了一个简单的表.将使用id列的哈希键来分布它: test=# ...
- navicat内的主键和外键
数据库内的一个重点是主键另一个是外键 实体完整性{ 主键的全称:主关键字 它能够进行唯一标示某一列的 主键的三大特点是:唯一 非空 排序 一个没有主键的表不是一个完整的表,只要表设置了主键那 ...
- 【Origin】工仕途中
-脚步翩跹,随蝶起舞,翩翩不知所往 晨起脚步催, 蝴蝶迎面飞; 正是春意浓, 三月好风景. -作于二零一六年三月二十八日
- java父类转子类的一个方法
一般子类可以转父类.但父类转子类就会报cast error. 使用jsonobject 思想:先把父类转jsonstring 再把jsonstring转子类.剩余的子类值可以设定进去. import ...
- 夺命雷公狗—angularjs—19—angular-route
ngRoute包括的内容 ng的路由机制是靠ngRoute提供的,通过hash和history两种方式实现了路由,可以检测浏览器是否支持history来灵活调用相应的方式.ng的路由(ngRoute) ...
- scan design flow(一)
一个典型的scan实现的flow: clock mux和一些rst,在Scan中都被bypass掉,是不能测到的.所以DFT的test coverage一般就在97%或98%. scan design ...
- ASP.NET MVC(三)
ASP.NET Routing 模块的责任是将传入的浏览器请求映射为特有的MVC controller actions. 请求 URL 当我们不使用路由的时候 请求 http://server/app ...