当前位置:网站首页>0-1背包问题
0-1背包问题
2022-07-07 21:52:00 【一只小小狗】
0 - 1 背包问题
1. 寻找最大价值
标准的01背包
装满最大价值是多少?
- 暴力解法就是dps深度回溯算法(装入背包 和不装背包)
- 非暴力使用dp方程进行动态递推
1.1 定义dp数组 dp[i][j]
i 表示列长,也就是出现的不同物品
j 表示行长,但是此时的行,为装了不同重量的可能性,也就是说0没装也是有可能的,所以(j = 最大重量 + 1)
而对应的dp[i][j] 里存放的是 价值。

1.2 二维数组实现
import java.util.*;
/** * @author JMWANG */
import java.io.*;
class Good {
//价值
int value;
//重量
int weight;
Good(int v, int p) {
this.value = v;
this.weight = p;
}
Good() {
}
public void setValue(int v) {
this.value = v;
}
public void setWeight(int p) {
this.weight = p;
}
}
public class Main {
public static void main(String[] args) throws IOException {
//最大背包重量
int n = 4;
//构建物品
List listGood = new ArrayList();
listGood.add(new Good(15, 1));
listGood.add(new Good(20, 3));
listGood.add(new Good(30, 4));
//dp递推方程构建
int[][] goods = new int[listGood.size()][n + 1];
// for (int i = 0; i < goods.length; i++) {
// System.out.println(Arrays.toString(goods[i]));
// }
dp(n, goods, listGood);
System.out.println(goods[listGood.size() - 1][n]);
}
public static void dp(int N, int[][] dp, List<Good> listGood) {
//上下层关系构建
//dp[i][j] = max(listGood.get(i),)
for (int i = 0; i < listGood.size(); i++) {
for (int j = 0; j < N + 1; j++) {
//初始化dp方程
if (j == 0) {
dp[i][j] = 0;
continue;
}
if (i == 0) {
dp[i][j] = listGood.get(i).weight <= N ? listGood.get(i).value : 0;
continue;
}
//如果当前 重量 j < 当前物品的重量,就直接将上一层重量下传
if (j < listGood.get(i).weight) dp[i][j] = dp[i - 1][j];
//如果当前 重量 j >= 当前物品的重量 ,就需要比较
//是垂直上一层价值更大 还是 左边+上当前物品价值更大
else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - listGood.get(i).weight] + listGood.get(i).value);
}
LookDP(dp);
System.out.println("______________________________");
}
}
}
public static void LookDP(int[][] goods) {
for (int i = 0; i < goods.length; i++) {
for (int j = 0; j < goods[i].length; j++) {
System.out.print(goods[i][j] + " ");
}
System.out.println();
}
}
}
1.3 一维数组实现
public static void testWeightBagProblem(int N, List<Good> listGood){
int len = listGood.size();
//定义dp数组,dp[j]表示背包容量为j时,能够获得的最大价值
int[] dp = new int[N +1];
//遍历顺序: 先遍历物品,再遍历背包容量
for(int i=0;i<len;i++){
for(int j=N;j>=listGood.get(i).weight;j--){
System.out.println(Arrays.toString(dp));
dp[j] = Math.max(dp[j], dp[j-listGood.get(i).weight]+listGood.get(i).value);
}
}
System.out.println(Arrays.toString(dp));
System.out.println(dp[N]);
}
如有错误欢迎指正
边栏推荐
- Entity层、DAO层、Service层、Controller层 先后顺序
- 七月第一周
- 系统架构设计师备考经验分享:论文出题方向
- Puce à tension stabilisée LDO - schéma de bloc interne et paramètres de sélection du modèle
- 三问TDM
- 系统设计概述
- V-for traversal object
- How can we make money by making video clips from our media?
- USB (XIV) 2022-04-12
- The 19th Zhejiang Provincial Collegiate Programming Contest 2022浙江省赛 F.EasyFix 主席树
猜你喜欢

S2b2b mall solution of intelligent supply chain in packaging industry: opening up a new ecosystem of e-commerce consumption

LeeCode -- 6. Z 字形变换

ROS2专题(03):ROS1和ROS2的区别【02】

Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid

Design and implementation of spark offline development framework

Markdown

B / Qurt Utilisateur Guide (36)

深入理解Mysql锁与事务隔离级别

Description of longitude and latitude PLT file format

Adults have only one main job, but they have to pay a price. I was persuaded to step back by personnel, and I cried all night
随机推荐
HDU 4747 Mex「建议收藏」
Conversion between commonsmultipartfile and file
CAIP2021 初赛VP
Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid
B_ QuRT_ User_ Guide(36)
One week learning summary of STL Standard Template Library
Puce à tension stabilisée LDO - schéma de bloc interne et paramètres de sélection du modèle
Cloud native is devouring everything. How should developers deal with it?
FPGA basics catalog
The 19th Zhejiang Provincial Collegiate Programming Contest VP记录+补题
2021icpc Shanghai h.life is a game Kruskal reconstruction tree
UE4_ Ue5 panoramic camera
Design and implementation of spark offline development framework
系统架构设计师备考经验分享:论文出题方向
云原生数据仓库AnalyticDB MySQL版用户手册
深入理解Mysql锁与事务隔离级别
力扣解法汇总648-单词替换
建筑建材行业SRM供应商云协同管理平台解决方案,实现业务应用可扩展可配置
Unity3d learning notes 5 - create sub mesh
Matlab 信号处理【问答随笔·2】