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 ...
随机推荐
- [phonegap]安装phonegap
下载nodejs,安装,单nodejs4.0.0 x64编译时,还需要python2.6 or python2.7: 参考怎么安装python2.7: http://jingya ...
- 学习CSS3BUTTON(一)
CSS3 Buttons is a simple framework for creating good-looking GitHub style button links. 引用方式: <li ...
- PostgreSQL 非持久化设置(Non-Durable Settings)
Durability is a database feature that guarantees the recording of committed transactions even if the ...
- 弦图的判定MCS算法(zoj1015)
题意:裸的弦图的判定: 弦图定义:给出一个无向连通图,如果每个环中都存在至少一条弦(环中存在不相邻的两点直接相连)这样的图叫做弦图: 转载:http://blog.csdn.net/crux_d/ar ...
- $.getJSON异步请求和同步请求
先说一下我遇到的问题吧,我之前的一个函数想调用上一个函数的返回值,但是它的返回值一直为空,后来翻了一些资料才明白是异步请求在作怪,不多说,看例子,这是我之前有返回值函数的代码: function ge ...
- 夺命雷公狗—angularjs—25—angular内置的方法(高级)
查看版本信息 angular.version console.log(angular.version); 判断是否相等 angular.equals() var str1 = ''; var str2 ...
- 夺命雷公狗—angularjs—20—$watch监听的用法
- PAT乙级 1024. 科学计数法 (20)
1024. 科学计数法 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 HOU, Qiming 科学计数法是科学家用来表示很 ...
- 开源软件free download manager在windows defender中报毒
从官网上下载的fdm lite 3.9.6,从图片中可以看出安装包有数字签名,windows defender报毒,在线杀毒也检出木马,官网的程序更新到了3.9.6版本,在sourceforge上的源 ...
- RobotFramework 安装配置(一)
服务器接口的测试框架的选择,最后选中了 RobotFramework ,原因一:能有效的管理测试用例,,支持批量执行,能实现关键字驱动或者数据驱动.原因二:支持测试人员可以使用Python和java创 ...