当前位置: 首页 > news >正文

0/1背包问题

例题HDU-2602

Problem Description
Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?
Input
The first line contain a integer T , the number of cases.
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.
Output
One integer per line representing the maximum of the total value (this number will be less than 231).
Sample Input
1
5 10
1 2 3 4 5
5 4 3 2 1
Sample Output
14

二维数组dp[][]

以下是代码

#include<vector>
#include<iostream>
using namespace std;int maxBoneValue(int n,int V,vector<int>& value,vector<int>& volumes)
{vector<vector<int>> dp(n+1,vector<int>(V+1,0));//i表示前i个骨头for(int i = 1;i<=n;i++){//j代表当前背包的容量,当j=0时,意味着背包的容量为0//dp[i][0]都是0,当我们数组初始化时,已经隐含了j=0的情况,所以从1开始for(int j= 1;j<=V;j++){//不选这个骨头,如果装不下if(j<volumes[i-1]){//这个判断 j < volumes[i-1] 是为了检查当前的背包容量 j 是否足够来装下第 i 个骨头。//volumes[i-1] 是第 i 个骨头的体积(因为数组是从0开始的,所以第 i 个骨头的体积在 volumes 数组中的索引是 i-1)    //j代表当前背包容量  dp[i][j] = dp[i-1][j];}else{//考虑这个骨头情况dp[i]
http://www.lryc.cn/news/180717.html

相关文章:

  • Redis入门到精通——00数据类型
  • PADS9.5使用记录
  • Axios post请求出现500错误
  • 【Leetcode】171.Excel 表列序号
  • 2023湖南省赛游记/题解
  • 海信电视U8KL使用体验:参数卷,画质技术也独有!
  • E. Mishap in Club
  • UE4 自带体积云应用
  • RTP/RTCP 协议讲解
  • 倒计时15天!百度世界2023抢先看
  • Redis 哈希(Hash)数据类型和命令(数据类型 二)
  • [Linux]线程互斥
  • leetcode-239-滑动窗口最大值
  • 基于大语言模型的智能问答系统应该包含哪些环节?
  • 【Cesium创造属于你的地球】相机系统
  • 运维困局下确保系统稳定的可行性
  • springmvc中DispatcherServlet关键对象
  • 某微e-office协同管理系统存在任意文件读取漏洞复现 CNVD-2022-07603
  • 消息驱动 —— SpringCloud Stream
  • 使用Apache HttpClient爬取网页内容的详细步骤解析与案例示例
  • 传输层协议—UDP协议
  • 【改造中序遍历】 538. 把二叉搜索树转换为累加树
  • 2022年11月工作经历
  • 使用广播信道的数据链路层
  • 第3章-指标体系与数据可视化-3.1.2-Seaborn绘图库
  • excel中将一个sheet表根据条件分成多个sheet表
  • 案例突破——再探策略模式
  • uboot启动流程-涉及lowlevel_init汇编函数
  • 质数距离 - 如何在较合理的时间复杂度内求2e9范围内的质数
  • 八、3d场景的区域光墙