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],要注意的是每门课只能选一次
 
AC代码:
#include<iostream> #include<math.h> #include<stdio.h> #include<string.h> using namespace std; int DP[5000],m[5000][5000],N,M; int w[5000],v[5000]; int max(int x,int y) { return (x>y? x:y); } int main () { 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",&m[i][j]);   for(int i=1;i<=N;++i)   for(int j=M;j>=1;--j)   for(int k=1;k<=j;++k)   DP[j]=max(DP[j],DP[j-k]+m[i][k]);   printf("%d\n",DP[M]); } return 0; }
posted @ 2013-04-14 14:33  xiaxiaosheng  阅读(118)  评论(0编辑  收藏  举报