洛谷p1507 nasa的食物计划

01背包板子题,不同之处在于有两个背包要考虑,所以加一层循环就行了
题目链接

ACcode

#include

using namespace std;

const int M = 2e4 + 9;
int dp[M][M], c[M], w[M], m[M];

int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int h, t;cin >> h >> t;
    int n;cin >> n;
    for (int i = 1;i <= n;i++)cin >> c[i] >> m[i] >> w[i];
    for (int i = 1;i <= n;i++)
        for (int j = h;j >= c[i];j--)
            for (int op = t;op >= m[i];op--)
                dp[j][op] = max(dp[j][op], dp[j - c[i]][op - m[i]] + w[i]);
    cout << dp[h][t];
    return 0;
}

你可能感兴趣的:(背包专题训练,01背包,背包dp,动态规划)