01背包

本文探讨了如何使用动态规划和记忆化搜索解决0-1背包问题。通过三种不同的方法——暴力递归、记忆化搜索和动态规划实现,展示了在有限容量的背包中选择物品以最大化总价值的过程。代码示例详细解释了每种方法的实现思路,为理解和应用这些算法提供了清晰的指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//#include<bits/stdc++.h> 没有vector
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

//输入如下    对应的输出为19
/*5 10
8 6
10 4
4 2
5 4
5 3*/

/***********************************暴力递归************************************************/
int CurrentChooseOrNotMaxvalue(vector<int>value, vector<int>weight, int index, int rext) {

	if (rext<0) return -1;
	if (index == weight.size()) return 0;
	int nochooseit = CurrentChooseOrNotMaxvalue(value, weight, index + 1, rext);//返回index+1以后的最大价值
	int chooseit = CurrentChooseOrNotMaxvalue(value, weight, index + 1, rext - weight[index]);//返回index+1以后的最大价值
	int choose = -1;
	if (chooseit != -1) {
		choose = chooseit + value[index];
	}
	return max(nochooseit, choose);
}
//  **************参数为已经使用过的重量Aw**** 也是暴力递归
int Rerucing(vector<int>v, vector<int>w, int index, int Aw, int bag) {
	if (Aw > bag) return -1;
	if (index == w.size()) return 0;
	int valuechoose = Rerucing(v, w, index + 1, Aw + w[index], bag);
	int chooseit = -1;
	if (valuechoose != -1) {
		chooseit = valuechoose + v[index];
	}
	int nochoose = Rerucing(v, w, index + 1, Aw, bag);
	return max(chooseit, nochoose);
}
/***********************************暴力递归************************************************/

/***********************************记忆化搜索************************************************/

int CurrentChooseOrNotMaxvalue2(vector<int>value, vector<int>weight, int index, int rext,vector<vector<int>>&dp) {
	if (rext<0) return -1;
	if (index == weight.size()) {
		dp[index][rext] = 0;
		return dp[index][rext];
	}
	int nochooseit = CurrentChooseOrNotMaxvalue2(value, weight, index + 1, rext,dp);
	int chooseit = CurrentChooseOrNotMaxvalue2(value, weight, index + 1, rext - weight[index],dp);
	int choose = -1;
	if (chooseit != -1) {
		choose = chooseit + value[index];
	}
	dp[index][rext] = max(nochooseit, choose);
	return dp[index][rext];
}

/***********************************记忆化搜索************************************************/

/***********************************动态规划************************************************/

int CurrentChooseOrNotMaxvalue3(vector<int>value, vector<int>weight, int bag) {
	vector<vector<int>>dp;
	dp.resize(weight.size() + 1, vector<int>((bag + 1), -1));
	//dp[weight.size()] = { 0 };错误 这样会把dp二维数组变成size=weight。size()的一维数组 写for循环或者resize(n,0)
	for (int k = 0; k < bag; k++) {
		dp[weight.size()][k] = 0;
	}
	for (int index = weight.size() - 1; index >= 0; index--) {
		for (int rest = 0; rest <= bag; rest++) {
			int nochooseit = dp[index+1][rest];
			int chooseit = -1;
			if (rest - weight[index]>=0) {
				chooseit = value[index] + dp[index + 1][rest - weight[index]];
			}
			dp[index][rest] = max(nochooseit, chooseit);
		}
	}
	return dp[0][bag];
}
/***********************************动态规划************************************************/


int main() {
	int num;
	int bag;
	cin >> num >> bag;
	vector<int>weight, value;
	weight.resize(num);
	value.resize(num);
	for (int i = 0; i<num; i++) {
		cin >> value[i] >> weight[i];
	}
	vector<vector<int>>dp;
	dp.resize(num + 1, vector<int>((bag + 1), -1));
	//for (int i = 0; i < num; i++) {
	//	cout << value[i] << " ";// << weight[i] << " ";
	//}

	int ans1 = CurrentChooseOrNotMaxvalue(value, weight, 0, bag); //暴力
	int ans2 = CurrentChooseOrNotMaxvalue2(value, weight, 0, bag,dp);//记忆化搜索
	int ans3 = CurrentChooseOrNotMaxvalue3(value, weight, bag);//dp
	cout << ans1 << endl;
	cout << ans2 << endl;
	cout << ans3 << endl;
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值