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 twodiseases (#1 and #2), which is no greater than K (2).
可得f[i]表示疾病状态为i的最大牛数
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int n,ease,m,all,ans;
int a[1005][20];
int f[33000];
int work(int x)
{
int y=0;
while(x>0)
{
y=y+(x&1);
x=x>>1;
}
return y;
}
int main()
{
cin>>n>>ease>>m;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i][0]);
for(int j=1;j<=a[i][0];j++) scanf("%d",&a[i][j]);
}
all=(1<<ease)-1;
for(int i=1;i<=n;i++)
for(int j=all;j>=0;j--) //一定要倒着循环,正着循环会把牛重复计算
{
int v=j; //枚举父状态
for(int k=1;k<=a[i][0];k++)
{
int x=(1<<(a[i][k]-1));
v=(v|x); //把当前疾病都or上,形成新状态
}
f[v]=max(f[v],f[j]+1);
}
for(int i=0;i<=all;i++)
if(work(i)<=m) ans=max(ans,f[i]);
cout<<ans;
return 0;
}
在本篇博文中,我们探讨了一个农场面对多种疾病时如何通过算法选择最多数量的奶牛进行挤奶,同时确保挤出的牛奶不会因疾病种类过多而被废弃。通过状态压缩动态规划的方法,有效地解决了这一问题。
184

被折叠的 条评论
为什么被折叠?



