ACboy needs your help(简单DP)
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.
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
1 #include <iostream> 2 #include <stdio.h> 3 #include <math.h> 4 #include <string.h> 5 using namespace std; 6 #define MX 105 7 int n,m; 8 int dp[MX][MX]; 9 int A[MX][MX]; 10 int main() 11 { 12 while (scanf("%d%d",&n,&m)&&(n||m)) 13 { 14 memset(dp,0,sizeof(dp)); 15 memset(A,0,sizeof(A)); 16 for (int i=1;i<=n;i++) 17 { 18 for (int j=1;j<=m;j++) 19 scanf("%d",&A[i][j]); 20 } 21 for (int i=1;i<=n;i++) // i 本书 22 { 23 for (int j=1;j<=m;j++) // j 天 24 { 25 for (int k=0;k<=j;k++) 26 { 27 dp[i][j]=max(A[i][k]+dp[i-1][j-k],dp[i][j]); 28 } 29 } 30 } 31 printf("%d\n",dp[n][m]); 32 } 33 return 0; 34 }