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. EBS R12重启后无法进入登录页面

    应用启动正常,但无法进入登录页面: The webpage cannot be found HTTP 404 ... No known changes had been made and the Mi ...

  2. .bashrc文件是干什么的(转)

    使用man bash命令查看到的联机帮助文件中的相关解释如下:.bashrc - The individual per-interactive-shell startup file. 这个文件主要保存 ...

  3. IPTV视频基本概念

      480x320, 640x480 标清 1024x720p 高清 1920x1080i (隔行扫描) 也属于高清 1920x1080p 全高清 3840x2160,7680x4320 超(高)清 ...

  4. HTML中的下拉列表 select

    HTML中的下拉列表: <select> <option value ="1">Volvo</option> <option value ...

  5. 第五百八十六天至第六百零五天 how ccan I 坚持

    考研中,勿扰... 我是个逗比,哈哈. 时间不够用了呢,哎.

  6. 解决IE9下JQuery的ajax失效的问题

    jquery ajax在跨域访问post请求的时候,ie9以下无效(包括ie9)   1. 设置浏览器安全属性,启用[通过域访问数据源]选项,如图:

  7. spark Mllib基本功系列编程入门之 SVM实现分类

    话不多说.直接上代码咯.欢迎交流. /** * Created by whuscalaman on 1/7/16. */import org.apache.spark.{SparkConf, Spar ...

  8. Ajax请求SpringMVC

    @RequestMapping(value = "/loadMenu", method = RequestMethod.GET) @ResponseBody public Arra ...

  9. ORA-12505 错误解决

     在Fedora下安装了Oracle 10gR2,安装完成之后,使用netca创建了监听,创建的时候没有使用默认的LISTENER和1521端口,而是使用了LISTENER_DELL和1522端口,终 ...

  10. ios开发 通讯录

    一.通信录开发 通信录开发主要是获取用户手机中的联系人 通过获取用户的通信录,可以在应用中添加好友等 二.如何访问用户的通讯录 在iOS9之前,有2个框架可以访问用户的通讯录 目前需要适配iOS8,所 ...