背包解法2

分组背包:

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m;
const int N=105;
int f[N],v[N],w[N];
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        int s;
        cin>>s;
        for(int j=1;j<=s;j++)
        cin>>v[j]>>w[j];
        for(int j=m;j>=0;j--)
        for(int k=1;k<=s;k++)
        if(j>=v[k])
        f[j]=max(f[j],f[j-v[k]]+w[k]);
    }
    cout<<f[m];
    return 0;
}

  

posted @ 2020-07-29 19:01  喜欢爬的孩子  阅读(101)  评论(0编辑  收藏  举报