当前位置:网站首页>多重背包!
多重背包!
2022-07-23 08:03:00 【ATTACH_Fine】
有N种物品和一个容量为V 的背包。第i种物品最多有Mi件可用,每件耗费的空间是Ci ,价值是Wi 。求解将哪些物品装入背包可使这些物品的耗费的空间 总和不超过背包容量,且价值总和最大。

毫无区别,这就转成了一个01背包问题了,且每个物品只用一次。
代码
public void testMultiPack1(){
// 版本一:改变物品数量为01背包格式
List<Integer> weight = new ArrayList<>(Arrays.asList(1, 3, 4));
List<Integer> value = new ArrayList<>(Arrays.asList(15, 20, 30));
List<Integer> nums = new ArrayList<>(Arrays.asList(2, 3, 2));
int bagWeight = 10;
for (int i = 0; i < nums.size(); i++) {
while (nums.get(i) > 1) {
// 把物品展开为i
weight.add(weight.get(i));
value.add(value.get(i));
nums.set(i, nums.get(i) - 1);
}
}
int[] dp = new int[bagWeight + 1];
for(int i = 0; i < weight.size(); i++) {
// 遍历物品
for(int j = bagWeight; j >= weight.get(i); j--) {
// 遍历背包容量
dp[j] = Math.max(dp[j], dp[j - weight.get(i)] + value.get(i));
}
System.out.println(Arrays.toString(dp));
}
}
边栏推荐
- How about the performance of Ruilong R7 Pro 5875u? What level is it equivalent to
- Life essays: annoying projects in 2022
- Detailed explanation of knapsack problem
- rtx3080相当于gtx什么显卡 rtx3080显卡什么水平 rtx3080显卡怎么样
- Medium range
- Head products generated 2.5 billion yuan, and SLG track was also targeted by black products
- Process blocks and methods
- Best practices of JD cloud Distributed Link Tracking in financial scenarios
- 笔记本酷睿i5 1135g7相当于什么水平?i5 1135g7性能怎么样
- iQOO 10 Pro和vivo X80 Pro区别 哪个好详细参数配置对比
猜你喜欢

鸡与蛋,产品与策略

rtx3080ti和3090差距 rtx3080ti和3090哪个性价比高

Principle of container network

Notes on the fourth day

What is Tianji 920 equivalent to a snapdragon? How much is Tianji 920 equivalent to a snapdragon? How about Tianji 920

BGP federal experiment

antd form表单——重置方法不生效——基础积累——prop的重要性

Day 8 notes

Configure the firetracker process, i.e. stepping on the pit record

背包问题详解
随机推荐
OSPF comprehensive experiment
Day 10 notes
JS to obtain age through ID card
第五天筆記
设计例化和连接
How to open the thought map pdf of postgraduate entrance examination in the small program of postgraduate entrance examination question bank
Comment creo 9.0 modifie - t - il rapidement le système de coordonnées Cao?
What level is the Core i7 1165g7 equivalent to? What grade does the i71165g7 belong to
求岛屿最大面积--深度优先搜索(染色法)
Notes on animal farm
Renforcement de l'apprentissage - points de compréhension du gradient stratégique
200 lines of code, in-depth analysis of the principle and implementation of dynamic calculation diagram
赛扬n5095处理器怎么样 英特尔n5095核显相当于什么水平
天玑1100相当于骁龙多少处理器 天玑1100相当于骁龙多少 天玑1100怎么样
拖拽----
How about the performance of Intel Celeron 7305? What level is it equivalent to
第七天笔记
Tensor、Numpy、PIL格式转换以及图像显示
What level is the notebook core i5 1135g7 equivalent to? How about i5 1135g7 performance
背包问题详解