[POJ-3281]
思路:
把牛拆点;
s向食物连边,流量1;
饮料向t连边,流量1;
食物向牛1连边,流量1;
牛2向饮料连边,流量1;
最大流;
来,上代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 2005
#define INF 0x7fffffff int n,f,d,s,t,head[maxn],E[maxn<<],V[maxn<<],F[maxn<<];
int cnt=,deep[maxn],que[maxn<<]; inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
} inline void edge_add(int u,int v,int f)
{
E[++cnt]=head[u],V[cnt]=v,F[cnt]=f,head[u]=cnt;
E[++cnt]=head[v],V[cnt]=u,F[cnt]=,head[v]=cnt;
} inline bool bfs()
{
for(int i=s;i<=t;i++) deep[i]=-;
int h=,tail=;deep[s]=,que[h]=s;
while(h<tail)
{
int now=que[h++];
for(int i=head[now];i;i=E[i])
{
if(deep[V[i]]<&&F[i]>)
{
deep[V[i]]=deep[now]+;
if(V[i]==t) return true;
que[tail++]=V[i];
}
}
}
return false;
} int flowing(int now,int flow)
{
if(flow<=||now==t) return flow;
int oldflow=;
for(int i=head[now];i;i=E[i])
{
if(F[i]>&&deep[V[i]]==deep[now]+)
{
int pos=flowing(V[i],min(flow,F[i]));
flow-=pos,oldflow+=pos,F[i]-=pos,F[i^]+=pos;
if(flow==) return oldflow;
}
}
if(oldflow==) deep[now]=-;
return oldflow;
} int main()
{
in(n),in(f),in(d);
int u,v,nf,nd;t=f+n+n+d+;
for(int i=;i<=f;i++) edge_add(s,i,);
for(int i=;i<=d;i++) edge_add(f+n+n+i,t,);
for(int i=;i<=n;i++)
{
edge_add(f+i,f+n+i,),in(nf),in(nd);
for(int j=;j<=nf;j++) in(u),edge_add(u,f+i,);
for(int j=;j<=nd;j++) in(u),edge_add(f+n+i,f+n+n+u,);
}
int ans=;
while(bfs()) ans+=flowing(s,INF);
cout<<ans;
return ;
}