01背包

作者: 点一下我的id | 来源:发表于2020-05-29 17:48 被阅读0次

ACM题-杭电OJ2602
参考

Problem Description

Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …

The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?

Input

The first line contain a integer T , the number of cases.
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.

Output

One integer per line representing the maximum of the total value (this number will be less than 2^31).

Sample Input

1
5 10
1 2 3 4 5
5 4 3 2 1

Sample Output

14

AC代码

/**
 *    author:  admin
 *    created: 05.29.05 17:15:14
**/
# include <bits/stdc++.h>

using namespace std;
const int N = 1e3 + 5, V = 1e3 + 5;
int dp[N][N];
int value[N], volume[N];

int main() {
# ifdef ONLINE_JUDGE
# else
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
# endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T, n, v;
    cin >> T;//T组数据
    while (T--) {
        memset(dp, 0, sizeof(dp));//格式化整个dp数组,每个元素为0
        cin >> n >> v;//n个bone,总体积为v
        for (int i = 1; i <= n; ++i) {
            cin >> value[i];
        }
        for (int i = 1; i <= n; ++i) {
            cin >> volume[i];
        }
        for (int i = 1; i <= n; ++i) {//从第1件物品到第n件物品依次进行选择
            for (int j = 0; j <= v; ++j) {//从0到v容量比较
                if (volume[i] <= j) {//如果第i件物品的容量小于等于j
                    dp[i][j] = max(dp[i - 1][j],
                                   dp[i - 1][j - volume[i]] + value[i]);//比较不拿第i件物品和拿第i件物品,谁更优
                } else {
                    dp[i][j] = dp[i - 1][j];//第i件物品容量大于j,拿不了,在j容量下从第1件选到第i件物品最优解是dp[i-1][j]
                }
            }
        }
        cout << dp[n][v] << endl;//每个局部最优到整体最优
//        for (int l = 0; l <=n; ++l) {//打印比较拿或不拿过程
//            for (int i = 0; i <=v ; ++i) {
//                cerr<<dp[l][i]<<"\t";
//            }
//            cerr<<endl;
//        }
    }
    return 0;
}

例子:
5 10
1 2 3 4 5
5 4 3 2 1
二维数组

容量        0   1   2   3   4   5   6   7   8   9   10
            0   0   0   0   0   0   0   0   0   0   0   
选择第1个物品 0   0   0   0   0   1   1   1   1   1   1   
选择第2个物品 0   0   0   0   2   2   2   2   2   3   3   
选择第3个物品 0   0   0   3   3   3   3   5   5   5   5   
选择第4个物品 0   0   4   4   4   7   7   7   7   9   9   
选择第5个物品 0   5   5   9   9   9   12  12  12  12  14

每个物品在选择时,只有两种情况,拿或者不拿,所以称01两种状态背包
dp[i][j]表示第1~i个物品在j容量下的最优解

        for (int i = 1; i <= n; ++i) {//从第1件物品到第n件物品依次进行选择
            for (int j = 0; j <= v; ++j) {//从0到v容量比较
                if (volume[i] <= j) {//如果第i件物品的容量小于等于j
                    dp[i][j] = max(dp[i - 1][j],
                                   dp[i - 1][j - volume[i]] + value[i]);//比较不拿第i件物品和拿第i件物品,谁更优
                } else {
                    dp[i][j] = dp[i - 1][j];//第i件物品容量大于j,拿不了,在j容量下从第1件选到第i件物品最优解是dp[i-1][j]
                }
            }
        }

相关文章

  • 算法模板(一) 01背包,多重背包,完全背包

    01背包 多重背包 完全背包

  • 动态规划-背包问题

    01背包问题 详解:01背包问题详解链接

  • DP小结

    DP种类 线性DP 区间DP 树形DP 背包DP01背包满背包完全背包(转成01背包) 例子:线性动规:拦截导弹,...

  • java算法巩固训练day01

    01背包 完全背包 多重背包 多重背包二进制优化算法

  • 动态规划-混合、二维费用、分组背包

    混合背包 如果将01背包、完全背包、多重背包三个背包混合起来,也就是说,有的物品只可以取一次(01背包),有的物品...

  • 01-背包、完全背包、多重背包及其相关应用

    本文介绍了背包问题系列,主要包括: 【1】 01-背包及其应用【2】完全背包及其应用【3】多重背包 【1】01-背...

  • 01 01背包

  • 01背包

    题目: 有N种物品(每种物品只有一件)和一个容量为V的背包。放入第i件物品耗费的费用为Ci,得到的价值是vi,求解...

  • 01背包

    01背包的问题很早就想写了,但是因为一些意外的事情耽搁了下来今天补习一下01背包问题的计算过程。首先,01背包的问...

  • 01背包

    1. 问题 给定背包承重W,对于n件物品(每件物品重量wi,价值vi),在背包的承重范围内,可以带走的物品价值总和...

网友评论

      本文标题:01背包

      本文链接:https://www.haomeiwen.com/subject/vmlfzhtx.html