背包问题模板

背包问题

  • 01背包(不需要装满)
  • 01背包(需要装满)
  • 完全背包
  • 多重背包(一维空间)
  • 多重背包(二进制)
  • 混合背包
  • 二维费用
  • 分组背包

01背包(不需要装满)

import java.util.Scanner;

public class ZeroOnePack {
	    static int N = 1010;
    static int INF = 0x3f3f3f3f;
    static int[] dp = new int[N];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int v = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            int weight = sc.nextInt();
            int value = sc.nextInt();
            for (int j = v; j >= weight; j--) {
                dp[j] = Math.max(dp[j], dp[j - weight] + value);
            }
        }
        System.out.println(dp[v]);
        sc.close();
    }
}

01背包(需要装满)

import java.util.Scanner;

public class ZeroOnePack {
    static int N = 1010;
    static int INF = 0x3f3f3f3f;
    static int[] dp = new int[N];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int v = sc.nextInt();
        //除了dp[0]填0,其他需要以-∞填充,其他背包问题同理
        Arrays.fill(dp, -INF);
        dp[0] = 0;
        for (int i = 1; i <= n; i++) {
            int weight = sc.nextInt();
            int value = sc.nextInt();
            for (int j = v; j >= weight; j--) {
                dp[j] = Math.max(dp[j], dp[j - weight] + value);
            }
        }
        System.out.println(dp[v]);
        sc.close();
    }
}

完全背包

import java.util.Scanner;

public class CompletePack {
	static int N=1010;
    static int[] dp = new int[N];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int v = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            int weight = sc.nextInt();
            int value = sc.nextInt();
            for (int j = weight; j <=v; j++) {
                dp[j] = Math.max(dp[j], dp[j - weight] + value);
            }
        }
        System.out.println(dp[v]);
        sc.close();
    }

}

多重背包(一维空间)


import java.util.Scanner;

public class MultiPack {
    static int N = 110;
    static int[] dp = new int[N];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int v = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            int weight = sc.nextInt();
            int value = sc.nextInt();
            int amount = sc.nextInt();
            for (int j = v; j >=0; j--) {
                for (int k = 0; k <= amount && j >= k * weight; k++) {
                    dp[j] = Math.max(dp[j], dp[j - k * weight] + k * value);
                }
            }
        }
        System.out.println(dp[v]);
        sc.close();
    }
}

多重背包(二进制)


import java.util.ArrayList;
import java.util.Scanner;

class goods {
    int weight, value;

    public goods(int weight, int value) {
        this.weight = weight;
        this.value = value;
    }
}


public class MultiPack {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        var Good = new ArrayList<goods>();
        int[] dp = new int[2010];
        int n = sc.nextInt();
        int v = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            int weight = sc.nextInt();
            int value = sc.nextInt();
            int amont = sc.nextInt();
            for (int j = 1; j <= amont; j *= 2) {
                amont -= j;
                Good.add(new goods(weight * j, value * j));
            }
            if (amont > 0) {
                Good.add(new goods(weight * amont, value * amont));
            }
        }
        for (var good :
                Good) {
            for (int i = v; i >= good.weight; i--) {
                dp[i] = Math.max(dp[i], dp[i - good.weight] + good.value);
            }
        }

        System.out.println(dp[v]);
        sc.close();
    }
}

混合背包

import java.util.ArrayList;
import java.util.Scanner;

class goods {
    int weight, value, kind;

    public goods(int weight, int value, int kind) {
        this.weight = weight;
        this.value = value;
        this.kind = kind;
    }
}

public class Main {
    static int n, v;
    static int[] dp = new int[2010];
    static ArrayList<goods> GOOD = new ArrayList<>();

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        v = sc.nextInt();
        for (int i = 0; i < n; i++) {
            int weight = sc.nextInt();
            int value = sc.nextInt();
            int kind = sc.nextInt();
            GOOD.add(new goods(weight, value, kind));
        }
        for (var good :
                GOOD) {
            if (good.kind == -1) {
                ZeroOnePack(good.weight, good.value);
            } else if (good.kind == 0) {
                CompletePack(good.weight, good.value);
            } else {
                MultiplePack(good.weight, good.value, good.kind);
            }
        }
        System.out.println(dp[v]);
        sc.close();
    }

    static void ZeroOnePack(int weight, int value) {
        for (int i = v; i >= weight; i--) {
            dp[i] = Math.max(dp[i], dp[i - weight] + value);
        }
    }

    static void CompletePack(int weight, int value) {
        for (int i = weight; i <= v; i++) {
            dp[i] = Math.max(dp[i], dp[i - weight] + value);
        }
    }

    static void MultiplePack(int weight, int value, int amount) {
        if (amount * weight >= v) {
            CompletePack(weight, value);
            return;
        }
        int k = 1;
        while (k < amount) {
            ZeroOnePack(k * weight, k * value);
            amount = amount - k;
            k = k * 2;
        }
        ZeroOnePack(amount * weight, amount * value);
    }
}

二维费用

import java.util.Scanner;

public class Main {

    static int N = 1010;
    static int n, v, m;
    static int[][] dp = new int[N][N];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        v = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            int volume = sc.nextInt();
            int weight = sc.nextInt();
            int value = sc.nextInt();
            for (int j = v; j >= volume; j--) {
                for (int k = m; k >= weight; k--) {
                    dp[j][k] = Math.max(dp[j][k], dp[j - volume][k - weight] + value);
                }
            }

        }
        System.out.println(dp[v][m]);
        sc.close();
    }
}

分组背包

import java.util.Scanner;

public class Main {
    static int N = 110;
    static int[] dp = new int[N];
    static int[] s = new int[N];
    static int[][] weight = new int[N][N];
    static int[][] value = new int[N][N];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int v = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            s[i] = sc.nextInt();
            for (int j = 1; j <= s[i]; j++) {
                weight[i][j]=sc.nextInt();
                value[i][j]=sc.nextInt();
            }
        }
        for (int i = 1; i <=n ; i++) {
            for (int j = v; j >=0 ; j--) {
                for (int k = 1; k <=s[i] ; k++) {
                    if (j>=weight[i][k]) {
                        dp[j] = Math.max(dp[j], dp[j - weight[i][k]] + value[i][k]);
                    }
                }
            }
        }
        System.out.println(dp[v]);
        sc.close();
    }
}

你可能感兴趣的:(#,算法模板)