侧边栏壁纸
博主头像
GabrielxD

列車は必ず次の駅へ。では舞台は?私たちは?

  • 累计撰写 471 篇文章
  • 累计创建 108 个标签
  • 累计收到 9 条评论

目 录CONTENT

文章目录

【动态规划】01背包问题

GabrielxD
2022-11-22 / 0 评论 / 0 点赞 / 17 阅读 / 1,142 字 / 正在检测是否收录...

题目

2. 01背包问题


NN 件物品和一个容量是 VV 的背包。每件物品只能使用一次。

ii 件物品的体积是 viv_i,价值是 wiw_i

求解将哪些物品装入背包,可使这些物品的总体积不超过背包容量,且总价值最大。
输出最大价值。

输入格式

第一行两个整数,NVN,V,用空格隔开,分别表示物品数量和背包容积。

接下来有 NN 行,每行两个整数 vi,wiv_i, w_i,用空格隔开,分别表示第 ii 件物品的体积和价值。

输出格式

输出一个整数,表示最大价值。

数据范围

0<N,V10000 \lt N, V \le 1000
0<vi,wi10000\lt v_i, w_i \le 1000

输入样例

4 5
1 2
2 4
3 4
4 5

输出样例:

8

解题

方法一:动态规划

思路

思维过程:

image-20221122130303686

动态规划:

  • 状态定义:dp[i][j]dp[i][j] 表示所有只考虑前 ii 个物品,且总体积不大于 jj 的所有选法中能得到的最大价值
  • 状态转移方程:dp[i][j]=max(dp[i1][j],dp[i1][jv[i]]+w[i])dp[i][j] = \max(dp[i-1][j], dp[i-1][j - v[i]] + w[i])v[i]jv[i] \le j)。
  • 初始状态:只考虑前 00 个物品的时候没有物品可选,最大价值一定是 00

代码

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int n = (int) in.nval;
        in.nextToken();
        int c = (int) in.nval;
        int[] v = new int[n + 1],  w = new int[n + 1];
        for (int i = 1; i <= n; ++i) {
            in.nextToken();
            v[i] = (int) in.nval;
            in.nextToken();
            w[i] = (int) in.nval;
        }
        int[][] dp = new int[n + 1][c + 1];
        for (int i = 1; i <= n; ++i) {
            int vi = v[i], wi = w[i];
            for (int j = 0; j <= c; ++j) {
                dp[i][j] = dp[i - 1][j];
                if (vi <= j) dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - vi] + wi);
            }
        }
        System.out.println(dp[n][c]);
    }
}
#include <iostream>

using namespace std;

const int N = 1010;
int n, c;
int v[N], w[N], dp[N][N];

int main() {
    scanf("%d%d", &n, &c);
    for (int i = 1; i <= n; ++i) scanf("%d%d", &v[i], &w[i]);
    for (int i = 1; i <= n; ++i) {
        int vi = v[i], wi = w[i];
        for (int j = 0; j <= c; ++j) {
            dp[i][j] = dp[i - 1][j];
            if (vi <= j) dp[i][j] = max(dp[i][j], dp[i - 1][j - vi] + wi);
        }
    }
    printf("%d\n", dp[n][c]);
    
    return 0;
}

滚动数组优化

每次状态转移只用到了上一行的数据,可以进行滚动数组优化:

x ^ 1 可以把 0011, 把 1100,相当于 1 - x

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int n = (int) in.nval;
        in.nextToken();
        int c = (int) in.nval;
        int[] v = new int[n + 1],  w = new int[n + 1];
        for (int i = 1; i <= n; ++i) {
            in.nextToken();
            v[i] = (int) in.nval;
            in.nextToken();
            w[i] = (int) in.nval;
        }
        int[][] dp = new int[2][c + 1];
        int curr = 0;
        for (int i = 1; i <= n; ++i) {
            int vi = v[i], wi = w[i];
            for (int j = 0; j <= c; ++j) {
                dp[curr][j] = dp[curr ^ 1][j];
                if (vi <= j) dp[curr][j] = Math.max(dp[curr][j], dp[curr ^ 1][j - vi] + wi);
            }
            curr ^= 1;
        }
        System.out.println(dp[curr ^ 1][c]);
    }
}

滚动数组再优化

每次状态转移时都只会用到上一次比 jj 小的列,所以可以从后向前迭代用新的状态来覆盖旧状态。

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int n = (int) in.nval;
        in.nextToken();
        int c = (int) in.nval;
        int[] v = new int[n + 1],  w = new int[n + 1];
        for (int i = 1; i <= n; ++i) {
            in.nextToken();
            v[i] = (int) in.nval;
            in.nextToken();
            w[i] = (int) in.nval;
        }
        int[] dp = new int[c + 1];
        for (int i = 1; i <= n; ++i) {
            int vi = v[i], wi = w[i];
            for (int j = c; j >= vi; --j) {
                dp[j] = Math.max(dp[j], dp[j - vi] + wi);
            }
        }
        System.out.println(dp[c]);
    }
}
#include <iostream>

using namespace std;

const int N = 1010;
int n, c;
int v[N], w[N], dp[N];

int main() {
    scanf("%d%d", &n, &c);
    for (int i = 1; i <= n; ++i) scanf("%d%d", &v[i], &w[i]);
    for (int i = 1; i <= n; ++i) {
        int vi = v[i], wi = w[i];
        for (int j = c; j >= vi; --j) {
            dp[j] = max(dp[j], dp[j - vi] + wi);
        }
    }
    printf("%d\n", dp[c]);
    
    return 0;
}
0

评论区