01背包问题程序源码(java)
来源:互联网 发布:淘宝没有解除手机绑定 编辑:程序博客网 时间:2024/05/18 01:18
package bagging;
public class Bag0Or1 {
public int[][] findMaxValue(int number, int weightLimit, int[] Weight,
int[] Value) {
int[][] result = new int[number + 1][weightLimit + 1];
// 初始化最后一行
for (int j = 0; j <= weightLimit; j++) {
result[number][j] = j < Weight[number - 1] ? 0 : Value[number - 1];
System.out.print(result[number][j] + ",");
}
System.out.println();
// 进行递推
for (int i = number - 1; i > 0; i--) {
for (int j = 0; j <= weightLimit; j++) {
if (Weight[i - 1] > j) {
result[i][j] = result[i + 1][j];
} else {
int a = result[i + 1][j];
int b = result[i + 1][j - Weight[i - 1]] + Value[i - 1];
if (a > b) {
result[i][j] = a;
} else {
result[i][j] = b;
}
}
System.out.print(result[i][j] + ",");
}
System.out.println();
}
return result;
}
public int[] findMaxValuePath(int number, int weightLimit, int[] Weight,
int[][] result) {
int[] re = new int[number];
int i = 1, j = weightLimit;
while (i < number && j > 0) {
if (result[i][j] == result[i + 1][j]) {
re[i - 1] = 0;
} else {
re[i - 1] = 1;
j = j - Weight[i - 1];
}
i++;
}
if (result[number][weightLimit] == 0) {
re[number - 1] = 0;
} else {
re[number - 1] = 1;
}
return re;
}
public static void main(String[] args) {
int k = 5;// 物品个数
int V[] = new int[] { 3, 5, 4, 6, 6 };// 物品价值
int W[] = new int[] { 2, 6, 5, 4, 2 };// 物品重量
int n = 10;// 限制选取物品的总重量
Bag0Or1 bag = new Bag0Or1();
int[][] result = bag.findMaxValue(k, n, W, V);//计算最大价值
System.out.println(result[1][n]);
int[] path = bag.findMaxValuePath(k, n, W, result);//计算最优解
for (int i = 0; i < path.length; i++) {
System.out.println(path[i]);
}
}
}
public class Bag0Or1 {
public int[][] findMaxValue(int number, int weightLimit, int[] Weight,
int[] Value) {
int[][] result = new int[number + 1][weightLimit + 1];
// 初始化最后一行
for (int j = 0; j <= weightLimit; j++) {
result[number][j] = j < Weight[number - 1] ? 0 : Value[number - 1];
System.out.print(result[number][j] + ",");
}
System.out.println();
// 进行递推
for (int i = number - 1; i > 0; i--) {
for (int j = 0; j <= weightLimit; j++) {
if (Weight[i - 1] > j) {
result[i][j] = result[i + 1][j];
} else {
int a = result[i + 1][j];
int b = result[i + 1][j - Weight[i - 1]] + Value[i - 1];
if (a > b) {
result[i][j] = a;
} else {
result[i][j] = b;
}
}
System.out.print(result[i][j] + ",");
}
System.out.println();
}
return result;
}
public int[] findMaxValuePath(int number, int weightLimit, int[] Weight,
int[][] result) {
int[] re = new int[number];
int i = 1, j = weightLimit;
while (i < number && j > 0) {
if (result[i][j] == result[i + 1][j]) {
re[i - 1] = 0;
} else {
re[i - 1] = 1;
j = j - Weight[i - 1];
}
i++;
}
if (result[number][weightLimit] == 0) {
re[number - 1] = 0;
} else {
re[number - 1] = 1;
}
return re;
}
public static void main(String[] args) {
int k = 5;// 物品个数
int V[] = new int[] { 3, 5, 4, 6, 6 };// 物品价值
int W[] = new int[] { 2, 6, 5, 4, 2 };// 物品重量
int n = 10;// 限制选取物品的总重量
Bag0Or1 bag = new Bag0Or1();
int[][] result = bag.findMaxValue(k, n, W, V);//计算最大价值
System.out.println(result[1][n]);
int[] path = bag.findMaxValuePath(k, n, W, result);//计算最优解
for (int i = 0; i < path.length; i++) {
System.out.println(path[i]);
}
}
}
0 0
- 01背包问题程序源码(java)
- 01背包java 源码
- 2 背包问题-Java程序
- 01背包问题-java
- Java 01背包问题
- 01背包问题 java
- 01背包问题--Java
- 01背包问题(java实现)
- 背包问题(01背包,完全背包,多重背包)
- 背包问题(01背包,完全背包,多重背包)
- 背包(01背包、完全背包、多重背包)问题总结
- 背包问题(01背包,完全背包,多重背包)
- 背包问题(01背包 + 完全背包 + 多重背包)
- 背包问题(01背包,完全背包,多重背包)
- 背包问题模板(01背包,完全背包,多重背包)
- 经典背包问题----(01背包、完全背包、多重背包)
- 背包问题(01背包和完全背包)java求解
- 背包问题(01背包和完全背包)java求解
- Odd even Linked list
- [从头学数学] 第129节 三角形 小结与复习题
- C# 静态扩展方法
- 《数据库》顺序有序表的合并
- Codeforces Round #345 (Div. 2) 总结
- 01背包问题程序源码(java)
- 校园网&openwrt记(十四) init服务方法
- 一种电池过放电保护电路
- linux 启动引导修复
- 编程格式
- HDU——2609How many(字符串的最小表示法+substr)
- 重磅消息,CMMI研究所并入ISACA!
- Android Studio 编译不通过,Unable to find optional library: org.apache.http.legacy
- if(false);