public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int w = sc.nextInt(); int n = sc.nextInt(); int[][] dp = new int[n+1][w+1]; int[] weight = new int[n+1]; int[] value = new int[n+1]; for (int i = 1; i <=n ; i++) { weight[i] = sc.nextInt(); value[i] = sc.nextInt(); } // 若是比第i个物品在j 的空间下放不下 就取前一个物品的最大值 填充 ,若放得下 就比较放于不放 for (int i = 1; i <=n ; i++) { for (int j = 1; j <=w ; j++) { if(weight[i] > j){ dp[i][j] = dp[i-1][j]; }else{ //变数在这里 dp递推方程 int v1 = dp[i-1][j]; int v2 = dp[i-1][j-weight[i]] + value[i]*weight[i]; dp[i][j] = Math.max(v1,v2); } } } System.out.println(dp[n][w]); } }
2.一维空间优化版
空间优化0-1背包: f[j] = max(f[j] , f[j−w[i]] + v[i])
1 2 3 4
for (int i = 1; i <= n; i++) for (int j = W; j >= w[i]; j--) f[j] = max(f[j], f[j - w[i]] + v[i]); printf("%d\n", f[W]);
import java.util.Scanner; public class P1616 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int m = sc.nextInt(); int[] dp = new int[t+1]; int[] weight = new int[m+1]; int[] value = new int[m+1]; for (int i = 1; i <=m ; i++) { weight[i] = sc.nextInt(); value[i] = sc.nextInt(); } for (int i = 1; i <=m ; i++) { for (int j = weight[i]; j <=t; j++) { dp[j] = Math.max(dp[j],dp[j-weight[i]] + value[i]); } } System.out.println(dp[t]); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int v = sc.nextInt(); int[] weight = new int[n+1]; int[] values = new int[n+1]; int[] counts = new int[n+1]; for (int i = 0; i <n ; i++) { weight[i] = sc.nextInt(); values[i] = sc.nextInt(); counts[i] = sc.nextInt(); } int[] dp = new int[v+1]; for (int i = 0; i <n ; i++) { for (int j = v; j >= 0 ; j--) { for (int k = 1; k<=counts[i] && k*weight[i] <= j ; k++) { dp[j] = Math.max(dp[j],dp[j-k*weight[i]]+k*values[i]); } } } System.out.println(dp[v]); } }
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int w = sc.nextInt(); List<Pair<Integer,Integer>> goods = new ArrayList<>(); for (int i = 0; i <n ; i++) { int weight = sc.nextInt(); int value = sc.nextInt(); int count = sc.nextInt(); for (int j = 1; j <=count ; j*=2) { count -= j; goods.add(new Pair<>(weight*j,value*j)); } if(count > 0) goods.add(new Pair<>(count*weight,count*value)); }
int[] dp = new int[w+1]; for (Pair<Integer,Integer> good:goods) { for (int j = w; j >=good.getKey() ; j--) { dp[j] = Math.max(dp[j],dp[j-good.getKey()]+good.getValue()); } } System.out.println(dp[w]); }
import java.util.Scanner; public class 二维费用背包 { // 在重量的基础上 限制体积 public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int w = sc.nextInt(); // 重量 int v = sc.nextInt(); // 体积 int[][] dp = new int[w+1][v+1]; for (int i = 0; i <n ; i++) { int weight = sc.nextInt(); int volume = sc.nextInt(); int value = sc.nextInt(); for (int j = w; j >=weight ; j--) { for (int k = v; k >=volume ; k--) { dp[j][k] = Math.max(dp[j][k],dp[j-weight][k-volume]+value); } } } System.out.println(dp[w][v]); } }