HDU 1712 ACboy needs your help

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
 
解题思路:
转化成01背包问题,利用j天复习第i门课视为一种物品,该物品的体积为j,价值为m[i][j],要注意的是每门课只能选一次
View Code
 1 #include<iostream>
 2 #include<math.h>
 3 #include<stdio.h>
 4 #include<string.h>
 5 using namespace std;
 6 int DP[5000],m[5000][5000],N,M;
 7 int w[5000],v[5000];
 8 int max(int x,int y) {
 9  return (x>y? x:y);
10 }
11 int main () {
12  while(scanf("%d%d",&N,&M)&&(N||M)) {
13   memset(DP,0,sizeof(DP));
14   for(int i=1;i<=N;++i)
15   for(int j=1;j<=M;++j)
16   scanf("%d",&m[i][j]);
17   for(int i=1;i<=N;++i)
18   for(int j=M;j>=1;--j)
19   for(int k=1;k<=j;++k)
20   DP[j]=max(DP[j],DP[j-k]+m[i][k]);
21   printf("%d\n",DP[M]);
22  }
23  return 0;
24 }

 

posted @ 2013-05-12 10:40  xiaxiaosheng  阅读(160)  评论(0编辑  收藏  举报