poj3281

来源:互联网 发布:设计淘宝店铺标志图片 编辑:程序博客网 时间:2024/06/05 02:50

Dining
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 10034 Accepted: 4615

Description

Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.

Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although he might not be able to stuff everybody, he wants to give a complete meal of both food and drink to as many cows as possible.

Farmer John has cooked F (1 ≤ F ≤ 100) types of foods and prepared D (1 ≤ D ≤ 100) types of drinks. Each of his N (1 ≤ N ≤ 100) cows has decided whether she is willing to eat a particular food or drink a particular drink. Farmer John must assign a food type and a drink type to each cow to maximize the number of cows who get both.

Each dish or drink can only be consumed by one cow (i.e., once food type 2 is assigned to a cow, no other cow can be assigned food type 2).

Input

Line 1: Three space-separated integers: NF, and D 
Lines 2..N+1: Each line i starts with a two integers Fi and Di, the number of dishes that cow i likes and the number of drinks that cow i likes. The next Fi integers denote the dishes that cow i will eat, and theDi integers following that denote the drinks that cow i will drink.

Output

Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishes

Sample Input

4 3 32 2 1 2 3 12 2 2 3 1 22 2 1 3 1 22 1 1 3 3

Sample Output

3

Hint

One way to satisfy three cows is: 
Cow 1: no meal 
Cow 2: Food #2, Drink #2 
Cow 3: Food #1, Drink #1 
Cow 4: Food #3, Drink #3 
The pigeon-hole principle tells us we can do no better since there are only three kinds of food or drink. Other test data sets are more challenging, of course.

题意:n头牛,F种食物,D种饮料。每种牛都有自己喜欢的食物和饮料。

每种食物和饮料均只有一份,现在为这些牛分配食物和饮料,问最多能使多少头牛同时分配到食物和饮料。

此题最容易想到的是将源点与牛相连,但这样需要将食物和饮料拆分,这样做也未尝不可。但有一种方法就是将牛放中间,左边食物,右边饮料,这里牛必须拆点,否则无法保证流过一头牛的流量小于等于1。

建模就非常简单了,给食物编号1...F,饮料F+1...F+D,第i头牛拆分成F+D+i和F+D+n+i,源点0,汇点F+D+2*n+1

然后源点连向食物,饮料连向汇点,F+D+i连向F+D+n+i,最后将牛喜欢的食物连向F+D+i,将F+D+n+i连向牛喜欢的饮料,所有边权都为1。跑一边dinic即可。

代码:

#include<cstdio>#include<iostream>#include<cstring>#define Maxn 410using namespace std;const int inf=0x3f3f3f3f;struct line{    int to,next,cap;}p[Maxn*Maxn];int head[Maxn];int q[Maxn];int d[Maxn];int tot;int src,t;int n,F,D;void addedge(int a,int b,int c){    p[tot].to=b;    p[tot].next=head[a];    p[tot].cap=c;    head[a]=tot++;}void insert(int a,int b,int c){    addedge(a,b,c);    addedge(b,a,0);}bool bfs(){    memset(d,-1,sizeof d);    int s=0,e=-1;    q[++e]=src;    d[src]=0;    while(s<=e){        int u=q[s++];        for(int i=head[u];i!=-1;i=p[i].next){            int v=p[i].to;            if(d[v]==-1&&p[i].cap){                d[v]=d[u]+1;                q[++e]=v;            }        }    }    return d[t]!=-1;}int dfs(int u,int alpha){    if(u==t) return alpha;    int w,used=0;    for(int i=head[u];i!=-1&&used<alpha;i=p[i].next){        int v=p[i].to;        if(p[i].cap&&d[v]==d[u]+1){            w=dfs(v,min(alpha-used,p[i].cap));            used+=w;            p[i].cap-=w;            p[i^1].cap+=w;        }    }    if(!used) d[u]=-1;    return used;}int dinic(){    int ans=0;    src=0,t=F+D+2*n+1;    while(bfs())        ans+=dfs(src,inf);    return ans;}int main(){    int FF,DD,x;    while(cin>>n>>F>>D){        memset(head,-1,sizeof head);        tot=0;        for(int i=1;i<=F;i++) insert(0,i,1);        for(int i=1;i<=D;i++) insert(F+i,F+D+2*n+1,1);        for(int i=1;i<=n;i++) insert(F+D+i,F+D+i+n,1);        for(int i=1;i<=n;i++){            scanf("%d%d",&FF,&DD);            for(int j=1;j<=FF;j++){                scanf("%d",&x);                insert(x,F+D+i,1);            }            for(int j=1;j<=DD;j++){                scanf("%d",&x);                insert(F+D+i+n,F+x,1);            }        }        printf("%d\n",dinic());    }return 0;}


0 0
原创粉丝点击