01背包(阴阳师)

#include <bits/stdc++.h>
using namespace std;
int dp[1005],f[1005];
int main(){
    ios::sync_with_sidio(false);
    int t;
    cin >>t;
    while(t--){
        memset(dp,0,sizeof(dp));
        int n,m;
        cin >> n >>m;
        int ans = 0;
        for(int i = 0;i <n;i ++){
            int w[1005],v[1005];
            int a;
            memcpy(f,dp,sizeof(f));
            cin >> a;
            for(int j = 0;j < a;j ++) cin >> v[j];
            for(int j = 0;j < a;j ++) cin >> w[j];
            for(int j = 0;j < a;j ++)
                for(int k=m; k>=w[j]; k--)
                    f[k]=max(f[k],dp[k-w[j]]+v[j]);//这里的比较很关键,决定了每种模式只选一个操作。
            memcpy(dp,f,sizeof(dp));
            /*for(int j = 0;j <= m;j ++)
            {
                cout<<dp[j]<<' ';
             } 
             cout<<endl;*/
        }
        cout<<dp[m]<<endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/lightac/p/10747490.html