The knapsack problem of Java brushing algorithm

01 backpack problem

1. Topic

There are N items and a knapsack with capacity V. Each item can only be used once. The volume of the i-th item is vi v_ivi,This is wi w_iwi. 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
The first line contains two integers, N and V, separated by spaces, representing the quantity of items and the volume of the backpack respectively. Next there are N lines, each with two integers vi , wi v_{i},w_ivi,wi, separated by spaces, respectively represent the volume and value of the i-th item.

Output Format
Output an integer representing the maximum value.

3. Test sample

输入样例
4 5
1 2
2 4
3 4
4 5
输出样例:
8

3. thought

Please add a picture description

4. Code

import java.util.Scanner;

public class 背包问题01 {
    
    
    //d[i][j]:前i件物品且总体积不大于j的最大价值
    public static void  main(String []args){
    
    
        final int maxn=1005;
        Scanner input=new Scanner(System.in);
        int N=input.nextInt(),V=input.nextInt();
        int []v=new int[maxn];
        int []w=new int[maxn];
        int [][]dp=new int [maxn][maxn];
        for(int i=1;i<=N;i++){
    
    
            v[i]=input.nextInt();
            w[i]=input.nextInt();
        }
        for(int i=1;i<=N;i++){
    
    
            for(int j=1;j<=V;j++){
    
    
                dp[i][j]=dp[i-1][j];//不选
                if(j>=v[i])
                dp[i][j]=Math.max(dp[i][j],dp[i-1][j-v[i]]+w[i]);
                
            }
        }
        System.out.println(dp[N][V]);
        input.close();
    }
}

complete knapsack problem

1. Topic

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 v_ivi,This is wi w_iwi. 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
The first line contains two integers, N and V, separated by spaces, which respectively represent the number of items and the volume of the backpack. Next there are N lines, each with two integers vij , wij v_{ij},w_{ij}vij,wij, separated by spaces, respectively represent the volume and value of the i-th item.

Output Format
Output an integer representing the maximum value.

3. Test sample

输入样例
4 5
1 2
2 4
3 4
4 5
输出样例:
10

4. thought

insert image description here

4. Code

import java.util.Scanner;

// 有 N 种物品和一个容量是 VV 的背包,每种物品都有无限件可用。
// 第 i 种物品的体积是 vi,价值是 wi。
// 求解将哪些物品装入背包,可使这些物品的总体积不超过背包容量,且总价值最大。
// 输出最大价值。
public class 完全背包 {
    
    
    public static void main(String []args){
    
    
        Scanner input=new Scanner(System.in);
        int N=input.nextInt();
        int V=input.nextInt();
        int w[]=new int[N+1];
        int v[]=new int[N+1];
        int dp[][]=new int[N+1][V+1];
        for(int i=1;i<=N;i++){
    
    
            v[i]=input.nextInt();
            w[i]=input.nextInt();
        }
        for(int i=1;i<=N;i++){
    
    
            for(int j=1;j<=V;j++){
    
    
                dp[i][j]=dp[i-1][j];
                if(v[i]<=j)dp[i][j]=Math.max(dp[i][j],dp[i][j-v[i]]+w[i]);
            }
        }
        System.out.println(dp[N][V]);
        input.close();
    }
    
}

group knapsack problem

1. Topic

There are N sets of items and a backpack with capacity V.
There are several items in each group, and only one item in the same group can be selected at most.
The volume of each item is vij v_{ij}vij, the value is wij w_{ij}wij, where i is the group number and j is the group number.
Solve which items to put into the backpack so that the total volume of the items does not exceed the capacity of the backpack and the total value is the largest.
output the maximum value.

Input format
The first line has two integers N, V, separated by a space, which represent the number of item groups and the capacity of the backpack respectively.
Next, there are N sets of data:
the first line of each set of data has an integer S i S_iSi, indicating the number of items in the i-th item group;
each group of data is followed by S i S_iSiLines, each line has two integers vij, wij v_{ij},w_{ij}vij,wij, separated by spaces, represent the volume and value of the j-th item in the i-th item group respectively;

Output Format
Output an integer representing the maximum value.

2. Test sample

输入样例:
3 5
2
1 2
2 4
1
3 4
1
4 5
输出样例:
8

3. thought

insert image description here

4. Code

import java.util.Scanner;

// 有 N 组物品和一个容量是 V 的背包。
// 每组物品有若干个,同一组内的物品最多只能选一个。
// 每件物品的体积是 vij,价值是 wij,其中 i 是组号,j 是组内编号。
// 求解将哪些物品装入背包,可使物品总体积不超过背包容量,且总价值最大。
// 输出最大价值。
public class Main {
    
    
    static int maxn=110;
    public static void main(String []args){
    
    
        Scanner input=new Scanner(System.in);
        int N=input.nextInt();
        int V=input.nextInt();
        int v[][]=new int[maxn][maxn];
        int w[][]=new int[maxn][maxn];
        int dp[][]=new int[maxn][maxn];
        int S[]=new int[maxn];
        for(int i=1;i<=N;i++)
        {
    
    
            S[i]=input.nextInt();
            for(int j=1;j<=S[i];j++){
    
    
            v[i][j]=input.nextInt();
            w[i][j]=input.nextInt();}
        }
        for(int i=1;i<=N;i++){
    
    
            for(int j=0;j<=V;j++){
    
    
                //不选
                dp[i][j]=dp[i-1][j];
                for(int k=1;k<=S[i];k++){
    
    
                    if(j>=v[i][k])
                    dp[i][j]=Math.max(dp[i][j],dp[i-1][j-v[i][k]]+w[i][k]);
                }
            }
        }
        System.out.println(dp[N][V]);
        input.close();
    }
}

Guess you like

Origin blog.csdn.net/m0_53192838/article/details/128705533