动态规划-分组背包问题

 

 

#include <iostream>
using namespace std;

const int N = 110;

int v[N][N], w[N][N];
int s[N], f[N];
int n, m;

int main()
{
    cin >> n >> m;
    
    for (int i = 1; i <= n; ++ i)
    {
        cin >> s[i];
        for (int j = 0; j < s[i]; ++ j)
            cin >> v[i][j] >> w[i][j];
    }
    
    for (int i = 1; i <= n; ++ i)
        for (int j = m; j >= 0; -- j)
            for (int k = 0; k < s[i]; ++ k)
                if (v[i][k] <= j)   // 第i组的第k件物品大于j,更新
                    f[j] = max(f[j], f[j - v[i][k]] + w[i][k]);
                    
    cout << f[m] << endl;
    
    return 0;
}

 

posted @ 2021-08-29 17:21  青衫客36  阅读(28)  评论(0编辑  收藏  举报