完全背包变形
题目链接
思路
每天手里的钱作为背包容量,商品今天的价格作为体积,明天的价格作为价值,设 d p [ i ] dp[i] dp[i]为花费达到 i i i元时最大的净利润
ACcode
#include<bits/stdc++.h>
using namespace std;
const int M = 2e4 + 9;
int dp[M], w[M][M];
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t, n, m;cin >> t >> n >> m;
for (int i = 1;i <= t;i++)
for (int j = 1;j <= n;j++)
cin >> w[j][i];
for (int i = 1;i < t;i++) {
for (int p = 0;p < M;p++)dp[p] = 0;
for (int j = 1;j <= n;j++) {
for (int k = w[j][i];k <= m;k++) {
dp[k] = max(dp[k], dp[k - w[j][i]] + w[j][i + 1] - w[j][i]);
}
}
m += dp[m];
}
cout << m;
return 0;
}