subject
01 knapsack problem of zz
describe
zz goes to the snack shop every day to find his favorite snacks. Unfortunately, zz's Backpack Capacity (V < = 10000) is limited. He can only hold a certain amount of snacks. In order to buy a snack combination with higher satisfaction, zz gives each snack a satisfaction degree, indicating how much satisfaction zz can get after buying this snack. There are n (n < = 100) kinds of snacks. Each kind of snack has an occupied space (WI < = 100) and a zz satisfaction (DI < = 1000). Please help zz calculate the maximum satisfaction he can get?
Input format
The first line contains two integers, n,v, representing the type of snack and the capacity of zz's backpack.
Next n lines, two integers Wi and Di for each line, respectively, represent the space occupied by the ith snack and the satisfaction obtained by zz after buying the snack.
Output format
An integer in a row represents the maximum degree of satisfaction that zz can obtain.
sample input
6 10
1 4
4 3
5 6
2 2
3 2
5 7
sample output
14
Topic train of thought
The first idea: dp two-dimensional array
#include <iostream> #include <algorithm> using namespace std; int dp[10001]; int main(){ int n,v,w[101]={},d[101]={}; //Defined array cin>>n>>v; for(int i=1;i<=n;i++) cin>>w[i]>>d[i]; //input for(int i=1;i<=n;i++){ for(int j=v;j>=1;j--){ if(j>=w[i]) dp[j]=max(dp[j],dp[j-w[i]]+d[i]); //State transfer equation } } cout<<dp[v]<<endl; //output return 0; }
The second way is to optimize it into dp one bit array
#include <iostream> #include <algorithm> using namespace std; int dp[10001]; int main(){ int n,v,w[101]={},d[101]={}; //array define cin>>n>>v; for(int i=1;i<=n;i++) cin>>w[i]>>d[i]; //input for(int i=1;i<=n;i++){ for(int j=v;j>=1;j--){ if(j>=w[i]) dp[j]=max(dp[j],dp[j-w[i]]+d[i]); //Optimal state transfer equation } } cout<<dp[v]<<endl; //output return 0; }
The third way of thinking memory search
#include <iostream> #include <cmath> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; int dp[105][3][10005]={}; int n,v;int w[105]={},d[105]={}; int dfs(int step_n,int point_w,int bag_v){ if(step_n>n||bag_v>v) return 0; if(dp[step_n][point_w][bag_v]!=-1) return dp[step_n][point_w][bag_v]; int tmp1=0,tmp2=0; if(bag_v+w[step_n]<=v) tmp1=dfs(step_n+1,1,bag_v+w[step_n])+d[step_n]; tmp2=dfs(step_n+1,2,bag_v); dp[step_n][point_w][bag_v]=max(tmp1,tmp2); return dp[step_n][point_w][bag_v]; } int main(){ cin>>n>>v; for(int i=1;i<=n;i++){ scanf("%d%d",&w[i],&d[i]); } for(int i=0;i<105;i++){ for(int j=0;j<3;j++){ for(int k=0;k<10005;k++){ dp[i][j][k]=-1; } } } cout<<dfs(0,0,0)<<endl; return 0; }