01 backpack backpack full &

· 01 & totally backpack backpack foundation

 

01 backpack model: Given n th items, a volume of the i-th item W is i , the value of V i , knapsack capacity sum, selecting some items into the backpack, the total value of the maximum requirements.

F [i, j] denotes the front i th items into the maximum capacity value obtained j bag.

For any items have two states, either release or hold, hold, then it is clear that value with the former, then we should put out a portion of the volume from the bag.

Backpack full model: Given n kinds of articles, the i-th item volume W is i , the value of V i , knapsack capacity sum, selecting some items into the backpack, the total value of the maximum requirements.

F [i, j] represents ago i kinds of items into the maximum value for the capacity of the bag j obtained .

01背包方程:f[i,j]=max(f[i-1,j],f[i-1,j-Wi]+V[i]) (if j>=Wi)

Backpack completely equation: F [I, J] = max (F [I, J], F [I-. 1, JW I ] + V [I]) (IF J> W is = I )

Target: max {f [N] [j] (0 <= j <= sum)}

 

 

 1 #include<iostream>
 2 using namespace std;
 3 int n,sum;
 4 int f[100][11000];
 5 int w[1100],v[1100];
 6 int main()
 7 {
 8     cin>>n>>sum;
 9     for(int i=1;i<=n;i++)
10         cin>>w[i]>>v[i];
11     f[0][0]=0;
12     for(int i=1;i<=n;i++)
13         for(int j=0;j<=sum;j++)
14         {
15             if(j>=w[i])f[i][j]=max(f[i-1][j],f[i-1][j-w[i]]+v[i]);
16             else f[i][j]=f[i-1][j];
17         }
18     cout<<f[n][sum];        
19     return 0;
20 } 
01 backpack ordinary two-dimensional

 

Since the state i in backpack 01 only at each stage of the upper stage i-1 related, so it can reduce the overhead space with an array of rolling -

Plus all in all in a first dimension & f__ of the state from dp f [0] _ and f [1] _ alternately.

 1 #include<iostream>
 2 using namespace std;
 3 int n,sum;
 4 int f[3][11000];
 5 int w[1100],v[1100];
 6 int main()
 7 {
 8     cin>>sum>>n;
 9     for(int i=1;i<=n;i++)
10         cin>>w[i]>>v[i];
11     f[0][0]=0;
12     for(int i=1;i<=n;i++)
13         for(int j=0;j<=sum;j++)
14         {
15             if(j>=w[i])f[i&1][j]=max(f[(i-1)&1][j],f[(i-1)&1][j-w[i]]+v[i]);
16             else f[i&1][j]=f[(i-1)&1][j];
17         }
18     cout<<f[n&1][sum];        
19     return 0;
20 } 
Scroll array of 01 backpack

I write to you very clearly, the first F-dimensional array can be completely omitted, the F [j] represents the backpack into the volume of the largest items of value j.

01 cycles emphasized flashback backpack, the backpack positive sequence loop completely, since the transfer from the backpack 01 i-1-> i, the backpack is transferred completely between i.

When the loop to j:

After half 1.F array f [j..m] is the i-th state, have considered the i

2.F first half of the f [0 ... j-1] states in i-1, i is not considered

J is decreasing means transferred from i-1-> i state.

 

(You can also initialize f 0, representing the value of goods when not put to 0)

 1 #include<iostream>
 2 using namespace std;
 3 int n,sum;
 4 int f[11000];
 5 int w[1100],v[1100];
 6 int main()
 7 { 
 8     cin>>n>>sum;
 9     for(int i=1;i<=n;i++)
10         cin>>w[i]>>v[i];
11     for(int i=1;i<=n;i++)
12         for(int j=sum;j>=w[i];j--)        
13             f[j]=max(f[j],f[j-w[i]]+v[i]);        
14     cout<<f[sum];
15     return 0;
16 }
01 backpack
 1 #include<iostream>
 2 using namespace std;
 3 int n,sum;
 4 int f[11000];
 5 int w[1100],v[1100];
 6 int main()
 7 { 
 8     cin>>n>>sum;
 9     for(int i=1;i<=n;i++)
10         cin>>w[i]>>v[i];
11     for(int i=1;i<=n;i++)
12         for(int j=w[i];j<=sum;j++)        
13             f[j]=max(f[j],f[j-w[i]]+v[i]);        
14     cout<<f[sum];
15     return 0;
16 }
Full backpack

 

· 01 applications backpack

Li 1: Given a positive integer n, to choose the number and make them as sum, find the number of programs.

f [j] represents the program and how much is the number of j

 1 #include<iostream>
 2 #define MAXN 1100
 3 using namespace std;
 4 int f[MAXN],a[MAXN];
 5 int main()
 6 {
 7     int n,sum;
 8     cin>>n>>sum;
 9     for(int i=1;i<=n;i++)
10         cin>>a[i];
11     f[0]=1;
12     for(int i=1;i<=n;i++)
13         for(int j=sum;j>=a[i];j--)
14             f[j]+=f[j-a[i]];
15     cout<<f[sum];    
16 }
Number combinations

Li 2: Given n items, Vi is the volume, quality of Wi, the value of Ki is, for a given volume of SUM, loading of backpack M, find the maximum value in the volume of the allowable loading.

01 backpack is simple, but the volume is more than one dimension only;

 

 1 #include<iostream>
 2 using namespace std;
 3 int v[51],w[51],k[51],f[401][401];
 4 int main()
 5 {   int n,sum,m;
 6     cin>>n>>sum>>m;
 7     for(int i=1;i<=n;i++)
 8         cin>>v[i]>>w[i]>>k[i];
 9     for(int i=1;i<=n;i++)
10         for(int j=sum;j>=v[i];j--)
11             for(int l=m;l>=w[i];l--)
12                 f[j][l]=max(f[j][l],f[j-w[i]][l-w[i]]+k[i]);
13     cout<<f[sum][m];
14     return 0;
15 }
Multidimensional knapsack 01

 

 

 


 

· Backpack full application

Given a natural number n, the n split into several additive forms a positive integer, the number can be reused, the number of required program.

Equivalent of n objects, volumes are 1,2,3..n, knapsack capacity is n, each item can be reused, with a like board, similar to a digital sum combination of the above.

 

 1 #include<iostream>
 2 using namespace std;
 3 int f[1100];
 4 int main()
 5 {
 6     int n;
 7     cin>>n;
 8     f[0]=1;
 9     for(int i=1;i<=n;i++)
10         for(int j=1;j<=n;j++)
11             f[j]=f[j]+f[j-1];
12     cout<<f[n];
13 }
Natural number Split

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/S-Gloria/p/11824116.html