ACboy needs your help(简单DP)

时间:2024-09-07 00:03:32

HDU 1712

Problem Description
ACboy has N courses this term, and he plans to spend at most M days on study.Of course,the profit he will gain from different course depending on the days he spend on it.How to arrange the M days for the N courses to maximize the profit?
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers N and M, N is the number of courses, M is the days ACboy has.
Next follow a matrix A[i][j], (1<=i<=N<=100,1<=j<=M<=100).A[i][j] indicates if ACboy spend j days on ith course he will get profit of value A[i][j].
N = 0 and M = 0 ends the input.
Output
For each data set, your program should output a line which contains the number of the max profit ACboy will gain.
Sample Input
2 2
1 2
1 3
2 2
2 1
2 1
2 3
3 2 1
3 2 1
0 0
Sample Output
3
4
6
题意:第一行两个整数 n,m ,代表有 n 个课程,m 天学习,然后 n 行,每行 m 个数,i 行 j 列代表第 i 课程花费 j 天可以获得的价值。问 m 天可以获得得最大价值
dp[i][j] 代表 i 种书,花费 j 天可以获得的最大价值
dp[i][j] = dp[i-1][j] + max (A[i][k])    0<=k<=j 
 #include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
using namespace std;
#define MX 105
int n,m;
int dp[MX][MX];
int A[MX][MX];
int main()
{
while (scanf("%d%d",&n,&m)&&(n||m))
{
memset(dp,,sizeof(dp));
memset(A,,sizeof(A));
for (int i=;i<=n;i++)
{
for (int j=;j<=m;j++)
scanf("%d",&A[i][j]);
}
for (int i=;i<=n;i++) // i 本书
{
for (int j=;j<=m;j++) // j 天
{
for (int k=;k<=j;k++)
{
dp[i][j]=max(A[i][k]+dp[i-][j-k],dp[i][j]);
}
}
}
printf("%d\n",dp[n][m]);
}
return ;
}