AcWing 4. 多重背包问题(背包)

在完全背包的基础上加了数量限制,思路也很简单,在完全背包基础上循环一边限制量就可解决。

题目

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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值