Dining

来源:互联网 发布:淘宝app软件官方下载 编辑:程序博客网 时间:2024/06/11 22:01

题目

农夫JOHN为牛们做了很好的食品,但是牛吃饭很挑食。每一头牛只喜欢吃一些食品和饮料而别的一概不吃。虽然他不一定能把所有牛喂饱,他还是想让尽可能多的牛吃到他们喜欢的食品和饮料。
  农夫JOHN做了F (1<=F<=100) 种食品和准备了D(1<=D<=100)种饮料。他有N(1<=N<=100)头牛,现在已经知道他的每头牛是否愿意吃某种食物和喝某种饮料。农夫JOHN想给每一头牛一种食品和一种饮料,使得尽可能多的牛得到喜欢的食物和饮料。
  每一件食物和饮料只能由一头牛来用。例如如果食物2被一头牛吃掉了,没有别的牛能吃食物2。

分析

很明显的约束条件,我们可以往网络流的方向想
我们会想到每次流出的值都是一种合法的方案,所以我们便可以接着往这个方向去想。
我们会发现若是源点向牛连边,那么我们不能够把饮料和食物连在一起,所以我们可以考虑把牛放在中间,旁边连一些饮料和食物。实际上也是这样的:
我们从源点向饮料连容量为1的边,再把每头牛拆成两个点x,x’。对于它喜欢的饮料i,和食物j,我们可以这样连边:(i->x,1)(x->x’,1)(x’->j,1),这样便可以跑最大流了。

var    nu,i,n,m,k,x,x1,x2,ans,p,s,ni,tu,j:longint;    b,last,next,f,re,h,vh:array[0..100000] of longint;procedure insert(x,y,z:longint);begin    inc(nu);b[nu]:=y;next[nu]:=last[x];last[x]:=nu;f[nu]:=z;re[nu]:=nu+1;    inc(nu);b[nu]:=x;next[nu]:=last[y];last[y]:=nu;f[nu]:=0;re[nu]:=nu-1;end;function min(l,r:longint):longint;begin    if l<r then exit(l);exit(r);end;function sap(x,y:longint):longint;var p,t,minh:longint;begin    if x=tu then exit(y);    p:=last[x];minh:=ni+1;    while p<>0 do begin       if f[p]>0 then begin          if h[x]=h[b[p]]+1 then begin             t:=sap(b[p],min(y,f[p]));             if t>0 then begin                f[p]:=f[p]-t;                f[re[p]]:=f[re[p]]+t;                exit(t);             end;             if h[0]>ni then exit(0);          end;          minh:=min(minh,h[b[p]]+1);       end;       p:=next[p];    end;    dec(vh[h[x]]);    if vh[h[x]]=0 then h[0]:=ni+1;    h[x]:=minh;    inc(vh[h[x]]);    exit(0);end;begin    readln(n,m,k);    for i:=1 to n do begin        read(x1,x2);insert(m+i,i+m+n,1);        for j:=1 to x1 do begin            read(x);insert(x,m+i,1);        end;        for j:=1 to x2 do begin            read(x);insert(n+m+i,n+m+n+x,1);        end;    end;    s:=0;tu:=n+m+k+1+n;ni:=n+m+k+2+n;    for i:=1 to m do insert(0,i,1);    for i:=1 to k do insert(i+m+n+n,tu,1);    vh[0]:=ni;    while h[0]<=ni do    ans:=ans+sap(0,maxlongint);    writeln(ans);end.
0 0