mirror of
https://gitee.com/pronting/DataStructureAndAlgorithm.git
synced 2024-12-22 12:48:56 +08:00
01背包模型及维度优化
This commit is contained in:
parent
a923b00eb5
commit
655f53b212
29
java/priv/pront/code/national/acwing/dp/bags/A2_01背包问题.java
Normal file
29
java/priv/pront/code/national/acwing/dp/bags/A2_01背包问题.java
Normal file
@ -0,0 +1,29 @@
|
||||
package priv.pront.code.national.acwing.dp.bags;
|
||||
|
||||
import java.util.Scanner;
|
||||
/*fixme 为何采用倒序遍历*/
|
||||
public class A2_01背包问题 {
|
||||
|
||||
static int n, V;
|
||||
static int[] w , v ;
|
||||
static int[] dp;
|
||||
public static void main(String[] args){
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
n = scanner.nextInt();
|
||||
V = scanner.nextInt();
|
||||
w = new int[n + 1];
|
||||
v = new int[n + 1];
|
||||
dp = new int[V + 1];
|
||||
|
||||
for(int i = 1; i <= n; i++){
|
||||
v[i] = scanner.nextInt();
|
||||
w[i] = scanner.nextInt();
|
||||
}
|
||||
for(int i = 1; i <= n; i++){
|
||||
for(int j = V; j >= v[i]; j--){
|
||||
dp[j] = Math.max(dp[j], dp[j - v[i]] + w[i]);
|
||||
}
|
||||
}
|
||||
System.out.println(dp[V]);
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package priv.pront.code.national.acwing.dp.bags;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Acwing1022_收服宠物小精灵 {
|
||||
|
||||
static int n, m, k, hit = 0;
|
||||
static int[] hurt, num;
|
||||
static int[][] dp;
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
n = scanner.nextInt(); //精灵球数量
|
||||
m = scanner.nextInt(); //皮卡丘初始的体力值
|
||||
k = scanner.nextInt(); //野生小精灵的数量
|
||||
hurt = new int[k + 1];
|
||||
num = new int[k + 1];
|
||||
dp = new int[1005][505];
|
||||
for (int i = 1; i <= k; i++) {
|
||||
num[i] = scanner.nextInt();
|
||||
hurt[i] = scanner.nextInt();
|
||||
}
|
||||
for (int i = 1; i <= k; i++) {
|
||||
for (int j = n; j >= 1; j--) { // 剩余的精灵球数量
|
||||
for (int t = m; t >= 1; k--) { // 剩余体力值
|
||||
// 捕捉
|
||||
if (j - num[i] >= 0 && t - hurt[i] >= 0) {
|
||||
if (dp[j][t] == dp[j - num[i]][t - hurt[i]]) {
|
||||
dp[j][t] = t > t - hurt[i] ? dp[j][t] : dp[j - num[i]][t - hurt[i]];
|
||||
} else {
|
||||
dp[j][t] = Math.max(dp[j][t], dp[j - num[i]][t - hurt[i]]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println(dp[n][m]);
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package priv.pront.code.national.acwing.dp.bags;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Acwing278_数字组合 {
|
||||
|
||||
static int[] dp;
|
||||
static int[] a;
|
||||
public static void main(String[] args){
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int n = scanner.nextInt();
|
||||
int m = scanner.nextInt();
|
||||
a = new int[n + 1];
|
||||
dp = new int[m + 1];
|
||||
dp[0] = 1;
|
||||
for(int i = 1; i <= n; i++) {a[i] = scanner.nextInt();}
|
||||
for(int i = 1; i <= n; i++){
|
||||
for(int j = m; j >= a[i]; j--){
|
||||
dp[j] += dp[j - a[i]];
|
||||
}
|
||||
}
|
||||
System.out.println(dp[m]);
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package priv.pront.code.national.acwing.dp.bags;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Acwing423_采药 {
|
||||
|
||||
static int T, M;
|
||||
static int[] t, w;
|
||||
static int[] dp;
|
||||
public static void main(String[] args){
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
T = scanner.nextInt(); M = scanner.nextInt();
|
||||
t = new int[M + 1];
|
||||
w = new int[M + 1];
|
||||
for(int i = 1; i <= M; i++){
|
||||
t[i] = scanner.nextInt();
|
||||
w[i] = scanner.nextInt();
|
||||
}
|
||||
dp = new int[T + 1];
|
||||
for(int i = 1; i <= M; i++){
|
||||
for(int j = T; j >= 1; j--){
|
||||
dp[j] = dp[j];
|
||||
if(j - t[i] >= 0){
|
||||
dp[j] = Math.max(dp[j], dp[j - t[i]] + w[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println(dp[T]);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user