HDU1712 ACboy needs your help

来源:互联网 发布:小说源码带采集 编辑:程序博客网 时间:2024/06/03 07:41

ACboy needs your help

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

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 21 21 32 22 12 12 33 2 13 2 10 0

Sample Output
346

 点击原题链接

题意:
n个课程,最多有m天可以用,每个课程花费不同的天数得到的收益不同,
第i个课程花费j天来学那么收益为a[i][j],问如何安排收益最大。
如第一测试数据: 
2  2
1  2  a[1][1] = 1;a[1][2] = 2;
1  3  a[2][1] = 1;a[2][2] = 3;
首先明确每个课程只会上一次。设dp[i][j]为前i个课程花费j天的最大收益,
枚举当前i课程的所有需要的天数k,并选择学还是不学对应的a[i][k]。
思路:
可以将每一门课看成一个分组,每门课不同天数的选择看成是分组的物品(显然只能有一个选择),
物品的费用即为花费的天数,物品的价值为题中给出的收获。该题中背包容量最大为M。
设dp[x]为前i组物品,在背包容量为x(即费用为x)时的最大价值。则将i从1到N进行过历遍后(第一重循环),dp[m]即为所求。


#include <stdio.h>#include <string.h>#define max(a,b)  a > b ? a : b int a[110],dp[1010];int main(){int n,m,i,j,k;//freopen("input.txt","r",stdin);while(scanf("%d%d",&n,&m) && (n || m)){memset(dp,0,sizeof(dp));for(i = 1;i <= n;i ++ )  //学i门课程时 {for(j = 1;j <= m;j ++ )scanf("%d",&a[j]);for(j = m;j > 0;j -- ) //当最多用j天去做时 for(k = 1;k <= j;k ++ ) //遍历用j天能k完成中得收益 {dp[j] = max(dp[j],dp[j - k] + a[k]);//取最大收益 //printf("dp[%d]=%d\n",j,dp[j]);} }printf("%d\n",dp[m]);}return 0;}


2 0
原创粉丝点击