标签:
题目链接 http://acm.hust.edu.cn/vjudge/problem/19210
解题思路
就是0-1背包问题。只是不止一个背包。
可以用滚动数组降成一维。
代码
#include<iostream> #include<cstdio> #include<string.h> using namespace std; const int maxLen = 1000; int dp[105][35]; int w[maxLen], v[maxLen]; int maxw[105]; int maxValue[105]; int cases, n, m; void Read() { scanf("%d", &n); for(int i=1; i<=n; i++) scanf("%d%d", &w[i], &v[i]); scanf("%d", &m); for(int i=1; i<=m; i++) scanf("%d", &maxw[i]); } int main() { cin >> cases; while(cases--) { memset(dp, 0, sizeof(dp)); Read(); int people = 1; while(people <= m) { for(int i=1; i<=n; i++) for(int j=maxw[people]; j>=0; j--) if(j - v[i] >= 0) dp[people][j] = max(dp[people][j-v[i]] + w[i], dp[people][j]); maxValue[people] = dp[people][maxw[people]]; people++; } int tot = 0; for(int i=1; i<people; i++) tot += maxValue[i]; printf("%d\n", tot); } }
标签:
原文地址:http://www.cnblogs.com/ZengWangli/p/5790406.html