//#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;
}
01背包
最新推荐文章于 2024-04-24 22:50:08 发布