POJ 3281 Dining(sap模板大法好)

来源:互联网 发布:数据流程图符号含义 编辑:程序博客网 时间:2024/05/01 05:35

题目链接        Dining
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 18739 Accepted: 8344

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-----牛1      水1

源点s      食物2     牛2-----牛2      水2      终点

               食物3     牛3-----牛3      水3

               食物4     牛4-----牛4      水4

牛和牛之间有一条路为1,控制每只牛最多拥有一种食物。

AC代码:

#include <stdio.h>#include <string.h>#define maxn  500#define maxx  30000#define inf   0x3fffffff#define MIN(a,b) a>b?b:a#define mems(p) memset(p,0,sizeof(p))struct E{    int to,val,next;} e[maxx];int nn[maxn],ff[maxn],dd[maxn],nnn[maxn];int vs,S,T,cnt;int dis[maxn],gap[maxn],head[maxn];void add(int from,int to,int val){    e[cnt].to=to;    e[cnt].val=val;    e[cnt].next=head[from];    head[from]=cnt++;    e[cnt].to=from;    e[cnt].val=0;    e[cnt].next=head[to];    head[to]=cnt++;}int dfs(int s,int aug){    if(s==T) return aug;    int flow=0,mi=vs-1;    for(int i=head[s]; i; i=e[i].next)        if(e[i].val)        {            if(dis[s]==dis[e[i].to]+1)            {                int t=dfs(e[i].to,MIN(aug-flow,e[i].val));                e[i].val-=t;                e[i^1].val+=t;                flow+=t;                if(dis[S]>=vs)return flow;                if(aug==flow)break;            }            mi=MIN(mi,dis[e[i].to]);        }    if(!flow)    {        if(!(--gap[dis[s]]))            gap[S]=vs;        ++gap[dis[s]=mi+1];    }    return flow;}int slove(int s,int ed){    mems(gap);    mems(dis);    gap[0]=vs=ed;    S=s;    T=ed;    int ans=0;    while(dis[S]<vs)        ans+=dfs(S,inf);    return  ans;}int main(){    int n,d,f,a,b,c;    while(~scanf("%d%d%d",&n,&f,&d))    {        mems(head);        cnt=2;//因为head数组我清零了,所以不能出现编号为零的路        int num=2;        for(int i=1; i<=f; i++)  ff[i]=num++;//对所有的节点进行编号        for(int i=1; i<=n; i++)  nn[i]=num++;        for(int i=1; i<=n; i++)  nnn[i]=num++;        for(int i=1; i<=d; i++)  dd[i]=num++;        for(int i=1;i<=f;i++)  add(1,ff[i],1);//建边        for(int i=1;i<=n;i++)  add(nn[i],nnn[i],1);        for(int i=1;i<=d;i++)  add(dd[i],num,1);        for(int i=1;i<=n;i++)        {            scanf("%d%d",&a,&b);            for(int j=1;j<=a;j++)            {                scanf("%d",&c);                add(ff[c],nn[i],1);            }            for(int j=1;j<=b;j++)            {                scanf("%d",&c);                add(nnn[i],dd[c],1);            }        }        printf("%d\n",slove(1,num));    }}