BZOJ 1711. [Usaco2007 Open] Dining dinner

Portal

It seems to be quite clear that the network flow ...

Food source to each $ 1 $ $ $ even flow side, $ drink $ flow is connected to the sink side of $ 1 $

Each cow split into two points $ I, the flow rate is between n + i $ $ $ 1, $ corresponding to even Food $ $ i $, $ n + i $ Drink connected to the corresponding $ $

Then the maximum flow

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
inline int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
    return x*f;
}
const int N=5e5+7,INF=1e9+7;
int n,F,D,ans;
int fir[N],from[N<<1],to[N<<1],val[N<<1],cntt=1;
inline void add(inta, int b, int c) 
{ 
    from [++ cntt] = fir [a]; fir [a] = IT; 
    to [IT] = b; val [IT] = c;
    from [++ cntt] = fir [b]; fir [b] = IT; 
    to [IT] = a; val [IT] = 0 ; 
} 
Int dep [N], S, T; 
queue < int > Q;
bool BFS () 
{ 
    for ( int i = 0 ; i <= T; i ++) dep [i] = 0 ; 
    dep [S] = 1 ; Q.push (S);
    while (! Q.empty())
    {
        int x=Q.front(); Q.pop();
        for(int i=fir[x];i;i=from[i])
        {
            int &v=to[i]; if(dep[v]||!val[i]) continue;
            dep[v]=dep[x]+1; Q.push(v);
        }
    }
    return dep[T]>0;
}
int DFS(int x,int mxfl)
{
    if(x==T||!mxfl) return mxfl;
    int fl=0,res;
    for(int i=fir[x];i;i=from[i])
    {
        int &v=to[i]; if(dep[v]!=dep[x]+1||!val[i]) continue;
        if( res=DFS(v,min(val[i],mxfl)) )
        {
            val[i]-=res; mxfl-=res;
            fl+=res; val[i^1]+=res;
            if(!mxfl) break;
        }
    }
    return fl;
}
int c[233],d[233];
int main()
{
    n=read(),F=read(),D=read(); S=0,T=n*2+F+D+1;
    int a,b;
    for(int i=1;i<=n;i++) add(i,n+i,1);
    for(int i=1;i<=F;i++) add(S,n*2+i,1);
    for(int i=1;i<=D;i++) add(n*2+F+i,T,1);
    for(int k=1;k<=n;k++)
    {
        a=read(),b=read();
        for(int i=1;i<=a;i++) c[i]=read(),add(n*2+c[i],k,1);
        for(int i=1;i<=b;i++) d[i]=read(),add(n+k,n*2+F+d[i],1);
    }
    while(BFS()) ans+=DFS(S,INF);
    printf("%d\n",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/LLTYYC/p/11417412.html