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
这道题目就是简单的分组背包跟背包九讲上的一样,背包九讲分组背包的链接----------->>>>>
点击打开链接
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
int n,m;
int a[105][105],dp[105];
while(~scanf("%d %d",&n,&m),n||m)
{
memset(dp,0,sizeof(dp));
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
scanf("%d",&a[i][j]);
for(int i=1; i<=n; i++) //一共有n个组所以循环到n
for(int k=m; k>0; k--) //一更有m天所以V=m
for(int j=1; j<=k; j++) //每组都有到当前最大天数的组数,就是j不能大于k的原因
{
dp[k]=max(dp[k],dp[k-j]+a[i][j]);
}
printf("%d\n",dp[m]);
}
return 0;
}
本文介绍了一种通过分组背包算法解决课程安排问题的方法,旨在帮助学生在有限的时间内通过最优的课程组合获得最大的收益。文章提供了完整的代码实现,并附有输入输出示例。

被折叠的 条评论
为什么被折叠?



