混合背包

https://www.acwing.com/problem/content/7/

混合背包

多重背包用二进制优化.

#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PII pair<int, int>
#define VIT vector<int>
#define x first
#define y second
#define inf 0x3f3f3f3f
int f[1010];
 
int main() {
    IO;
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        int v, w, s;
        cin >> v >> w >> s;
        if (!s) for (int j = v; j <= m; ++j) f[j] = max(f[j], f[j - v] + w);
        else {
            if (s == -1) s = 1;
            int k = 1;
            while (k < s) {
                for (int j = m; j >= k * v; --j) f[j] = max(f[j], f[j - k * v] + k * w);
                s -= k;
                k <<= 1;
            }
            if (s) for (int j = m; j >= s * v; --j) f[j] = max(f[j], f[j - s * v] + s * w);
        }
    }
    cout << f[m] << '\n';
    return 0;
}

posted @ 2021-04-13 19:15  phr2000  阅读(40)  评论(0编辑  收藏  举报