题解 | 【模板】分组背包

【模板】分组背包

https://www.nowcoder.com/practice/32a6c222213c42efa902da6b5c9f8e99

#include <iostream>
#include <vector>
#include <map>
using namespace std;

int main() {
    int n , M;
    cin>>n>>M;

    map<int,vector<pair<int,int>>> things;

    for(int i = 0 ; i < n ; i++){
        int w,v,g;
        cin>>w>>v>>g;

        things[g].push_back({w,v});
    }

    vector<vector<int>> dp(things.size()+1,vector<int>(M+1,0));

    for(int i = 1 ; i <= things.size() ; i++){
        for(int j = 0 ; j <= M ; j++){

            dp[i][j] = dp[i-1][j];
            
            for(auto& thing : things[i]){

                if(j < thing.first){
                    continue;
                }
                dp[i][j] = max(dp[i][j] , dp[i-1][j - thing.first] + thing.second);
            }


        }
    }


    cout<<dp[things.size()][M]<<endl;
}

全部评论

相关推荐

11-07 16:07
深圳大学 运营
前端飞升:学长,阿里不是卡双非吗,我深也能去吗
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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