标签:i++ new 价值 限制 static ext 思想 class next
多重背包
有N种物品和一个容量为V的背包。第i种物品最多有n[i]件可用,每件费用是c[i],价值 是w[i]。求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大
母函数的思想也是如此 给你 价值, 物品数量的限制, 然后凑,
hdu2191
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); int m = sc.nextInt(); int v[] = new int[101]; int w[] = new int[101]; int num[] = new int[101]; for (int i = 0; i < m; i++) { v[i] = sc.nextInt(); w[i] = sc.nextInt(); num[i] = sc.nextInt(); } int dp[] = new int[10001]; for (int i = 0; i <= m; i++) dp[i] = 0; for (int i = 0; i < m; i++) { for (int k = 0; k <= num[i]; k++) { for (int j = n; j >= v[i]; j--) { if (dp[j] < dp[j - v[i]] + w[i]) { dp[j] = dp[j - v[i]] + w[i]; } } } } System.out.println(dp[n]); } } }
标签:i++ new 价值 限制 static ext 思想 class next
原文地址:https://www.cnblogs.com/ls-pankong/p/10490891.html