分组背包

分组背包

#include <bits/stdc++.h>

using namespace std;
const int maxn = 110;
int n,V;
int v[maxn][maxn],w[maxn][maxn],s[maxn],dp[maxn];
int main(){
    ios::sync_with_stdio(0);
    cin >> n >> V;
    for(int i = 1; i <= n; i++) {
        cin >> s[i];
        for(int j = 1; j <= s[i]; j++)
            cin >> v[i][j] >> w[i][j];
    }
    for(int i = 1; i <= n; i++){
        for(int j = V; j >= 0;j--)
            for(int k = 1; k <= s[i]; k++)
                if(j >= v[i][k])
                    dp[j] = max(dp[j],dp[j - v[i][k]] + w[i][k]);
    }
    cout << dp[V] << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/xcfxcf/p/13338900.html