【BZOJ】1688: [Usaco2005 Open]Disease Manangement 疾病管理(状压dp)

http://www.lydsy.com/JudgeOnline/problem.php?id=1688

很水的状压。。

提交了很多次优化的,但是还是100msT_T

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }

const int N=1005, M=70000;
int f[M], tot, n, d, k, b[17], c[N], ans;

bool ck(int i) {
	int s=0;
	while(i) ++s, i-=i&-i;
	return s<=k;
}

int main() {
	read(n); read(d); read(k);
	for1(i, 1, 15) b[i]=1<<(i-1);
	tot=(1<<d)-1;
	for1(i, 1, n) {
		int t=getint();
		while(t--) c[i]+=b[getint()];
	}
	for1(l, 1, n) {
		int t=c[l];
		for3(i, tot, 0)
			f[i|t]=max(f[i|t], f[i]+1);
	}
	for1(i, 0, tot) if(ck(i)) ans=max(ans, f[i]);
	print(ans);
	return 0;
}

 

 


 

 

Description

Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) is running through the farm. Farmer John would like to milk as many of his N (1 <= N <= 1,000) cows as possible. If the milked cows carry more than K (1 <= K <= D) different diseases among them, then the milk will be too contaminated and will have to be discarded in its entirety. Please help determine the largest number of cows FJ can milk without having to discard the milk.

Input

* Line 1: Three space-separated integers: N, D, and K * Lines 2..N+1: Line i+1 describes the diseases of cow i with a list of 1 or more space-separated integers. The first integer, d_i, is the count of cow i's diseases; the next d_i integers enumerate the actual diseases. Of course, the list is empty if d_i is 0. 有N头牛,它们可能患有D种病,现在从这些牛中选出若干头来,但选出来的牛患病的集合中不过超过K种病.

Output

* Line 1: M, the maximum number of cows which can be milked.

Sample Input

6 3 2
0---------第一头牛患0种病
1 1------第二头牛患一种病,为第一种病.
1 2
1 3
2 2 1
2 2 1

Sample Output

5

OUTPUT DETAILS:

If FJ milks cows 1, 2, 3, 5, and 6, then the milk will have only two
diseases (#1 and #2), which is no greater than K (2).

HINT

Source

posted @ 2014-09-06 17:19  iwtwiioi  阅读(533)  评论(0编辑  收藏  举报