首页 > 代码库 > AC日记——Dining poj 3281

AC日记——Dining 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 0x7fffffffint n,f,d,s,t,head[maxn],E[maxn<<2],V[maxn<<2],F[maxn<<2];int cnt=1,deep[maxn],que[maxn<<4];inline void in(int &now){    char Cget=getchar();now=0;    while(Cget>9||Cget<0) Cget=getchar();    while(Cget>=0&&Cget<=9)    {        now=now*10+Cget-0;        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]=0,head[v]=cnt;}inline bool bfs(){    for(int i=s;i<=t;i++) deep[i]=-1;    int h=0,tail=1;deep[s]=0,que[h]=s;    while(h<tail)    {        int now=que[h++];        for(int i=head[now];i;i=E[i])        {            if(deep[V[i]]<0&&F[i]>0)            {                deep[V[i]]=deep[now]+1;                if(V[i]==t) return true;                que[tail++]=V[i];            }        }    }    return false;}int flowing(int now,int flow){    if(flow<=0||now==t) return flow;    int oldflow=0;    for(int i=head[now];i;i=E[i])    {        if(F[i]>0&&deep[V[i]]==deep[now]+1)        {            int pos=flowing(V[i],min(flow,F[i]));            flow-=pos,oldflow+=pos,F[i]-=pos,F[i^1]+=pos;            if(flow==0) return oldflow;        }    }    if(oldflow==0) deep[now]=-1;    return oldflow;}int main(){    in(n),in(f),in(d);    int u,v,nf,nd;t=f+n+n+d+1;    for(int i=1;i<=f;i++) edge_add(s,i,1);    for(int i=1;i<=d;i++) edge_add(f+n+n+i,t,1);    for(int i=1;i<=n;i++)    {        edge_add(f+i,f+n+i,1),in(nf),in(nd);        for(int j=1;j<=nf;j++) in(u),edge_add(u,f+i,1);        for(int j=1;j<=nd;j++) in(u),edge_add(f+n+i,f+n+n+u,1);    }    int ans=0;    while(bfs()) ans+=flowing(s,INF);    cout<<ans;    return 0;}

 

AC日记——Dining poj 3281