1. 0-1背包问题
w
[
i
]
w[i]
w[i]为第
i
i
i个物品的重量,
v
[
i
]
v[i]
v[i]为第
i
i
i个物品的价值,每种物品只有一个
d
p
[
i
]
[
j
]
=
M
a
t
h
.
m
a
x
(
d
p
[
i
−
1
]
[
j
]
,
v
[
i
]
+
d
p
[
i
−
1
]
[
j
−
w
[
i
]
]
)
dp[i][j] = Math.max(dp[i-1][j], v[i] + dp[i-1][j-w[i]])
dp[i][j]=Math.max(dp[i−1][j],v[i]+dp[i−1][j−w[i]])
class Pack01 {
public int knapSack(int[] w, int[] v, int cap) { //未优化
int num = w.length;
if (num == 0 || cap == 0)
return 0;
int[][] dp = new int[num][cap+1];
for (int i = 0; i <= cap; i++) {
dp[0][i] = w[0]<=i ? v[0] : 0; //先初始化放入第一个物品
}
for (int i = 1; i < num; i++) {
for (int j = 0; j <=cap ; j++) {
dp[i][j] = dp[i-1][j]; //默认为不放当前第i个物品
if (w[i] <= j)
dp[i][j] = Math.max(dp[i][j], v[i] + dp[i-1][j-w[i]]);
}
}
return dp[num-1][cap];
}
public int youhua(int[] w, int[] v, int cap) { //优化后
int num = w.length;
int[] dp = new int[cap+1];
for (int i = 0; i < num; i++) {
for (int j = cap; j >= w[i]; j--) { //从最后一个背包开始装
dp[j] = Math.max(dp[j], dp[j-w[i]] + v[i]);
}
}
return dp[cap];
}
public static void main(String[] args) {
int[] w = {2, 1, 3, 2};
int[] v = {12, 10, 20, 15};
Pack01 pack01 = new Pack01();
System.out.println(pack01.knapSack(w, v, 5));
System.out.println(pack01.youhua(w, v, 5));
}
}
2. 完全背包问题
w
[
i
]
w[i]
w[i]为第
i
i
i个物品的重量,
v
[
i
]
v[i]
v[i]为第
i
i
i个物品的价值,每种物品的数量无限。
d
p
[
i
+
1
]
[
j
]
=
M
a
t
h
.
m
a
x
(
d
p
[
i
+
1
]
[
j
]
,
d
p
[
i
]
[
j
−
k
∗
w
[
i
]
]
+
k
∗
v
[
i
]
)
;
dp[i+1][j]=Math.max(dp[i+1][j],dp[i][j - k * w[i]] + k * v[i]);
dp[i+1][j]=Math.max(dp[i+1][j],dp[i][j−k∗w[i]]+k∗v[i]);其中
0
<
=
k
<
=
j
/
w
[
i
]
0<=k<=j/w[i]
0<=k<=j/w[i]为第
i
i
i种物品取的个数。
class Packwq {
public int wqpack(int[] w, int[] v, int cap) {
//dp[i][j] = max{dp[i-1][t - w[i] * k] + v[i] * k}; (0 <= k * w[i] <= j)
int[][] dp = new int[v.length+1][cap+1];
for(int i = 0; i < v.length; i++) {
for(int j = 0; j <= cap; j++) {
for(int k=0; k * w[i] <= j; k++)
dp[i+1][j]=Math.max(dp[i+1][j],dp[i][j - k * w[i]] + k * v[i]);
}
}
return dp[v.length][cap];
}
public int youhua(int[] w, int[] v, int cap) { //优化后的算法
//dp[j] = max(dp[j], dp[j-w[i]]+v[i])
int[] dp = new int[cap+1];
for (int i = 0; i < v.length; i++) {
for (int j = w[i]; j <= cap; j++) {
dp[j] = Math.max(dp[j], dp[j-w[i]] + v[i]);
}
}
return dp[cap];
}
public static void main(String[] args) {
int[] w = {2, 1, 3, 2};
int[] v = {12, 10, 20, 15};
Packwq packwq = new Packwq();
System.out.println(packwq.wqpack(w, v, 5));
System.out.println(packwq.youhua(w, v, 5));
}
}
3. 多重背包问题
类似于完全背包,只不过多一个判断条件 k < = m [ i ] k<=m[i] k<=m[i], k k k为第 i i i个物品放入的数量。
class Packdc {
public int dcpack(int[] w, int[] v, int[] m, int cap) {
int[][] dp = new int[v.length+1][cap+1];
for (int i = 0; i < v.length; i++) {
for (int j = 0; j <= cap; j++) {
for (int k = 0; k<=m[i] && k*w[i]<=j ; k++) {
dp[i+1][j] = Math.max(dp[i+1][j], dp[i][j - k * w[i]] + k * v[i]);
}
}
}
return dp[v.length][cap];
}
public int youhua(int[] w, int[] v, int[] m, int cap) {
int[] dp = new int[cap+1];
for (int i = 0; i < v.length; i++) {
int k = 1;
for (int j = w[i]; k<=m[i] && j<=cap ; j++) {
dp[j] = Math.max(dp[j], dp[j-w[i]] + v[i]);
k++;
}
}
return dp[cap];
}
public static void main(String[] args) {
int[] w = {2, 1, 3, 2};
int[] v = {12, 10, 20, 15};
int[] m = {2, 3, 3, 6};
Packdc packdc = new Packdc();
System.out.println(packdc.dcpack(w, v, m, 5));
System.out.println(packdc.youhua(w, v, m, 5));
}
}
4. 最少货币问题
arr为货币构成数组,aim为目标钱数,构建dp[arr.length][aim+1],dp[0…arr.length-1][0] === 0; 找不开的都为Integer.MAX_VALUE,先放第一行。
其中
d
p
[
i
]
[
j
]
=
M
a
t
h
.
m
i
n
(
d
p
[
i
−
1
]
[
j
]
,
d
p
[
i
]
[
j
−
a
r
r
[
i
]
]
+
1
]
)
dp[i][j] = Math.min(dp[i-1][j], dp[i][j-arr[i]]+1])
dp[i][j]=Math.min(dp[i−1][j],dp[i][j−arr[i]]+1])
public class MinMoney {
public int minConins1(int[] arr, int aim){ //未优化
if(arr == null || arr.length == 0 || aim < 0){
return -1;
}
int[][] dp = new int[arr.length][aim+1];
int max = Integer.MAX_VALUE;
//设置第一行
for(int j=1; j <= aim; j++){
dp[0][j] = max;
if (j % arr[0] == 0) {
dp[0][j] = j / arr[0];
}
// if(j-arr[0] >= 0 && dp[0][j-arr[0]] != max ){
// dp[0][j] = dp[0][j-arr[0]] + 1;
// }
}
int left = 0;
for(int i=1; i < arr.length; i++){
for(int j=1; j <=aim; j++){
left = max;
if(j-arr[i] >=0 && dp[i][j-arr[i]] != max){
left = dp[i][j-arr[i]] + 1;
}
dp[i][j] = Math.min(left, dp[i-1][j]);
}
}
return dp[arr.length-1][aim] != max ? dp[arr.length-1][aim] : -1;
}
public int youhua(int[] arr, int aim) { //优化后
if (arr == null || arr.length == 0)
return -1;
int[] dp = new int[aim+1];
int max = Integer.MAX_VALUE;
//先放第一行
for (int i = 1; i <= aim; i++) {
dp[i] = max;
if (i % arr[0] == 0)
dp[i] = i / arr[0];
}
int tmp = max;
for (int i = 1; i < arr.length; i++) {
for (int j = 1; j <= aim; j++) {
tmp = max;
if (j - arr[i] >= 0 && dp[j-arr[i]] != max)
tmp = dp[j-arr[i]] + 1;
dp[j] = Math.min(dp[j], tmp);
}
}
return dp[aim];
}
public static void main(String[] args) {
int[] arr = {5,2,3,1};
int aim = 5;
MinMoney m = new MinMoney();
System.out.println(m.youhua(arr, aim));
}
}