HDU 1712 ACboy needs your help(泛化物品dp)

背包九讲中的泛化物品,裸模板了。

#pragma warning(disable:4996)
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

int a[105][105];
int ans[105];

int main(){
	int n, m;
	while (scanf("%d %d", &n, &m)){
		if (n == 0 && m == 0)break;
		for (int i = 1; i <= n; i++){
			for (int j = 1; j <= m; j++){
				scanf("%d", &a[i][j]);
			}
		}
		memset(ans, 0, sizeof ans);
		for (int i = 1; i <= n; i++){
			for (int j = m; j > 0; j--){
				for (int k = 1; k <= j; k++){
					ans[j] = max(ans[j], ans[j - k] + a[i][k]);
				}
			}
		}
		printf("%d\n", ans[m]);
	}
	return 0;
}


你可能感兴趣的:(HDU 1712 ACboy needs your help(泛化物品dp))