01背包问题Java实现
【题目描述】
一个旅行者有一个最多能装 MM 公斤的背包,现在有 nn 件物品,它们的重量分别是W1,W2,...,WnW1,W2,...,Wn,它们的价值分别为C1,C2,...,CnC1,C2,...,Cn,求旅行者能获得最大总价值。
【输入】
第一行:两个整数,MM(背包容量,M≤200M≤200)和NN(物品数量,N≤30N≤30);
第2..N+12..N+1行:每行二个整数Wi,CiWi,Ci,表示每个物品的重量和价值。
【输出】
仅一行,一个数,表示最大总价值。
【输入样例】
10 4 2 1 3 3 4 5 7 9
【输出样例】
12
【代码样例1】
import java.io.IOException; /** * @Description * @Author * @Version V1.0.0 * @Since 1.0 */ public class PckTest { private static int[][] dp = new int[35][205]; private static int[] w = new int[35]; private static int[] c = new int[35]; public static void main(String[] args) throws IOException{ int m; int n; m = Integer.parseInt(ConsoleInput.readToWhiteSpace(true)); n = Integer.parseInt(ConsoleInput.readToWhiteSpace(true)); for (int i = 1; i <= n; i++) { w[i] = Integer.parseInt(ConsoleInput.readToWhiteSpace(true)); c[i] = Integer.parseInt(ConsoleInput.readToWhiteSpace(true)); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (j < w[i]) { dp[i][j] = dp[i - 1][j]; } else { dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - w[i]] + c[i]); } } } for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { System.out.print(dp[i][j]); System.out.print(" "); } System.out.print("\n"); } System.out.print(dp[n][m]); } }
【代码样例2】
static private int[] w = new int[35]; static private int[] c = new int[35]; static private int[] dp = new int[205]; public static void main(String[] args) throws IOException { int m; int n; m = Integer.parseInt(ConsoleInput.readToWhiteSpace(true)); n = Integer.parseInt(ConsoleInput.readToWhiteSpace(true)); for (int i = 1; i <= n; i++) { w[i] = Integer.parseInt(ConsoleInput.readToWhiteSpace(true)); c[i] = Integer.parseInt(ConsoleInput.readToWhiteSpace(true)); } for (int i = 1; i <= n; i++) { for (int j = m; j >= 1; j--) { if (j >= w[i]) { dp[j] = Math.max(dp[j], dp[j - w[i]] + c[i]); } } } System.out.print(dp[m]); }
输入代码
final class ConsoleInput { private static boolean goodLastRead = false; public static boolean lastReadWasGood() { return goodLastRead; } public static String readToWhiteSpace(boolean skipLeadingWhiteSpace) throws IOException { String input = ""; char nextChar; while (Character.isWhitespace(nextChar = (char) System.in.read())) { //accumulate leading white space if skipLeadingWhiteSpace is false: if (!skipLeadingWhiteSpace) { input += nextChar; } } //the first non white space character: input += nextChar; //accumulate characters until white space is reached: while (!Character.isWhitespace(nextChar = (char) System.in.read())) { input += nextChar; } goodLastRead = input.length() > 0; return input; } public static String scanfRead() throws IOException{ return scanfRead(null, -1); } public static String scanfRead(String unwantedSequence) throws IOException{ return scanfRead(unwantedSequence, -1); } public static String scanfRead(String unwantedSequence, int maxFieldLength) throws IOException{ String input = ""; char nextChar; if (unwantedSequence != null) { nextChar = '\0'; for (int charIndex = 0; charIndex < unwantedSequence.length(); charIndex++) { if (Character.isWhitespace(unwantedSequence.charAt(charIndex))) { //ignore all subsequent white space: while (Character.isWhitespace(nextChar = (char) System.in.read())) { } } else { //ensure each character matches the expected character in the sequence: nextChar = (char) System.in.read(); if (nextChar != unwantedSequence.charAt(charIndex)) return null; } } input = (new Character(nextChar)).toString(); if (maxFieldLength == 1) return input; } while (!Character.isWhitespace(nextChar = (char) System.in.read())) { input += nextChar; if (maxFieldLength == input.length()) return input; } return input; } }
结果