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++】
1 #include<cstdio> 2 #include<cstring> 3 int main(){ 4 int weight[5001], t, i, j, n, B, max_value, w, v; 5 scanf("%d", &t); 6 7 while (t--){ 8 scanf("%d%d", &n, &B); 9 memset(weight, 0, sizeof(weight)); 10 weight[0] = B, max_value = 0; 11 12 for (j = 0; j < n; ++j){ 13 scanf("%d%d", &w, &v); 14 for (i = max_value; i >= 0; --i){ 15 if (weight[i] - w > weight[i + v]) weight[i + v] = weight[i] - w; 16 } 17 for (i = max_value + 1; i <= 5000; ++i) if (weight[i]) max_value = i; 18 } 19 20 printf("%d\n", max_value); 21 } 22 return 0; 23 }
FZU 2214
转载于:https://www.cnblogs.com/Simon-X/p/5128130.html
FZU 2214 Knapsack problem(背包问题)相关推荐
- FZU 2214 Knapsack problem
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2214 题 意:给你一个背包,让你往里面尽量加价值总和多的物品. 思 路:是个01背 ...
- JavaScript实现Knapsack problem背包问题算法(附完整源码)
JavaScript实现Knapsack problem背包问题算法(附完整源码) Comparator.js完整源代码 Sort.js完整源代码 MergeSort.js完整源代码 Knapsack ...
- 动态规划法(四)0-1背包问题(0-1 Knapsack Problem)
继续讲故事~~ 转眼我们的主人公丁丁就要离开自己的家乡,去大城市见世面了.这天晚上,妈妈正在耐心地帮丁丁收拾行李.家里有个最大能承受20kg的袋子,可是妈妈却有很多东西想装袋子里,已知行李的编 ...
- Dynamic Programming 01 —knapsack problem(动态规划背包问题)
首先引入动态变化的含义:为什么要有动态规划? Introduction: 从斐波那契函数的递归中我们发现,在例子求fib(7)的过程中,我们需求得fib(5)和fib(6),而我们在求fib(6)的时 ...
- C#,背包问题(Knapsack Problem)贪心算法的源代码
背包问题(KnapSack Problem)的相关算法是常用的规划算法. 一.什么是背包问题? 背包的问题是,你有一个"袋子",可以装有限数量的物品,鉴于你有一组物品可以从每个物品 ...
- Python多维约束(重量+体积+次数)背包问题(Knapsack Problem)
问题描述:1.一个背包,往里装东西,物品重量w(weight)对应为[2,3,4,7] ,价值va(value)对应为[1,4,7,12] ,如果你的最大承重为20,每个物品可装次数不限,求你能装入背 ...
- Knapsack Problem
背包九讲 0-1 knapsack problem 01背包-牛客网 已知一个背包最多能容纳体积之和为V的物品,现有 n 个物品,第 i 个物品的体积为 vi , 重量为 wi,求当前背包最多能装多大 ...
- [Algorithmic Toolbox学习笔记][week6]0/1 Knapsack Problem
问题描述 具体的问题描述请参考以下链接: [Algorithmic Toolbox学习笔记][week3]战利品的最大价值_Karen_AMPM的博客-CSDN博客假设小偷有一个背包只能放下一定重量的 ...
- 【 FZU - 2214 】Knapsack problem(逆向0-1背包)
题干: Given a set of n items, each with a weight w[i] and a value v[i], determine a way to choose the ...
最新文章
- linux ssh登录失败
- Java程序员从笨鸟到菜鸟之(二十九)javascript对象的创建和继承实现
- 20181120-1 每周例行报告
- 如何在线将pdf转换成ppt格式
- C# http://xamarin.com/
- 【网站开发】搭建一个属于自己的网站
- haproxy代理设置及配置文件详解
- 基于CC1310的915MHz硬件设计
- python中的zip是什么意思_Python的zip()函数是什么
- 洛谷 P5194 [USACO05DEC]Scales S(DFS)
- php+5.3.15下载,Rapid PHP2018
- php7单独运行,如何让PHP 7运行更加神速
- error: implicit declaration of function ‘read‘ [-Werror,-Wimplicit-function-declaration]
- ubuntu20.04+vtd环境搭建
- 计算机表格画斜线,word表格斜线_Word2010怎么绘制斜线表头-太平洋IT百科手机版...
- c++求余弦的泰勒展开式
- Air202入坑指南2---LED闪烁
- 美国南加州大学骆沁毅:构建高性能的异构分布式训练算法
- 利用RNAi研究马铃薯基因功能——StRIK与外表皮应激反应
- java switch的意思_java switch
热门文章
- UML--9种图及关系
- Error:org.gradle.api.internal.changedetection.state.FileCollectionSnapshotImpl cannot be cast to org
- 原创 | OpenAPI 标准规范
- 面试官绝杀:系统是如何支撑高并发的?
- 分享一道阿里Java并发面试题 (详细分析篇)
- 分库分表就能无限扩容吗,解释得太好了
- hashcode()和hash()
- 成为Apache顶级项目核心贡献者是一种什么样的体验?
- 一文告诉你 Java RMI 和 RPC 的区别!
- 青少年蓝桥杯_2020_steam考试_初级组_第一题