首页 > 其他分享 >分组背包问题

分组背包问题

时间:2023-02-13 13:00:34浏览次数:45  
标签:std 背包 const cout 210 int max 问题 分组

//没有状态压缩

#include<bits/stdc++.h>
using namespace std;

const int N=210;

int n,m;
int v[N][N],w[N][N],s[N];
int f[N][N];

int main()
{
    cin>>n>>m;
    
    for(int i=1;i<=n;i++)
    {
        cin>>s[i];
        for(int j=1;j<=s[i];j++)
        cin>>v[i][j]>>w[i][j];
    }
    
    for(int i=1;i<=n;i++)
    for(int j=0;j<=m;j++)
    for(int k=0;k<=s[i];k++)
    if(j>=v[i][k])
    f[i][j]=max(f[i][j],f[i-1][j-v[i][k]]+w[i][k]);
    
    cout<<f[n][m];
    
    return 0;
}
//状态压缩
#include<bits/stdc++.h>
using namespace std;

const int N=210;

int n,m;
int v[N][N],w[N][N],s[N];
int f[N];

int main()
{
    cin>>n>>m;
    
    for(int i=1;i<=n;i++)
    {
        cin>>s[i];
        for(int j=1;j<=s[i];j++)
        cin>>v[i][j]>>w[i][j];
    }
    
    for(int i=1;i<=n;i++)
    for(int j=m;j>=0;j--)
    for(int k=0;k<=s[i];k++)
    if(j>=v[i][k])
    f[j]=max(f[j],f[j-v[i][k]]+w[i][k]);
    
    cout<<f[m];
    
    return 0;
}

 

标签:std,背包,const,cout,210,int,max,问题,分组
From: https://www.cnblogs.com/tolter/p/17115965.html

相关文章

  • 排列问题
    有n个人排成一队散步,第二天每个人都不想和前一天前面的人相同,多少种排列n!-(n-1)(n-1)!+(n-2)(n-2)!-...1*1模拟n=3#include<iostream>#include<algorithm>#include<......
  • video设置mix-blend-mode后造成的问题
    在项目,有个业务需要展示一个视频,video标签设置src后,发现一个1个,视频下方有一块黑色的,百度后,对视频进行样式处理,增加【mix-blend-mode:screen】,这一加,就造成了2个问题,经过排......
  • 【问题讨论】关于golang调用so的问题的讨论
    runtime:dlopen/dlsymwithoutCGo#18296 Open  iamacarpetopenedthisissueDec13,2016·12comments  Open  ......
  • IDEA中Tomcat在控制台乱码问题
    首先要分清是tomcat日志编码,与idea的日志显示控制台编码tomcat日志编码:cmd内“cd/dtomcat根目录”“bin\catalina.batrun”运行,"chcp65001"切换cmd为utf8,"chcp936"切......
  • 解决curl中文乱码问题
    文章目录​​1、问题描述​​​​2、解决方案:安装iconv​​1、问题描述curl下载地址:​​https://curl.se/download.html​​​在执行命令​​curlwww.baidu.com​​的时候......
  • RuntimeError: Input, output and indices must be on the current device 问题
    RuntimeError:Input,outputandindicesmustbeonthecurrentdevice做项目时遇到这个问题明明就已经加了这条语句,使其运行在我设置好的设备上了。为何会报错呢?......
  • 解决将Editplus添加到鼠标右键的问题
    解决将Editplus添加到鼠标右键的问题以管理员身份运行EditPlus一次点击–>工具–>首选项–>常规–>勾选将EditPlus添加到系统右键菜单选项......
  • MySQL按日期分组统计(按天统计,按月统计)
    以下为您演示MySQL常用的日期分组统计方法:按月统计(一)SELECTdate_format(r.pay_time,'%Y-%m')pay_time,r.pay_time,SUM(r.actual_payment)ASactu......
  • 【AGC】禁用华为签名问题
    1.关于AGC的禁用华为签名的问题。问题背景:cp启用华为签名后,咨询如何禁用华为重签名,走app自己的签名校验。应用接入了微信支付后,微信支付报错【签名不对,请检查签名是否与......
  • JAVA - - - HashMap常见问题解答
    HashMap与ConcurrentHashMap的异同都是key-value形式的存储数据;HashMap是线程不安全的,ConcurrentHashMap是JUC下的线程安全的;HashMap底层数据结构是数组+......