(01背包 当容量特别大的时候) Knapsack problem (fzu 2214)
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
Sample Output
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)的更多相关文章
- HDU 2639(01背包求第K大值)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2639 Bone Collector II Time Limit: 5000/2000 MS (Jav ...
- HDU 2639 01背包求第k大
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 【hdu3080】01背包(容量10^7)
[题意]n个物品,有wi和vi,组成若干个联通块,只能选取一个联通块,问得到m的价值时最小要多少空间(v).n<=50,v<=10^7 [题解] 先用并查集找出各个联通块. 这题主要就是v ...
- FZU 2214 ——Knapsack problem——————【01背包的超大背包】
2214 Knapsack problem Accept: 6 Submit: 9Time Limit: 3000 mSec Memory Limit : 32768 KB Proble ...
- 3466 ACM Proud Merchants 变形的01背包
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3466 题意:假设你有M元,已经Pi,Qi,Vi(i为角标,1<i<N),当M>Qi,时才 ...
- dp——01背包
今天学习了01背包不算是复习吧,发现完全不会状态之间的转移如此让我捉摸不透尽管很简单但本人觉得还是很难,奇怪地拐点也很难被发现.知道01背包二维的话是很慢的,然后就是非得先打二维毕竟一维是根据二维的想 ...
- POJ 3211 Washing Cloths(01背包变形)
Q: 01背包最后返回什么 dp[v], v 是多少? A: 普通01背包需要遍历, 从大到小. 但此题因为物品的总重量必定大于背包容量, 所以直接返回 dp[V] 即可 update 2014年3月 ...
- csu 1547(01背包)
1547: Rectangle Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 996 Solved: 277[Submit][Status][Web ...
- HDU3339 In Action 【最短路】+【01背包】
<题目链接> 题目大意: 给出一个0-n组成的图,1-n的点上分布着值为pow的电站,给出图的m条边以及距离,从0出发到n个点中的x个点的行走距离和最小(因为是每炸一个点派出一辆坦克),且 ...
随机推荐
- String 及其数组的相关问题
由其他类型转String一般用三种方法 方法1:采用 Object.toString()方法 请看下面的例子: Object object = getObject(); System.out.prin ...
- 汉诺塔算法详解之C++
汉诺塔: 有三根杆子A,B,C.A杆上有N个(N>1)穿孔圆环,盘的尺寸由下到上依次变小.要求按下列规则将所有圆盘移至C杆: 每次只能移动一个圆盘: 大盘不能叠在小盘上面. 提示:可将圆盘临时置 ...
- Linux命令总结:file
转自:http://www.cnblogs.com/kerrycode/p/3806618.html 1.命令简介:用来识别文件类型,也可辨别一些文件的编码格式.它是通过查看文件的头部信息来获取文件类 ...
- 上传图片HTML <form> 标签的 method 属性
<!DOCTYPE HTML><html><body> <form action="/example/html5/demo_form.asp&quo ...
- linux 修改ssh端口号
vi /etc/ssh/sshd_config 找到#Port 22一段,这里是标识默认使用22端口,修改为如下: 代码如下 复制代码 Port 22 Port 50000 然后保存退出 执行/et ...
- 搭建nexus后,进入首页的时候出现warning: Could not connect to Nexus.错误
nexus出现这种问题,一般是版本太旧,换一个高版本的nexus就能解决了.
- c++学习笔记——聚合类
聚合类定义:1.所有的成员都是public的. 2.没有定义任何构造函数. 3.没有类内初始值. 4.没有基类,也没有virtual函数. 聚合类的初始化:我们可以提供一个花括号括起来的成员函数初始值 ...
- 英语语法 It all started the summer before second grade when our moving van pulled into her neighborhood
It all started the summer before second grade when our moving van pulled into herneighborhood It all ...
- spi controller
http://blog.csdn.net/droidphone/article/details/24353293 http://www.china-cpu.com/supports/article/0 ...
- jquery 找不到live方法解决
http://stackoverflow.com/questions/15573645/typeerror-live-is-not-a-function