DP Starter Series - -01 backpack knapsack problem

-01 backpack knapsack problem

Knapsack problem has long been used to study mice DP, for the knapsack problem I have in DFS has held over the chestnuts, so this time we thought with the transition to learn about the 01 backpack.
What is the 01 backpack it? It means this item does not take issue with the holding. In other words, when faced with the decision I have two options, take and not take. But do not get to take and will have an impact on the results back, so that you can not glance optimal solution (optimal solution, I will use the following outline to OPT). So we can only put all the cases enumerated and then select, selection process, we have a lot of problems, of course, the problem for DP is double counting, so we consider how the state transition enough to solve this problem.
First, I recommend a blog site DP beginner essential, WOC too Niubi! ! ! QAQ
So let's take a subject carrying a curtain to reveal us today.

Title Description

There are N items V and a capacity of the backpack. Each item can only be used once. Volume is the i-th items vi, a value wi. Which solving the items into the bag, the total volume of these items can not exceed the capacity of the backpack, and the total value of the maximum. The maximum output value.

Input Format

The first line of two integers N, V, separated by spaces, respectively, and the number of articles backpack volume. Down there are N rows, each row two integers vi, wi, separated by spaces, respectively, and the volume of the i-th value of the items.

Output Format

Output An integer representing the maximum value.

data range

0 <N, V≤1000
0 <we, wi≤1000

Sample Input

4 5
1 2
2 4
3 4
4 5

Sample Output

8

submit

OK We use two methods to understand and solve the problem.
First, the knapsack problem is the election and not vote for the issue, if elected, it will reduce the backpack volume, the value will increase, so we have to study each selected maximum use of space remaining value after a book.

So, we should start from the remaining space, how much of the remaining space, we only find the optimal solution.
So let the remaining space from 1 to start thinking about (a total of six kinds of space remaining case is 0,1,2,3,4,5, respectively) of these six, and each of the remaining space will be decided by the last remaining space, If the presence of a residual presence in this article, then the value will not choose this than the current value of smaller items.

#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
int f[1010][1010];
int n,m;
int v[10000],w[10000];
int main()
{
 cin>>n>>m;
 for(int i=1;i<=n;i++)cin>>v[i]>>w[i];
 for(int i=1;i<=n;i++)
 for(int j=0;j<=m;j++)
 {
  f[i][j]=f[i-1][j];
  if(j>=v[i])
  f[i][j]=max(f[i][j],f[i-1][j-v[i]]+w[i]);
 }
 int res=0;
 for(int i=0;i<=m;i++)res=max(res,f[n][i]);
 cout<<res<<endl;
 return 0;
 } 

Talk about repeated placement of the issue, the current state is placed not appear likely to repeat, because he will now put every thing and hold will be placed in different dimensions among different dimensions to be transferred.
So there is no problem repeating placed.
But the one-dimensional array if there is such a case.
Here the code for a one-dimensional array of light out

#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
int w[1010],v[1010];
int f[200000];
int main()
{
 int n,m;
 cin>>n>>m;
 for(int i=0;i<n;i++)cin>>w[i]>>v[i];
 for(int i=n;i>=0;i--)
 for(int j=m;j>=w[i];j--)
  f[j]=max(f[j],f[j-w[i]]+v[i]);
  cout<<f[m]<<endl;
 } 

You will find my code is circulating in reverse order because, if I was positive sequence, then there will be possible to repeat the previous articles placed. If you go in reverse order, then, did not put before each update are based on the state of the object is transferred over. So there will be no repeat of the problem. So if you updated based on the updated, he is likely that this will repeat items placed!
By- wheel month

Published 32 original articles · won praise 12 · views 1195

Guess you like

Origin blog.csdn.net/qq_35339563/article/details/103580729