HDOJ 1712 ACboy needs your help(分组背包入门)
题意:
给出课程 i 所付出时间 j 所取得的收益,求最大收益。
思路:
分组背包入门:dp[i][j] = max(dp[i-1][j-k] + valk) dp[i][j] 代表前 i 种物品付出时间 j 所得到的最大收益。
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 110;
const int MAXD = 110;
int dp[MAXD], A[MAXN];
int main()
{
int n, m;
while (scanf("%d %d", &n, &m) && n && m)
{
memset(dp, 0, sizeof(dp));
for (int i = 0; i < n; ++i)
{
for (int j = 1; j <= m; ++j)
scanf("%d", &A[j]);
for (int v = m; v > 0; --v)
for (int j = 0; j <= v; ++j)
dp[v] = max(dp[v], dp[v - j] + A[j]);
}
int ret = 0;
for (int v = 1; v <= m; ++v)
ret = max(ret, dp[v]);
printf("%d\n", ret);
}
return 0;
}
-------------------------------------------------------
kedebug
Department of Computer Science and Engineering,
Shanghai Jiao Tong University
E-mail: kedebug0@gmail.com
GitHub: http://github.com/kedebug
-------------------------------------------------------