Problem 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

1 5 15 12 4 2 2 1 1 4 10 1 2

 Sample Output

15

 Source

第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)

 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
#include <queue>
using namespace std;
typedef long long LL;
#define N 2600000
#define met(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f int v[], w[];
int dp[N]; int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int i, j, n, B, Max=, Index; scanf("%d%d", &n, &B); for(i=; i<=n; i++)
{
scanf("%d%d", &w[i], &v[i]);
Max += v[i];
} for(i=; i<=Max; i++)
dp[i] = INF;
dp[] = ;
for(i=; i<=n; i++)
for(j=Max; j>=v[i]; j--)
{
dp[j] = min(dp[j], dp[j-v[i]]+w[i]);
} for(i=; i<=Max; i++)
if(dp[i]<=B) Index = i; printf("%d\n", Index); }
return ;
}

(01背包 当容量特别大的时候) Knapsack problem (fzu 2214)的更多相关文章

  1. HDU 2639(01背包求第K大值)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2639 Bone Collector II Time Limit: 5000/2000 MS (Jav ...

  2. HDU 2639 01背包求第k大

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. 【hdu3080】01背包(容量10^7)

    [题意]n个物品,有wi和vi,组成若干个联通块,只能选取一个联通块,问得到m的价值时最小要多少空间(v).n<=50,v<=10^7 [题解] 先用并查集找出各个联通块. 这题主要就是v ...

  4. FZU 2214 ——Knapsack problem——————【01背包的超大背包】

    2214 Knapsack problem Accept: 6    Submit: 9Time Limit: 3000 mSec    Memory Limit : 32768 KB  Proble ...

  5. 3466 ACM Proud Merchants 变形的01背包

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3466 题意:假设你有M元,已经Pi,Qi,Vi(i为角标,1<i<N),当M>Qi,时才 ...

  6. dp——01背包

    今天学习了01背包不算是复习吧,发现完全不会状态之间的转移如此让我捉摸不透尽管很简单但本人觉得还是很难,奇怪地拐点也很难被发现.知道01背包二维的话是很慢的,然后就是非得先打二维毕竟一维是根据二维的想 ...

  7. POJ 3211 Washing Cloths(01背包变形)

    Q: 01背包最后返回什么 dp[v], v 是多少? A: 普通01背包需要遍历, 从大到小. 但此题因为物品的总重量必定大于背包容量, 所以直接返回 dp[V] 即可 update 2014年3月 ...

  8. csu 1547(01背包)

    1547: Rectangle Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 996  Solved: 277[Submit][Status][Web ...

  9. HDU3339 In Action 【最短路】+【01背包】

    <题目链接> 题目大意: 给出一个0-n组成的图,1-n的点上分布着值为pow的电站,给出图的m条边以及距离,从0出发到n个点中的x个点的行走距离和最小(因为是每炸一个点派出一辆坦克),且 ...

随机推荐

  1. Asp.net 怎样去除表单多行文本框滚动条

    <textarea style="overflow:hidden;border-width:0px;">永远没有滚动条</textarea><text ...

  2. DataSet读取XML

    string file = File.ReadAllText("c://123.xml", Encoding.Default); using (DataSet ds = new D ...

  3. Angular $http解析通过接口获得的json数据

    刚接触angular不久,对很多东西都不了解,今天需要用angular通过接口得到json数据,折腾了好久,总算是能获取到数据了,下面是部分源码,仅供参考: HTML部分: <body ng-a ...

  4. .NET 扩展方法

    .NET 的扩展方法是在.NET 3.0引入的,MSDN给出的定义是:扩展方法使你能够向现有类型“添加”方法(包括你自定义的类型和对象噢),而无需创建新的派生类型.重新编译或以其他方式修改原始类型.扩 ...

  5. 去除VA(Visual Assist)中文注释的红色波浪线

    VS版本:vs2015 番茄版本:VA_X.dll file version 10.9.2089.0 built 2016.02.01 1.打开番茄设置 2.去掉 Underlining->Un ...

  6. ipad或iPhone 访问https网站不成功

    可能的原因是设备的日期不对,将设备日期调整正确即可解决

  7. information_schema系列二(列,列权限,事件,存储引擎)

    这个系列的文章主要是为了能够让自己了解MySQL5.7的一些系统表,统一做一下备注和使用,也希望分享出来让大家能够有一点点的受益. 1:COLUMNS 老规矩.查一下这个表,看一下记录,由于这个是看表 ...

  8. 《深入浅出 Java Concurrency》

    http://www.blogjava.net/xylz/archive/2010/07/08/325587.html

  9. Linux_03------Linux的基本命令

    /** * 基本命令格式 * 命令 [选项] [参数] */ /** * 目录处理命令 * mkdir dirname 创建目录 * mkdir -p dir1/dir 递归创建目录 * cd dir ...

  10. Spring JDBC常用方法详细示例

    Spring JDBC使用简单,代码简洁明了,非常适合快速开发的小型项目.下面对开发中常用的增删改查等方法逐一示例说明使用方法 1 环境准备 启动MySQL, 创建一个名为test的数据库 创建Mav ...