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基础之创建窗口——使用GridBagLayout管理器(TryGridBagLayout)
控制台程序. java.awt.GridBagLayout管理器比前面介绍的其他布局管理器灵活得多,因此使用起来也比较复杂.基本机制就是在随意的矩形网格中布局组件,但网格的行和列不一定拥有相同的高度和 ...
- Lintcode: Partition Array
Given an array "nums" of integers and an int "k", Partition the array (i.e move ...
- Notice: Undefined offset 的解决方法
Notice: Undefined offset: 1 in D:\wwwroot\wr\askseo\404.php on line 5 Notice: Undefined offset: 2 in ...
- SQL静态
静态 1.普通成员普通成员都是属于对象的用对象调用 2.静态成员静态成员是属于类的用类名调用 class FenBi{public int length;//普通成员public string col ...
- CCF真题之日期计算
201509-2 日期计算 问题描述 给定一个年份y和一个整数d,问这一年的第d天是几月几日? 注意闰年的2月有29天.满足下面条件之一的是闰年: 1) 年份是4的整数倍,而且不是100的整数倍: 2 ...
- phpredis 订阅者模式
[TOC] 一.场景介绍 最近的一个项目需要用到发布/订阅的信息系统,以做到最新实时消息的通知.经查找后发现了redis pub/sub(发布/订阅的信息系统)可以满足我的开发需求,而且学习成本和使用 ...
- 台电幻彩u盘拆解
- 关键词:ACM & 大小端 & 面试官
关于“ACM” fender0107401 :面试了一个在ACM拿过奖的人 我问了他几个问题: 读取数组中的一个元素,计算复杂度是多少,回答不清楚. 往链表里面存一个数,不排序的情况下,计算复杂度是多 ...
- 怎样查出SQLServer的性能瓶颈
怎样查出SQLServer的性能瓶颈 --王成辉翻译整理,转贴请注明出自微软BI开拓者[url]www.windbi.com[/url]--原帖地址 如果你曾经做了很长时间的DBA,那么你会了解到SQ ...
- 关于jQuery的bind()\trigger()\triggerHandler()
1.bind() 事件绑定. 多个事件会链式累加,而不会覆盖. 即 $("div").bind("click",funtion(){alert("te ...