在完全背包的基础上加了数量限制,思路也很简单,在完全背包基础上循环一边限制量就可解决。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(System.out);
static int N = 110;
static int n, m;
static int v[] = new int[N], w[] = new int[N], c[] = new int[N];
static int f[] = new int[N];
public static void main(String[] args) throws IOException {
String[] s = br.readLine().split(" ");
n = Integer.parseInt(s[0]);
m = Integer.parseInt(s[1]);
for (int i = 1; i <= n; i++) {
s = br.readLine().split(" ");
w[i] = Integer.parseInt(s[0]);
v[i] = Integer.parseInt(s[1]);
c[i] = Integer.parseInt(s[2]);
}
for (int i = 1; i <= n; i++)
for (int j = m; j >= 0; j--)
for (int k = 0; k <= c[i]; k++)
if (j >= k * w[i])
f[j] = Math.max(f[j], f[j - k * w[i]] + k * v[i]);
pw.println(f[m]);
pw.flush();
pw.close();
br.close();
}
}