2013
12-21

# HDU 1712 ACboy needs your help-背包问题-[解题报告] c-sharp

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?

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.

For each data set, your program should output a line which contains the number of the max profit ACboy will gain.

2 2
1 2
1 3
2 2
2 1
2 1
2 3
3 2 1
3 2 1
0 0

3
4
6

//分组背包,看背包九讲
/*
for 所有的组k
for v=V..0
for 所有的i属于组k
f[v]=max{f[v],f[v-c[i]]+w[i]}
*/
#include<iostream>
#include<cstdio>
using namespace std;
int a[101][101];
int n,m;
int dp[101]; //dp[i]表示用i天的时间做功课得到的最大收益
int max(int a,int b)
{
return a>b?a:b;
}
void solve(int x)
{
int i,j;
for(i=m;i>=0;i--)
for(j=1;j<=m;j++)    //对于每个分组里物品
if(i>=j)
dp[i]=max(dp[i],dp[i-j]+a[x][j]);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF&&!(n==0&&m==0))
{
int i,j;
for(i=0;i<n;i++)
for(j=1;j<=m;j++)
scanf("%d",&a[i][j]);
memset(dp,0,sizeof(dp));
for(i=0;i<n;i++)
solve(i);
printf("%d/n",dp[m]);
}
return 0;
}

1. 第一题是不是可以这样想，生了n孩子的家庭等价于n个家庭各生了一个1个孩子，这样最后男女的比例还是1:1