二维费用的背包

模板题

AcWing-8-二维费用的背包open in new window

#include <iostream>
using namespace std;

const int N = 1010, T = 110;
struct node {
    int v, m, w;
};
int n, V, M;
node a[N];
int f[T][T];

int main () {
    cin >> n >> V >> M;
    for (int i = 1; i <= n; ++ i)
        cin >> a[i].v >> a[i].m >> a[i].w;
    
    for (int i = 1; i <= n; ++ i) {
        int v = a[i].v, m = a[i].m, w = a[i].w;
        for (int j = V; j >= v; -- j)
            for (int k = M; k >= m; -- k)
                f[j][k] = max(f[j][k], f[j - v][k - m] + w);
    }
    cout << f[V][M] << endl;
    return 0;
}
最后修改于: