HDU 2602 Bone Collector (经典0-1背包)

http://acm.hdu.edu.cn/showproblem.php?pid=2602


/*31ms,240KB*/

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mx = 1005;

int w[mx], v[mx], dp[mx];

int _01pack(int n, int maxw)
{
	memset(dp, 0, sizeof(dp));
	int i, j;
	for (i = 0; i < n; ++i)
		for (j = maxw; j >= w[i]; --j)
			dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
	return dp[maxw];
}

int main()
{
	int t, n, maxw, i;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d", &n, &maxw);
		for (i = 0; i < n; ++i) scanf("%d", &v[i]);
		for (i = 0; i < n; ++i) scanf("%d", &w[i]);
		printf("%d\n", _01pack(n, maxw));
	}
	return 0;
}

你可能感兴趣的:(C++,ACM,HDU,背包)