Linear DP & knapsack DP

Posted by xgrewellx on Mon, 24 Jan 2022 22:03:35 +0100

The type of DP with the characteristics of linear programming is called linear DP
This kind of DP is generally more basic. Konjaku does not mention the word simple
DP:
The status representation shall meet three characteristics:
1. Optimization: meet the properties of the optimal substructure
(slightly different from the greedy "snowball", DP algorithm does not necessarily meet the local optimization, resulting in the global optimization, but DP algorithm can achieve the global optimization by updating the optimal solution)
2. No aftereffect: that is, the decision of the current problem is not affected by the subsequent decision, which is no aftereffect
For example, the big man turned into konjaku
3. Repeatability of subproblems: the premise of applying DP is the similarity of subproblems, which enables us to recursively deduce the relationship to achieve the optimal solution: state transition equation
Therefore, the state transition equation is the key to the implementation of DP algorithm
Linear DP:
The state transition of linear DP increases in a direction along the dimension
The following is a typical example:
1.LIS (longest ascending subsequence): obviously, if i and j make i > J & & AI > AJ,
Then Ai can be added to the ascending subsequence ending with Aj,
Compared with the original ascending subsequence ending with Ai, it can get a longer and better length, that is, state update
Then the state transition equation can be obtained through the above analysis:
Bi=max (Bi, Bj+1), special, B0=0
2.LCS (longest common subsequence)
Here we separate the processed part from the untreated part
Then the state transition equation is:
F[i,j]=max(F[i-1,j],F[i,j-1],F[i-1,j-1]+1)
Backpack DP: the original backpack has changed from 9 to 7. Is it okay if I swallow 3 more
Firstly, for all knapsack DP, their state transition equations are unified as follows:
F[j]=max(F[j],F[j-v[i]]+w[i])
Where I refers to the corresponding value w [i] of the i-th article, the price v [i], and j refers to the maximum value F [J] when the weight in the package is J (both the above price and weight refer to "price")
Note: in the above state transition equation: f [j-v [i]] is undoubtedly the state of the maximum value obtained when the mass in the package is at j-v [i],
Adding w [i] in this state means that if the value of w [i] is added in this state (obviously, w [i] has no effect on the previous part, it is still the optimal state under the ideal),
Compared with the current optimal state, the maximum value is updated and the transition of the optimal state is completed
1.0/1DP
There are only one item of N kinds. The value is C and the price is V. the maximum price you can pay is M,
Try to find the maximum value that can be gained without exceeding the maximum cost
Set the above state transition equation
Code

Click to view the code
void DP(){
    for(int i=1;i<=n;i++){//Enumerate from item 1 to item n
        for(int j=m;j>=v[i];j--){//0 / 1 knapsack search in reverse order to prevent the reuse of certain items
            f[j]=max(f[j],f[j-v[i]]+c[i]);//State transition equation
        }
    }
    for(int i=1;i<=m;i++){//Find the optimal solution obtained at various costs
       ans=max(ans,f[i]);//Update the answer with the optimal solution
    }
    cout<<ans;//Output answer
    return;//It's over. It's over
}
2. Complete Backpack N kinds of goods, each countless, value C, price V, maximum bearing cost M Still ask the maximum value within the acceptable range It is also a set of state transition equations, but the cyclic structure changes appropriately Code Click to view the code
void DP(){
     for(int i=1;i<=n;i++){//Traverse items from 1 to n
         for(int j=v[i];j<=m;j++){//Different from 0 / 1, the positive sequence loop here guarantees that unlimited items can be used, and I items can also be used when the value of j changes from Ma to Ma+v [i]
             f[j]=max(f[j],f[j-v[i]]+c[j]);//State transition update
         }
     }
     for(int i=1;i<=m;i++){//Ergodic optimal solution under various costs
         ans=max(ans,f[i]);//Update answer
     }
     cout<<ans;//Output results
     return;//end
}
3. Multiple backpacks N kinds of goods, P of each kind, value C, price V, maximum bearing M What I want to say is that there is also O (MN) algorithm here optimization algorithm
#include <bits/stdc++. h> / / maintain multiple knapsacks in monotonic queue
using namespace std;
int n,m,ans;
int v[501],w[501],c[501],f[6000];
int q[10000];
int calc(int i,int u,int k){
	return f[u+k*v[i]]-k*w[i];
}
int main(){
	cin>>n>>m;
	memset(f,0xcf,sizeof(f));
	f[0]=0;
	for(int i=1;i<=n;i++){
		scanf("%d%d%d",&v[i],&w[i],&c[i]);
		for(int u=0;u<v[i];u++){
			int l=1,r=0;
			int maxp=(m-u)/v[i];
			for(int k=maxp-1;k>=max(maxp-c[i],0);k--){
				while(l<=r&&calc(i,u,q[r])<=calc(i,u,k))r--;
				q[++r]=k;
			}
			for(int p=maxp;p>=0;p--){
				while(l<=r&&q[l]>p-1)l++;
				if(l<=r)
					f[u+p*v[i]]=max(f[u+p*v[i]],calc(i,u,q[l])+p*w[i]);
				if(p-c[i]-1>=0){
					while(l<=r&&calc(i,u,q[r])<=calc(i,u,p-c[i]-1))r--;
					q[++r]=p-c[i]-1;
				}
			}
		}
	}
	for(int i=1;i<=m;i++){
		ans=max(ans,f[i]);
	}
	cout<<ans;
	return 0;
} 
4. Group Backpack Code
memset(f,0xcf,sizeof(f));
f[0]=0;
	for(int i=1;i<=n;i++){
		for(int j=m;j>=0;j--)
			for(int k=1;k<=c[i];k++)
				for(int k=1;k<=c[i];k++)
					if(j>=v[i][k])
						f[j]=max(f[j],f[j-v[i][k]]+c[i][k]);