当前位置:网站首页>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]);
}
如有错误欢迎指正
边栏推荐
- As a new force, chenglian premium products was initially injected, and the shares of relevant listed companies rose 150% in response
- 统计电影票房排名前10的电影并存入还有一个文件
- VS扩展工具笔记
- CAIP2021 初赛VP
- Matlab SEIR infectious disease model prediction
- Conversion between commonsmultipartfile and file
- SQL database execution problems
- Count the top 10 films at the box office and save them in another file
- Happy gathering time
- USB(十四)2022-04-12
猜你喜欢

移动端异构运算技术 - GPU OpenCL 编程(基础篇)

Unity3D学习笔记6——GPU实例化(1)

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

As a new force, chenglian premium products was initially injected, and the shares of relevant listed companies rose 150% in response

2022 Season 6 perfect children's model Shaanxi finals came to a successful conclusion

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

Spark 离线开发框架设计与实现

13、 System optimization

Live-Server使用

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
随机推荐
How to generate unique file names
S2b2b mall solution of intelligent supply chain in packaging industry: opening up a new ecosystem of e-commerce consumption
产业共融新势能,城链科技数字峰会厦门站成功举办
Dynamic agent explanation (July 16, 2020)
LDO稳压芯片-内部框图及选型参数
Description of longitude and latitude PLT file format
New potential energy of industrial integration, Xiamen station of city chain technology digital summit successfully held
Digital procurement management system for fresh food industry: help fresh food enterprises solve procurement problems and implement online procurement throughout the process
Matlab 信号处理【问答随笔·2】
The 19th Zhejiang Provincial College Programming Contest 2022 f.easyfix chairman tree
深入理解Mysql锁与事务隔离级别
Windows set redis to start automatically
The 19th Zhejiang Provincial Collegiate Programming Contest VP记录+补题
FPGA basics catalog
UE4_UE5蓝图command节点的使用(开启关闭屏幕响应-log-发布全屏显示)
B_QuRT_User_Guide(37)
B_QuRT_User_Guide(39)
PCI-Express接口的PCB布线规则
系统设计概述
js 获取对象的key和value