ACboy needs your help

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


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
 

 

Source
 
 
题目意思:
有n个课程,每个课程在不同天数得到不同价值,总共m天,问m天之内得到最大的价值。
 
思路:
每个课程若在i天完成,就不能在j天完成。以n个课程为组,天数为体积,价值为价值,即得到分组背包的模型。
 
dp[i][j]=max(dp[i-1][j],dp[i-1][j-v[k]]+w[k])//dp[i][j]表示前i组已占用j体积时最大价值。
 
代码:
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <vector>
 6 #include <queue>
 7 #include <cmath>
 8 #include <set>
 9 using namespace std;
10 
11 #define N 105
12 
13 int max(int x,int y){return x>y?x:y;}
14 int min(int x,int y){return x<y?x:y;}
15 int abs(int x,int y){return x<0?-x:x;}
16 
17 
18 int a[N][N];
19 int n, m;
20 int dp[N];
21 
22 main()
23 {
24     int i, j, k;
25     while(scanf("%d %d",&n,&m)==2){
26         if(!n&&!m) break;
27         for(i=1;i<=n;i++){
28             for(j=1;j<=m;j++)
29                 scanf("%d",&a[i][j]);
30         }
31         memset(dp,0,sizeof(dp));
32         for(i=1;i<=n;i++){
33             for(j=m;j>=0;j--){
34                 for(k=1;k<=j;k++){
35                     dp[j]=max(dp[j],dp[j-k]+a[i][k]);
36                 }
37             }
38         }
39         printf("%d\n",dp[m]);
40     }
41 }

 

posted on 2015-07-17 15:33  MC幽夜  阅读(161)  评论(0编辑  收藏  举报