ACboy needs your help(HDU 1712 分组背包入门)

ACboy needs your help

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5872    Accepted Submission(s): 3196

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
 
Source
 
分组背包,背包容量m,物品分为n组,每组只能取一件,求背包最大价值。
dp[i][j]表示对于前i组物品,背包容量为j时的最大价值,此时对于每种dp[i][j]需要遍历第i组的每一个物品,求出最大的dp[i][j];
 
状态转移方程:
          dp[i][j]=max(dp[i][j],dp[i-1][j-k]+ma[i][k])
 
 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
#define Max 105
int dp[Max][Max],ma[Max][Max];
int n,m;
int main()
{
int i,j;
memset(ma,,sizeof(ma));
freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m))
{
if(n==&&m==)
break;
for(i=;i<=n;i++)
for(j=;j<=m;j++)
scanf("%d",&ma[i][j]);
memset(dp,,sizeof(dp));
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
for(int k=;k<=j;k++)
{
if(dp[i][j]<dp[i-][j-k]+ma[i][k])
dp[i][j]=dp[i-][j-k]+ma[i][k];
}
//cout<<dp[i][j]<<" ";
}
// cout<<endl;
}
printf("%d\n",dp[n][m]);
}
return ;
}
 
上一篇:nginx变量名规则


下一篇:string字符串转C风格字符串 进而转换为数字