(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个点的行走距离和最小(因为是每炸一个点派出一辆坦克),且 ...
随机推荐
- Consuming a Web Service in AX 2012
Consuming a Web Service in AX 2012 在AX2012版本中如果想调用外部的Web Service变得非常容易. 第一步,在VS中创建一个Web Service并发布 第 ...
- java中多线程中Runnable接口和Thread类介绍
java中的线程时通过调用操作系统底层的线程来实现线程的功能的. 先看如下代码,并写出输出结果. // 请问输出结果是什么? public static void main(String[] args ...
- 127.0.0.1和localhost完全相等吗?
今天在使用ajax发请求的时候遇到如下问题: 以[Access-Control-Allow-Origin]为关键字搜索的结果进行改进,但没有效果. 返回仔细查看错误提示,发现ajax请求的url是lo ...
- c#中获取数组的行列个数的方法
GetUpperBound可以获取数组的最高下标.GetLowerBound可以获取数组的最低下标.这样就可以实现对数组的遍历//定义二维数组string[,] myStrArr2=new strin ...
- mysqldb模块的简单用法
# - *- coding:utf-8-*-import urllib2import reimport MySQLdbimport sysreload(sys)sys.setdefaultencodi ...
- Keynote of Python III
[Keynote of Python III] 1.许多大型网站是用Python开发的,例如YouTube.Instagram,还有国内的豆瓣.很多大公司,包括Google.Yahoo等,甚至NASA ...
- 分享一个 jquery serializeArray()序列化方法
http://www.365mini.com/page/jquery-serializearray.htm http://www.365mini.com/diy.php?f=jquery-serial ...
- ORACLE 分组之后容易被忽略的bug
COL_2 COL_321 3123 31 如上表数据 前台显示显示需要把COL_2的21和23转换成中文 ‘整机’ 最开始如下编写 SELECT t.col_3, CASE ...
- HTML小知识---Label
今天知道了一个html小知识: <input type="checkbox" id="chkVersion" /> ...
- 三级设置页面管理测试demo
#include "PhoneBookWindow.h"#include "xWindow/xWindow.h"#include "hardwareD ...