【[Offer收割]编程练习赛11 B】物品价值
【题目链接】:http://hihocoder.com/problemset/problem/1486
【题意】
【题解】
设f[i][j]表示前i个物品,每种属性的状态奇偶状态为j的最大价值;
这里用j的二进制对应每种属性的状态;
为1表示那种属性的物品个数为奇数否则为偶数
f[i][j] = max(f[i-1][j],f[i-1][j^zt[i]]+jz[i]);
zt[i]是第i个物品拥有的属性的状态,jz[i]是第i个物品的价值
最后输出f[n][(1<< m)-1]就好;
【Number Of WA】
1
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1100;
const int ZT = 1050;
int jz[N],zt[N],n,m,f[N][ZT];
int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf就别用了!
rep1(i,0,ZT-1) f[0][i] = -1;
int T;
cin >> T;
while (T--)
{
cin >> n >> m;
rep1(i,1,n)
{
int sl;
cin >> jz[i] >> sl;
zt[i] = 0;
rep1(j,1,sl)
{
int ng;
cin >> ng;
zt[i]|=(1<<(ng-1));
}
}
f[0][0] = 0;
rep1(i,1,n)
rep1(j,0,(1<<m)-1)
{
f[i][j] = -1;
//不选
if (f[i-1][j]!=-1) f[i][j] = f[i-1][j];
//选
int pre = j^zt[i];
if (f[i-1][pre]!=-1)
f[i][j] = max(f[i][j],f[i-1][pre]+jz[i]);
}
cout << f[n][(1<<m)-1] << endl;
}
return 0;
}