package eg.nk_mt; import java.util.Scanner; /** * 多重背包 * 多重和完全更接近,多了数量的限制,用一个count[n]计数数组来限制物品i的数量。 * 当放入第i个物品是较优值的时候,count[i]=count[j-weight[i]]+1(j 的含义:); * 这样做是因为,放入第i个物品的操作是基于count[j-weight[i]]放入的, * 所以当count[i-weight[i]]>=amount[i]时,就要阻止放入即便放入第i个物品是较优值 */ public class backpack03 { public static void main(String[] args) { /* Scanner in = new Scanner(System.in); int n = in.nextInt();//物品种类个数 int [] weight = new int[n];//重量 int [] value = new int[n];//价值 for (int i = 0; i < n; i++) { weight[i] = in.nextInt(); } for (int i = 0; i < n; i++) { value[i] = in.nextInt(); } int target = in.nextInt();//背包容量*/ int n = 5; int [] weight = {5,4,7,2,6}; int [] value = {12,3,10,3,6}; int target = 15; int [] account = {2,4,6,8,1};//每种物品的数量 int[] arr = new int[target + 1]; //依次轮转,把当背包中有一个、二个、三个,,, for (int i = 0; i < n; i++) { // i 表示背包中可以放前(i + 1) 种物品 // 完全背包问题 if(weight[i] * account[i] >= target){ for (int j = weight[i]; j <= target; j++) { arr[j] = Math.max(arr[j],arr[j - weight[i]] + value[i]); } return; } //01背包问题,并使用二进制方法进行优化 int tmpCount = 1; while (tmpCount < account[i]){ for (int j = target; j >= tmpCount * weight[i]; j--) { // j 表示当前背包的容量 arr[j] = Math.max(arr[j],arr[j - tmpCount * weight[i]] + tmpCount * value[i]); } account[i] -= tmpCount; tmpCount = tmpCount << 1; } for (int j = target; j >= weight[i]; j--) { // j 表示当前背包的容量 arr[j] = Math.max(arr[j],arr[j - weight[i]] + value[i]); } } for (int i = 0; i <= target; i++) { System.out.print(arr[i] + " "); } } }
多重背包 java实现
最新推荐文章于 2024-08-04 11:07:52 发布