HDU 1712 ACboy needs your help (分组背包模版题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712

有n门课,和m天时间。每门课上不同的天数有不同的价值,但是上过这门课后不能再上了,求m天里的最大价值。

分组背包模版题。

 1     //#pragma comment(linker, "/STACK:102400000, 102400000")
 2     #include <algorithm>
 3     #include <iostream>
 4     #include <cstdlib>
 5     #include <cstring>
 6     #include <cstdio>
 7     #include <vector>
 8     #include <cmath>
 9     #include <ctime>
10     #include <list>
11     #include <set>
12     #include <map>
13     using namespace std;
14     typedef long long LL;
15     typedef pair <int, int> P;
16     const int N = 1e2 + 5;
17     int dp[N], a[N][N], inf = 1e6;
18 
19     int main()
20     {
21         int n, m;
22         while(~scanf("%d %d", &n, &m) && (n || m)) {
23             for(int i = 1; i <= n; ++i) {
24                 for(int j = 1; j <= m; ++j) {
25                     scanf("%d", &a[i][j]);
26                     dp[j] = 0;
27                 }   
28             }
29             for(int i = 1; i <= n; ++i) { //n组
30                 for(int j = m; j >= 1; --j) { //体积
31                     for(int k = 1; k <= m; ++k) { //第i组的各个数据
32                         if(j - k >= 0) {
33                             dp[j] = max(dp[j - k] + a[i][k], dp[j]);
34                         }
35                     }
36                 }
37             }
38             printf("%d
", dp[m]);
39         }
40         return 0;
41     }
原文地址:https://www.cnblogs.com/Recoder/p/5893037.html