【分组背包】HDU1712ACboy needs your help

来源:互联网 发布:马克思资本论 知乎 编辑:程序博客网 时间:2024/06/07 06:50

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=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 21 21 32 22 12 12 33 2 13 2 10 0
 

Sample Output
346

题目意思:

n,m,表示共有n门课,你有m天的时间去复习;

然后接下来是一个n*m的矩阵a[i][j];

表示第i门课复习j天的收获为a[i][j];

问你复习m天能有最大的收获是多少;

我们可以想成每一门课就是一个分组,这门课复习的天数就是这个分组的成员,该组的成员之间是互斥的;

代码:

#include<iostream>#include<cstring>#include<algorithm>using namespace std;int dp[110],a[110][110];int n,m;int main(){    while(cin>>n>>m&&n+m){       //  n门课程,m天时间复习;        memset(dp,0,sizeof(dp));        for(int i=1;i<=n;i++){       //  矩阵,第i门课程,复习j天的价值;            for(int j=1;j<=m;j++){                cin>>a[i][j];            }        }        //  分组背包;        for(int i=1;i<=n;i++){          //  每组的枚举;            for(int j=m;j>=1;j--){      //  天数的情况枚举;                for(int k=1;k<=j;k++){      //  该组的成员枚举;                    dp[j]=max(dp[j],dp[j-k]+a[i][k]);                }            }        }        cout<<dp[m]<<endl;    }    return 0;}/*    01背包的分组问题;    想想我们处理01背包问题;        for i=1...n       //    这里需要注意,必须先枚举物品,在枚举的容量,反之不行;            for j=m...0                dp[j]=max(dp[j],dp[j-w[i]]+v[i]);    然后我们来看看分组的01背包;        for i=1...n            for j=m...0     //  前两个for我们和01背包一样,一个枚举组,一个枚举容量;顺序不允许错;                for k=1...j;        //  这个是对该组的物品进行枚举;这里我们需要注意的是顺序我们不能搞错了;*/


0 0
原创粉丝点击