ac 5 多种背包问题

输入是

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

实际上把他边成

4 5
1 2 1
2 4 1
2 4 1
3 4 1
6 8 1
4 5 1
4 5 1

全部是一就变成01背包问题了

#include<bits/stdc++.h>
using namespace std;

const int N = 25000, M = 010;

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

int main() {
    cin >> n >> m;
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        int a, b, s;
        cin >> a >> b >> s;
        int k = 1;
        while(k <= s) {
            cnt ++;
            v[cnt] = a * k;   //体积
            w[cnt] = b * k;   //价值
            s -= k;
            k *= 2;
        }
        if (s > 0) {
            cnt ++;
            v[cnt] = a * s;
            w[cnt] = b * s;
        }
    }
    
    n = cnt;
    for (int i = 0; i <= n; i++) {
        for (int j = m; j >= v[i]; j--) {
            f[j] = max(f[j], f[j - v[i]] + w[i]);
        }
    }
    cout << f[m] << endl;
    return 0;
}
posted @ 2022-10-27 19:12  天然气之子  阅读(13)  评论(0编辑  收藏  举报