2016寒假个人赛(1)C(背包)
来源:互联网 发布:java如何解压rar文件 编辑:程序博客网 时间:2024/05/16 08:54
Description
Given a set of n items, each with a weight w[i] and a value v[i], determine a way to choose the items into a knapsack so that the total weight is less than or equal to a given limit B and the total value is as large as possible. Find the maximum total value. (Note that each item can be only chosen once).
Input
The first line contains the integer T indicating to the number of test cases.
For each test case, the first line contains the integers n and B.
Following n lines provide the information of each item.
The i-th line contains the weight w[i] and the value v[i] of the i-th item respectively.
1 <= number of test cases <= 100
1 <= n <= 500
1 <= B, w[i] <= 1000000000
1 <= v[1]+v[2]+...+v[n] <= 5000
All the inputs are integers.
Output
For each test case, output the maximum value.
Sample Input
Sample Output
#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<cstdio>#include<cmath>#include<ctime>#include<cstdlib>#include<queue>#include<vector>#include<set>using namespace std;const int T=5500;#define inf 0x3f3f3f3fL#define mod 1000000000typedef long long ll;typedef unsigned long long LL;int dp[T];int main(){#ifdef zsc freopen("input.txt","r",stdin);#endifint N,i,j,k,n,m,u,v;scanf("%d",&N);while(N--){memset(dp,inf,sizeof(dp));dp[0] = 0;scanf("%d%d",&n,&m);for(i=0;i<n;++i){scanf("%d%d",&u,&v);for(j=5000;j>=v;--j){if(dp[j-v]+u<=m&&dp[j]>dp[j-v]+u){dp[j] = dp[j-v]+u;}}}bool flag = false;for(j=5000;j>=0;--j){if(dp[j]<=m){flag = true;break;}}if(flag){printf("%d\n",j);}else{printf("0\n");}} return 0;}
- 2016寒假个人赛(1)C(背包)
- 2016寒假个人赛(1)B(数学)
- 2016寒假个人赛(1)A(贪心)
- 2016寒假个人赛(1)J(模拟)
- CDZSC_2016寒假个人赛(2)-C
- CDZSC_2016寒假个人赛(2)-G(模拟)
- CDZSC_2016寒假个人赛(2)-L(模拟)
- 2016寒假训练——01背包
- 2015寒假训练赛一 C题(hdacm 4740)
- SDUT - 2017年寒假集训 阶段测试赛3(个人) -- 解题报告
- 寒假个人总结
- 寒假前刷题(1)
- 0-1背包问题(c/c++)
- 2016寒假冬训练习#1
- 2012寒假POJ(1)
- 2016 寒假训练赛(一) 自我总结
- 2016寒假训练赛(二)自我总结
- 0-1背包问题(C++)
- IOS 使用UIPageViewController创建开始屏幕 附代码
- Android Code name Version API level
- php乱码的解决方法
- 程序员就是累死能干的闲死偷懒的
- ffmpeg 读取 rtmp流 编码成ts m3u8文件
- 2016寒假个人赛(1)C(背包)
- GoogleAPI Adwords Ad Scheduling
- 自定义dialog 通过DialogFragment
- springmvc validator遇到的问题
- HDOJ 1878 欧拉回路(判定无向图欧拉回路简单题)
- Java程序的运行和调试
- iOS Label行间距设置
- 16. Shell scripts
- 简述正则表达式