一 问题描述
有一个背包和 4 个物品,每个物品的重量和价值都如下图所示,背包的容量 W=10,求在不超过背包容量的前提下,把哪些物品放入背包才能获得最大价值。
二 实现
package com.platform.modules.alg.alglib.p932;
import java.util.LinkedList;
import java.util.Queue;
public class P932 {
public String output = "";
public static final int N = 10;
boolean bestx[] = new boolean[N];
Goods goods[] = new Goods[N];
Integer bestp;
int W;
int n;
int sumw;
int sumv;
public String cal(String input) {
String[] line = input.split("\n");
String[] words = line[0].split(" ");
// 物品的个数和背包的容量
n = Integer.parseInt(words[0]);
W = Integer.parseInt(words[1]);
bestp = 0; // bestv 用来记录最优解
sumw = 0; // sumw 为所有物品的总重量。