AcWing-3. Complete knapsack problem (java implementation)

AcWing-3. Complete knapsack problem

topic description

There are N items and a knapsack of capacity V, and infinite pieces of each item are available.

The volume of the i-th item is vi, and the value is wi.

Solve which items to put into the backpack so that the total volume of these items does not exceed the capacity of the backpack and the total value is the largest.
output the maximum value.

input format

In the first line, two integers, N and V, are separated by spaces, which represent the number of items and the volume of the backpack respectively.

Then there are N lines, and each line has two integers vi, wi, separated by spaces, representing the volume and value of the i-th item respectively.

output format

Output an integer representing the maximum value.

data range

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

Input sample:

4 5
1 2
2 4
3 4
4 5

Sample output:

10

Ideas (plain version)

State representation: f ( i , j ), represents all value sets selected from 1 to i whose capacity does not exceed j State representation: f(i,j), represents all value sets selected from 1 to i with capacity not exceeding jState representation : f ( i ,j),Represents all value sets selected from 1 to i whose capacity does not exceed j

Attribute: Max Attribute: MaxAttributes : M a x

Core: do not take the last number i or take k pieces (the maximum value of k is the maximum possible number of i, that is, the total capacity does not exceed j after taking k pieces of i) core: do not take the last number i or take k pieces (k The maximum value is the maximum possible number of i, that is, after taking k i, the total capacity does not exceed j)the core:The last number i is not taken or k is taken ( the maximum value of k is the maximum possible number of i , that is, the total capacity does not exceed j after taking k i )

State division: f ( i − 1 , j ) takes 0 i, 1 i ... ... , k i becomes f ( i , j ) state division: f(i-1,j) takes 0 i, 1 i...,k i becomes f(i,j)state division:f(i1,j ) take 0 i , _1 i _,k i become f ( i , _j)

The state transition equation can be obtained by state division:
f ( i , j ) = Max ( f ( i − 1 , j ) , f ( i − 1 , j − vi ) + wi , f ( i − 1 , j − 2 ∗ vi ) + 2 ∗ wi , … … , f ( i − 1 , j − k ∗ vi ) + k ∗ wi ) f(i,j) = Max(f(i-1,j),f(i- 1,j-vi)+wi,f(i-1,j-2*vi)+2*wi,...,f(i-1,jk*vi)+k*wi)f(i,j)=Max(f(i1,j),f(i1,jvi)+wi,f(i1,j2vi)+2wi,,f(i1,jkvi)+kwi)

core code

		for(int i=1;i<=n;i++) {
    
    
			for(int j=1;j<=v;j++) {
    
    
				dp[i][j] = dp[i-1][j];
				if(a[i].v<=j) {
    
    
					for(int k=1;k<=j/a[i].v;k++) {
    
    
						dp[i][j] = Math.max(dp[i][j],dp[i-1][j-k*a[i].v]+k*a[i].w);
					}
				}
			}
		}

full code

package acWing003;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class Main {
    
    
	static int N = 1010;
	static int n,v;
	static Pair a[] = new Pair[N];
	static int dp[][] = new int[N][N];
	public static void main(String[] args) throws Exception{
    
    
		Read r = new Read();
		n = r.nextInt();v = r.nextInt();
		for(int i=1;i<=n;i++) {
    
    
			a[i] = new Pair(r.nextInt(),r.nextInt());
		}
		for(int i=1;i<=n;i++) {
    
    
			for(int j=1;j<=v;j++) {
    
    
				dp[i][j] = dp[i-1][j];
				if(a[i].v<=j) {
    
    
					for(int k=1;k<=j/a[i].v;k++) {
    
    
						dp[i][j] = Math.max(dp[i][j],dp[i-1][j-k*a[i].v]+k*a[i].w);
					}
				}
			}
		}
		System.out.println(dp[n][v]);
	}
}
class Pair{
    
    
	int v,w;
	public Pair(int a,int b){
    
    
		v = a;
		w = b;
	}
}
class Read{
    
    
	StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
	public int nextInt() throws Exception{
    
    
		st.nextToken();
		return (int)st.nval;
	}
}

optimization (state optimization)

The state transition equation can be obtained by state division:
f ( i , j ) = Max ( f ( i − 1 , j ) , f ( i − 1 , j − vi ) + wi , f ( i − 1 , j − 2 ∗ vi ) + 2 ∗ wi , … … , f ( i − 1 , j − k ∗ vi ) + k ∗ wi ) f(i,j) = Max(f(i-1,j),f(i- 1,j-vi)+wi,f(i-1,j-2*vi)+2*wi,...,f(i-1,jk*vi)+k*wi)f(i,j)=Max(f(i1,j),f(i1,jvi)+wi,f(i1,j2vi)+2wi,,f(i1,jkvi)+kwi)

f ( i , j − vi ) = M ax ( f ( i − 1 , j − vi ) , f ( i − 1 , j − 2 ∗ vi ) + wi , … … , f ( i − 1 , j − k ∗ vi ) + ( k − 1 ) ∗ wi ) f(i,j-vi) = ~~~~~~~~~~~~~~Max(f(i-1,j-vi),~~ ~~~~~~~f(i-1,j-2*vi)+wi,……,f(i-1,jk*vi)+(k-1)*wi)f(i,jvi)=              Max(f(i1,jvi),         f(i1,j2vi)+wi,,f(i1,jkvi)+(k1)wi)

Combined to obtain the final state transition equation:
f ( i , j ) = Max ( f ( i − 1 , j ) , f ( i , j − vi ) + wi ) f(i,j) = Max(f(i -1,j),f(i,j-vi)+wi)f(i,j)=Max(f(i1,j),f(i,jvi)+wi)

core code

		for(int i=1;i<=n;i++) {
    
    
			for(int j=1;j<=v;j++) {
    
    
				dp[i][j] = dp[i-1][j];
				if(a[i].v<=j) {
    
    
					dp[i][j] = Math.max(dp[i][j],dp[i][j-a[i].v]+a[i].w);
				}
			}
		}

full code

package acWing003;
//	读入和Pair类都在朴素版
public class Main2 {
    
    
	static int N = 1010;
	static int n,v;
	static Pair a[] = new Pair[N];
	static int dp[][] = new int[N][N];
	public static void main(String[] args) throws Exception{
    
    
		Read r = new Read();
		n = r.nextInt();v = r.nextInt();
		for(int i=1;i<=n;i++) {
    
    
			a[i] = new Pair(r.nextInt(),r.nextInt());
		}
		for(int i=1;i<=n;i++) {
    
    
			for(int j=1;j<=v;j++) {
    
    
				dp[i][j] = dp[i-1][j];
				if(a[i].v<=j) {
    
    
					dp[i][j] = Math.max(dp[i][j],dp[i][j-a[i].v]+a[i].w);
				}
			}
		}
		System.out.println(dp[n][v]);
	}
}

Optimization (use rolling array dimensionality reduction)

The combined final state transition equation is:
f ( i , j ) = Max ( f ( i − 1 , j ) , f ( i , j − vi ) + wi ) f(i,j) = Max(f( i-1,j) , f(i,j-vi)+wi)f(i,j)=Max(f(i1,j),f(i,jvi)+w i )
It can be seen from the state equation that only the current line and the previous line need to be used, so the concept of rolling array can be used to reduce the two-dimensional to one-dimensional, namely:
f ( j ) = Max ( f ( j ) , f ( j − vi ) + wi ) f(j) = Max(f(j),f(j-vi)+wi)f(j)=Max(f(j),f(jvi)+wi)

core code

		for(int i=1;i<=n;i++) {
    
    
			for(int j=a[i].v;j<=v;j++) {
    
    
				dp[j] = Math.max(dp[j],dp[j-a[i].v]+a[i].w);
			}
		}

full code

package acWing003;
//	读入和Pair类都在朴素版
public class Main3 {
    
    
	static int N = 1010;
	static int n,v;
	static Pair a[] = new Pair[N];
	static int dp[] = new int[N];
	public static void main(String[] args) throws Exception{
    
    
		Read r = new Read();
		n = r.nextInt();v = r.nextInt();
		for(int i=1;i<=n;i++) {
    
    
			a[i] = new Pair(r.nextInt(),r.nextInt());
		}
		for(int i=1;i<=n;i++) {
    
    
			for(int j=a[i].v;j<=v;j++) {
    
    
				dp[j] = Math.max(dp[j],dp[j-a[i].v]+a[i].w);
			}
		}
		System.out.println(dp[v]);
	}
}

Guess you like

Origin blog.csdn.net/gudada010/article/details/117490503