HOJ 2602 Bone Collecter简单DP

简单使用01背包的方法

#include
#include
#define max(a, b) a > b ? a: b
using namespace std;
const int NN = 1005;
int N, V, value[NN], volume[NN], cap[NN][NN];

int main() {
	int c;
	cin >> c;
	while (c--) {
		memset(cap, 0, sizeof(cap));
		cin >> N >> V;
		for (int i = 1; i <= N; i++)
			cin >> value[i];
		for (int i = 1; i <= N; i++)
			cin >> volume[i];
		for (int i = 1; i <= N; i++)
			for (int j = 0; j <= V; j++)
				if (j >= volume[i])
					cap[i][j] = max(cap[i - 1][j], cap[i - 1][j - volume[i]] + value[i]);
				else
					cap[i][j] = cap[i - 1][j];
		cout << cap[N][V] << endl;

	}
	return 0;
}

你可能感兴趣的:(动态规划)