题解 | #【模板】多重背包#

【模板】多重背包

https://www.nowcoder.com/practice/8fa10063d33a43dd9652c1511a34d461

不开long long见祖宗。

#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <stack>
#include <numeric>
#include <ctime>
#include <string>
#include <bitset>
#include <unordered_map>
#include <unordered_set>

using namespace std;
using ll = long long;

const ll N = 3e6 + 5, mod = 1e9 + 7, inf = 2e18;
const double esp = 1e-8;

ll dp[N], vv[N], ww[N];

void solve() {
    ll n, m;
    cin >> n >> m;
    memset(dp, 0, sizeof(dp));

    int num = 1;
    for (int i = 1; i <= n; i++) {
        int w, v, s;
        cin >> w >> v >> s;

        for (int j = 1; j <= s; j <<= 1) {

            ww[num] = w * j;
            vv[num++] = v * j;
            s -= j;
        }

        if (s) {
            ww[num] = s * w;
            vv[num++] = s * v;
        }

    }

    for (int i = 1; i < num; i++) {
        for (int j = m; j >= ww[i]; j--) {
            dp[j] = max(dp[j], dp[j - ww[i]] + vv[i]);
        }
    }
    cout << dp[m] << '\n';
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int t = 1;
    cin >> t;

    while (t--) {

        solve();

    }

    return 0;
}

全部评论

相关推荐

10-21 00:37
已编辑
门头沟学院 C++
小浪_Coding:你问别人,本来就是有求于人,别人肯定没有义务免费回答你丫, 有点流量每天私信可能都十几,几十条的,大家都有工作和自己的事情, 付费也是正常的, 就像你请别人搭把手, 总得给人家买瓶水喝吧
点赞 评论 收藏
分享
12-02 20:08
已编辑
门头沟学院 后端工程师
notbeentak...:孩子,说实话,选择很重要,可能你换一个方向会好很多,但是现在时间不太够了,除非准备春招
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务