poj3624 dp 01背包

链接:点我

很简单的01。。。

#include <iostream>
#include <cstdio>
using namespace std;
#define max(a,b)	((a)>(b)?(a):(b))
const int maxn = 3405;
int main()
{
	int n, v;
	int dp[4*maxn], val[maxn], vol[maxn];
	while (scanf("%d %d", &n, &v) != EOF)
	{
		int i, j;
		for (i=0; i<n; i++) scanf("%d %d", &vol[i], &val[i]);
		memset(dp, 0, sizeof(dp));

		for (i=0; i<n; i++)
			for (j=v; j>=vol[i]; j--)
				if (dp[j] < dp[j-vol[i]] + val[i])
					dp[j] = dp[j-vol[i]] + val[i];

		printf("%d\n", dp[v]);
	}
	return 0;
}


你可能感兴趣的:(dp,ACM,01背包,poj3624)