当前位置:网站首页>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]);
}
如有错误欢迎指正
边栏推荐
- Markdown
- 树后台数据存储(採用webmethod)[通俗易懂]
- Adrnoid Development Series (XXV): create various types of dialog boxes using alertdialog
- 系统设计概述
- USB (XVII) 2022-04-15
- How to login and enable synchronization function in Google browser
- [microservices SCG] gateway integration Sentinel
- UE4_ Use of ue5 blueprint command node (turn on / off screen response log publish full screen display)
- Explain
- Turbo introder common scripts
猜你喜欢
UE4_UE5结合罗技手柄(F710)使用记录
进度播报|广州地铁七号线全线29台盾构机全部完成始发
LeeCode -- 6. Z 字形变换
2021icpc Shanghai h.life is a game Kruskal reconstruction tree
Vulnerability recurrence ----- 49. Apache airflow authentication bypass (cve-2020-17526)
高效的S2B2C电商系统,是这样帮助电子材料企业提升应变能力的
UE4_ Use of ue5 blueprint command node (turn on / off screen response log publish full screen display)
Oracle database backup and recovery
B_ QuRT_ User_ Guide(37)
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
随机推荐
Install a new version of idea. Double click it to open it
MySQL Index Optimization Practice II
Happy gathering time
Unity3d learning notes 5 - create sub mesh
Spark 离线开发框架设计与实现
Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
The 19th Zhejiang Provincial Collegiate Programming Contest VP记录+补题
系统架构设计师备考经验分享:论文出题方向
ROS2专题(03):ROS1和ROS2的区别【02】
城联优品作为新力量初注入,相关上市公司股价应声上涨150%
B_QuRT_User_Guide(37)
三问TDM
Turbo introder common scripts
Force deduction solution summary 648 word replacement
HDU 4747 mex "recommended collection"
648. Word replacement
在软件工程领域,搞科研的这十年!
Caip2021 preliminary VP
Puce à tension stabilisée LDO - schéma de bloc interne et paramètres de sélection du modèle
Conversion between commonsmultipartfile and file