FZU 2214 Knapsack 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). |
给你n件物品,以及每件物品的质量w[i]和价值v[i]。选择一种装包方式使得背包的最终质量小等于上限B并且最终价值尽可能大。找出最大的总价值。(注意,每件物品只能被选择一次) |
|
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. |
输入的首行是一个整数T表示测试样例的数量。 对于每个测试样例,第一行包含两个整数n和B。 接下来有n行表示每件物品的信息。 第i行分别包含第i件物品的质量w[i]与价值v[i]。 1 <= 测试样例数量 <= 100 1 <= n <= 500 1 <= B, w[i] <= 1000000000 1 <= v[1]+v[2]+...+v[n] <= 5000 输入均为整数。 |
|
Output |
输出 |
|
For each test case, output the maximum value. |
每个测试样例输出其最大价值。 |
|
Sample Input - 输入样例 |
Sample Output - 输出样例 |
|
1 5 15 12 4 2 2 1 1 4 10 1 2 |
15 |
【题解】
最大质量为1000000000,数组肯定不够用。
不过,总价值才5000,我们以价值为轴开辟记录剩余可载质量的一维数组,后面的做法就与01背包如出一辙。
【代码 C++】
#include<cstdio>
#include<cstring>
int main(){
int weight[], t, i, j, n, B, max_value, w, v;
scanf("%d", &t); while (t--){
scanf("%d%d", &n, &B);
memset(weight, , sizeof(weight));
weight[] = B, max_value = ; for (j = ; j < n; ++j){
scanf("%d%d", &w, &v);
for (i = max_value; i >= ; --i){
if (weight[i] - w > weight[i + v]) weight[i + v] = weight[i] - w;
}
for (i = max_value + ; i <= ; ++i) if (weight[i]) max_value = i;
} printf("%d\n", max_value);
}
return ;
}
FZU 2214
FZU 2214 Knapsack problem(背包问题)的更多相关文章
- FZU 2214 ——Knapsack problem——————【01背包的超大背包】
2214 Knapsack problem Accept: 6 Submit: 9Time Limit: 3000 mSec Memory Limit : 32768 KB Proble ...
- FZU 2214 Knapsack problem 01背包变形
题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...
- FZU - 2214 Knapsack problem 01背包逆思维
Knapsack problem Given a set of n items, each with a weight w[i] and a value v[i], determine a way t ...
- knapsack problem 背包问题 贪婪算法GA
knapsack problem 背包问题贪婪算法GA 给点n个物品,第j个物品的重量,价值,背包的容量为.应选哪些物品放入包内使物品总价值最大? 规划模型 max s.t. 贪婪算法(GA) 1.按 ...
- FOJProblem 2214 Knapsack problem(01背包+变性思维)
http://acm.fzu.edu.cn/problem.php?pid=2214 Accept: 4 Submit: 6Time Limit: 3000 mSec Memory Lim ...
- Problem 2214 Knapsack problem 福建第六届省赛
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2214 题目大意:给你T组数据,每组有n个物品,一个背包容量B,每件有体积和价值.问你这个背包容纳的物品最大价值 ...
- FZU Problem 2214 Knapsack problem(背包+思维转换)
转化思维,把价值当成背包容量,选择最小的花费,从上到下枚举,找到当这个最小的花费. #include<iostream> #include<cstring> #include& ...
- FZU 2214 Knapsack dp (转化背包)
就是一个背包裸题,由于物品的重量太大,开不了这么大的数组 所以转化一下,由于价值总和不大于5000,所以把价值看作重量,重量看作价值,那么就是同样的价值下,求一个最轻的重量 #include<c ...
- FZU-2214 Knapsack problem(DP使用)
Problem 2214 Knapsack problem Accept: 863 Submit: 3347Time Limit: 3000 mSec Memory Limit : 327 ...
随机推荐
- n阶乘 尾数0的个数
class Solution {public: int trailingZeroes(int n) { if(n<=0) return 0; int i=0; ...
- 纯CSS实现nav导航栏+jQuery实现article区DIV切换
效果图: main.html 代码: <!DOCTYPE html> <html> <head> <title>MyHomepage</title ...
- ffmpeg无法接收组播流问题处理
问题:ffmpeg无法对IP组播进行处理,表现如下 [root@os01 /]# ffprobe udp://225.0.0.2:9000 ffprobe version Copyright (c) ...
- UVA 12950 : Even Obsession(最短路Dijkstra)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- node-webkit 新建实例窗口间通信问题解决办法
终于弄明白这问题了,只要在js文件里加上段代码,就可解决两窗口间通信问题. var str = { username: User.name, userrole: User.role }; var ne ...
- 完整学习git三 查看暂存区目录树 git diff
1显示暂存区中的目录树 git ls-files git ls-tree git diff 魔法 1工作区与暂存区比较 git diff 2工作区与HEAD比较 git diff HEAD 3暂存区与 ...
- 【转】学习总结--Cookie & Session总结
转载地址:http://www.phperzone.cn/portal.php?aid=718&mod=view 一.状态管理 1)什么是状态管理? 将浏览器与web服务器之间多次交互过程 ...
- SlickGrid example 3a: 可编辑单元
可编辑单元支持一列展示多个属性域,可以为编辑单元提供验证,并且自定义验证事件. 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 T ...
- LA 3907 Puzzle
问题描述:先给你s个禁止串,求不包含禁止串的最长串,如果存在,打印字典序最大. 数据范围:s <= 1000, 禁止串长度不超过50. 分析:不匹配问题实际上等同于匹配问题.假设我们已经有满足条 ...
- uva 816 Abbott的复仇
题目链接:https://uva.onlinejudge.org/external/8/816.pdf 紫书:P165 题意: 有一个最多包含9*9个交叉点的迷宫.输入起点.离开起点时的朝向和终点,求 ...