hdu1712 ACboy needs your help (分组背包)

#include <stdio.h>
#include <string.h>
#define MAXN 102
//分组背包
int main()
{
	int n,m,i,j,k;
	int dp[MAXN],profit[MAXN][MAXN];
	while(scanf("%d %d",&n,&m),(n||m))
	{
		for(i=1;i<=n;++i)
		{
			for(j=1;j<=m;++j)
				scanf("%d",&profit[i][j]);
		}
		memset(dp,0,sizeof(dp));
		for (i=1;i<=n;++i)
		{
			for(j=m;j>=1;--j)
				for(k=1;k<=j;++k)
				{
					if(dp[j]<dp[j-k]+profit[i][k])
						dp[j]=dp[j-k]+profit[i][k];
				}
		}
		printf("%d\n",dp[m]);
	}
	return 0;
}
 
 
 
 

你可能感兴趣的:(hdu1712 ACboy needs your help (分组背包))