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
Source
昨天晚上开会总结了所有知识点,突然想起来分组背包没做过,果断早上滚来补题~
先说分组背包是什么玩意,分组分组,顾名思义,在背包里面的物品是分成不同组的,每个组只能取一件物品。类似于01背包的做法,很容易想到,背包的容量一定是从大到小递推才能保证每组物品只取一个,这么说来,组间的循环一定是第一层。那么还有第三层的循环是每组内的各个物品的循环。
再说这个题:第一层是不同科间的循环,第二层的循环是v从已知最大天数到0,第三层是组内循环也就是每个课程选几天的付出
/***************
hdu1712
2016.3.13
93MS 1624K 724 B G++
***************/
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int num[110][110],n,m;
int dp[110];
int max(int a,int b){if(a>b)return a;return b;}
int main()
{
// freopen("cin.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
if(m==0&&n==0)break;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&num[i][j]);
memset(dp,0,sizeof(dp));
for(int k=1;k<=n;k++)//the type of course
for(int v=m;v>=0;v--)
for(int j=1;j<=v;j++)
dp[v]=max(dp[v],dp[v-j]+num[k][j]);
printf("%d\n",dp[m]);
}
return 0;
}