HDOJ 3496 Watch The Movie(基本二维背包)
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define max(a,b) (((a) > (b)) ? (a) : (b))
const int MAXN = 105;
int t[MAXN], v[MAXN];
int dp[1005][MAXN]; // dp[i][j] 花费时间i, 选择j件取得的最大value
int main()
{
int c;
scanf("%d", &c);
while (c--)
{
int n, m, l;
scanf("%d %d %d", &n, &m, &l);
for (int i = 0; i < n; ++i)
scanf("%d %d", &t[i], &v[i]);
memset(dp, 0, sizeof(dp));
for (int j = 0; j <= l; ++j)
for (int k = 0; k <= m; ++k)
if (k == 0)
dp[j][k] = 0;
else
dp[j][k] = -1e9;
for (int i = 0; i < n; ++i)
for (int j = l; j >= t[i]; --j)
for (int k = m; k >= 1; --k)
dp[j][k] = max(dp[j][k], dp[j-t[i]][k-1] + v[i]);
if (dp[l][m] > 0)
printf("%d\n", dp[l][m]);
else
printf("0\n");
}
return 0;
}
-------------------------------------------------------
kedebug
Department of Computer Science and Engineering,
Shanghai Jiao Tong University
E-mail: kedebug0@gmail.com
GitHub: http://github.com/kedebug
-------------------------------------------------------