AcWing 5. 多重背包问题 II-java-二进制拆分
原题链接
中等
作者:
Astarion
,
2021-02-19 16:23:26
,
所有人可见
,
阅读 233
import java.io.*;
class Main {
static int N, V;
static int[] v = new int[20010];
static int[] w = new int[20010];
static int[] s = new int[20010];
static int len;
static int[] f = new int[2010];
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
String[] strs = in.readLine().split(" ");
N = Integer.parseInt(strs[0]);
V = Integer.parseInt(strs[1]);
for (int i = 1; i<=N; i++) {
strs = in.readLine().split(" ");
int vv = Integer.parseInt(strs[0]);
int ww = Integer.parseInt(strs[1]);
int ss = Integer.parseInt(strs[2]);
int j = 1, t = ss;
while (j<=t) {
v[++len] = vv*j;
w[len] = ww*j;
t -= j;
j *= 2;
}
if (t>0) {
v[++len] = vv*t;
w[len] = ww*t;
}
}
in.close();
isr.close();
OutputStreamWriter osw = new OutputStreamWriter(System.out);
BufferedWriter out = new BufferedWriter(osw);
for (int i = 1; i<=len; i++)
for (int j = V; j>=v[i]; j--)
f[j] = Math.max(f[j], f[j-v[i]]+w[i]);
out.write(f[V]+"");
out.flush();
out.close();
osw.close();
}
}