背包。注释写详细了。
本想这样写:每个组内各自做背包,然后组间做背包,但是由于这题M=10000,时间复杂度太大。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<functional>
using namespace std; const int maxn = + ;
int n, m, g; vector<int>G[]; int cost[ + ];
int val[ + ];
int y[];
bool flag[ + ];
int dp1[ + ];
int dp2[ + ]; void init()
{
memset(flag, , sizeof flag);
for (int i = ; i <= g; i++) G[i].clear();
} int main()
{
while (~scanf("%d%d", &n, &m)){ for (int i = ; i <= n; i++) scanf("%d", &cost[i]);
for (int i = ; i <= n; i++) scanf("%d", &val[i]);
scanf("%d", &g);
init();
for (int i = ; i <= g; i++)
{
int f;
scanf("%d", &f);
for (int j = ; j <= f; j++)
{
int id;
scanf("%d", &id);
G[i].push_back(id);
flag[id] = ;
}
scanf("%d", &y[i]);
} memset(dp1, , sizeof dp1); //没有组的归为一组
for (int i = ; i <= n; i++)
{
if (flag[i]) continue;
for (int j = m; j >= cost[i]; j--)
dp1[j] = max(dp1[j], dp1[j - cost[i]] + val[i]);
} for (int k = ; k <= g; k++)
{
//此时dp2数组存储了之前的所有组合最优解
for (int i = ; i <= m; i++) dp2[i] = dp1[i]; //接下来假设不买完这个组合内的魔方,这样DP的话事实上也存在买完的组合,但会被之后的dp2更新,所以不存在问题
for (int s = ; s < G[k].size(); s++)
{
for (int j = m; j >= cost[G[k][s]]; j--)
{
dp1[j] = max(dp1[j], dp1[j - cost[G[k][s]]] + val[G[k][s]]);
}
} //假设买完,用dp2更新
int sum_cost = , sum_val = y[k];
for (int s = ; s < G[k].size(); s++)
{
sum_cost = sum_cost + cost[G[k][s]];
sum_val = sum_val + val[G[k][s]];
} for (int j = m; j >= sum_cost; j--)
{
dp2[j] = max(dp2[j], dp2[j - sum_cost] + sum_val);
} //然后dp1和dp2存下最大值
for (int i = ; i <= m; i++) dp1[i] = max(dp1[i], dp2[i]);
} printf("%d\n", dp1[m]);
}
return ;
}