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 ...
随机推荐
- 【python cookbook】【数据结构与算法】18.将名称映射到序列的元素中
问题:希望通过名称来访问元素,减少结构中对位置的依赖性 解决方案:使用命名元组collections.namedtuple().它是一个工厂方法,返回的是python中标准元组类型的子类,提供给它一个 ...
- 161128、Redis 4.0发布及其新功能介绍
Redis 4.0-rc1 发布了,这是 4.0 的首个 RC 版.Redis 是一个高性能的key-value数据库.Redis 的出现,很大程度补偿了memcached这类keyvalue存储的不 ...
- scala偏函数
package com.ming.test /** * 在Scala中,偏函数是具有类型PartialFunction[-T,+V]的一种函数.T是其接受的函数类型,V是其返回的结果类型. * 偏函数 ...
- redis监控状态
Redis介绍 Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表.哈希.集合和有序集合5种.支持在服务器端计算集合 ...
- org.apache.cxf.interceptor.Fault: No such operation
webservice错误,访问的时候加后缀wsdl即可,如:http://localhost:9000/HelloWorld?wsdl
- React-Native入门指导之iOS篇
React-Native 入门指导系列教程目录 一.准备工作 (已完成) 二.项目介绍与调试 三.CSS样式与Flex布局 四.常用UI控件的使用 五.JSX在React-Native中的应用 六.事 ...
- Linux字符串替换一例:根据IP地址获取指定内容
需求:使用脚本获取到本机IP地址,需要添加iptables规则,需生成网段地址 源格式:获取IP地址为10.10.10.221 目标格式:10.10.10.0 # 方法1 [hadoop@localh ...
- 在discuz二次开发模板时,diy编辑显示我“抱歉,您没有权限添加此模块
<div id="diy_vk_portal_slide_top" class="area"><div id="frameCRxR0 ...
- 在Ecshop后台打印订单页面将商品按货号排序
ECSHOP后台管理里的“打印订单" 页面里的商品排序有点乱,现在想改成按序号来排序,修改方法如下 下面是在2.7.2基础上做的修改 打开 admin/order.php 文件 找到(大约 ...
- 杭电1020-Encoding
Problem Description Given a string containing only 'A' - 'Z', we could encode it using the following ...