初识背包问题

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
using namespace std;
const int N = 505;
const int MAXN = 1000005;




//01背包问题
int dp[55][55 * 55 + 1];
int n, W;
int v[N], w[N];
int f[N];

int f1(int kind, int money) {
	if (kind == 6) {
		return 0;
	}
	if (money + w[kind] > W) {
		return f1(kind + 1, money);
	} else {
		return max(f1(kind + 1, money), f1(kind + 1, money + w[kind]) + v[kind]);
	}
}

int f2(int kind, int weight) {
	if (dp[kind][weight] >= 0) {
		return dp[kind][weight];
	}
	int res;
	if (kind == 6) {
		return 0;
	} else if (weight + w[kind] > W) {
		res = f2(kind + 1, weight);
	} else {
		res = max(f2(kind + 1, weight), f2(kind + 1, weight + w[kind]) + v[kind]);
	}
	return dp[kind][weight] = res;
}

void solve00() {
	for (int i = n - 1; i >= 0; i--) {
		for (int j = 1; j <= W; j++) {
			if (j < w[i]) {
				dp[i][j] = dp[i + 1][j];
			} else {
				dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
			}
		}
	}
	printf("%d", dp[0][W]);
}

void solve0() {
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= W; j++) {
			if (j < w[i]) {
				dp[i][j] = dp[i - 1][j];
			} else {
				dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
			}
		}
	}
	printf("%d", dp[n][W]);
}

//完全背包问题
void solve1() {
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j <= W; j++) {
			for (int k = 0; k * w[i] <= j; k++ ) {
				dp[i][j] = max(dp[i][j], dp[i - 1][j - k * w[i]] + k * v[i]);
			}
		}
	}
	printf("%d\n", dp[n][W]);
}

//01背包优化
void solve3() {
	for (int i = 1; i <= n; i++) {
		for (int j = W; j >= w[i]; j--) {
			f[j] = max(f[j], f[j - w[i]] + v[i]);
		}
	}
	printf("%d", f[W]);
}

//完全背包优化1
void solve2() {
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j <= W; j++) {
			if (w[i] > j) {
				dp[i][j] = dp[i - 1][j];
			} else {
				dp[i][j] = max(dp[i - 1][j], dp[i][j - w[i]] + v[i]);
			}
		}
	}
	printf("%d", dp[n][W]);
}

//完全背包优化2
void solve4() {
	for (int i = 1; i <= n; i++) {
		for (int j = w[i]; j <= W; j++) {
			f[j] = max(f[j], f[j - w[i]] + v[i]);
		}
	}
	printf("%d", f[W]);
}

//01背包2相较于最初的01背包限制条件有所改变(W规模变大),尝试改变DP对象
void solve5() {
	fill(dp[0], dp[0] + 55 * 55 + 1, MAXN);
	dp[0][0] = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j <= 55 * 55 + 1; j++) {
			if (j < v[i]) {
				dp[i][j] = dp[i - 1][j];
			} else {
				dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - v[i]] + w[i]);
			}
		}
	}
	int res = 0;
	for (int i = 0; i <= 55 * 55; i++) {
		if (dp[n][i] <= W) {
			res = i;
		}
	}
	printf("%d", res);
}

int main() {
	scanf("%d %d", &n, &W);
	for (int i = 1; i <= n; i++) {
		scanf("%d %d", w + i, v + i);
	}
	//solve1();
	//solve3();
	//solve2();
	//solve4();
	solve5();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值