洛谷P1507 NASA的食物计划 题解 二维费用背包

题目链接:https://www.luogu.com.cn/problem/P1507

解题思路:二维费用背包模板题。

示例代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 505;
int v1, v2, n, f[maxn][maxn];
void pack(int c1, int c2, int v) {
    for (int i = v1; i >= c1; i --)
        for (int j = v2; j >= c2; j --)
            f[i][j] = max(f[i][j], f[i-c1][j-c2] + v);
}
int main() {
    cin >> v1 >> v2 >> n;
    while (n --) {
        int c1, c2, v;
        cin >> c1 >> c2 >> v;
        pack(c1, c2, v);
    }
    cout << f[v1][v2] << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/quanjun/p/13638416.html