POJ 3281 Dining (拆点+ 网络流)

Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 19307   Accepted: 8607

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: N, F, 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 the Di 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 3
2 2 1 2 3 1
2 2 2 3 1 2
2 2 1 3 1 2
2 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.

Source


123


题意:

给n个牛  f 食物, d 饮料,  每个牛 喜欢x中食物和y种饮料,

现在想求最大 的牛 喝饮料喝食物;     

类似于二分匹配:

建立图:



#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define FIN      freopen("input.txt","r",stdin)
#define FOUT     freopen("output.txt","w",stdout)
#define S1(n)    scanf("%d",&n)
#define SL1(n)   scanf("%I64d",&n)
#define S2(n,m)  scanf("%d%d",&n,&m)
#define SL2(n,m)  scanf("%I64d%I64d",&n,&m)
#define Pr(n)     printf("%d\n",n)

using namespace std;
typedef long long ll;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int MOD=1e9+7;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};

ll inv[maxn*2],fac[maxn];
ll gcd(ll a,ll b){ return b?gcd(b,a%b):a;}
ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll ans=exgcd(b,a%b,x,y);ll temp=x;x=y;y=temp-a/b*y;return ans;}
ll lcm(ll a,ll b){ return b/gcd(a,b)*a;}
ll qpow(ll x,ll n){ll res=1;for(;n;n>>=1){if(n&1)res=(res*x)%MOD;x=(x*x)%MOD;}return res;}
void INV(){inv[1] = 1;for(int i = 2; i < maxn; i++) inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){ x=1; y=0; d=a; }else{ ex_gcd(b,a%b,d,y,x); y-=x*(a/b);}}
void Fac(){fac[0]=1;for(int i=1;i<=maxn;i++)fac[i]=(fac[i-1]*i)%MOD;}
ll inv_exgcd(ll a,ll n){ll d,x,y;ex_gcd(a,n,d,x,y);return d==1?(x+n)%n:-1;}
ll inv1(ll b){return b==1?1:(MOD-MOD/b)*inv1(MOD%b)%MOD;}
ll inv2(ll b){return qpow(b,MOD-2);}
ll cal(ll m,ll n){if(m<n)return 0;return (fac[m]*inv[fac[n]]%MOD)%MOD*inv[fac[m-n]]%MOD;}
ll cals(ll m,ll n){if(m<n)return 0;return (fac[m]*inv1(fac[n])%MOD)%MOD*inv1(fac[m-n])%MOD;}

int n,f,d;

struct node{
    int v,w,next; //u  v 从 u-v 权值为w
}edge[maxn];

int head[maxn],num[maxn],start,END,cnt,sum;
void add(int u,int v,int w)
{
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].v=u;
    edge[cnt].w=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
int bfs()
{
    queue<int>Q;
    mem(num,0);
    num[start]=1;
    Q.push(start);
    while(!Q.empty())
    {
        int t=Q.front();
        Q.pop();
        if(t==END)
            return 1;
        for(int i=head[t];i!=-1;i=edge[i].next)// 链式前向星访问 找增广路
        {
            int t1= edge[i].v;// 下一个节点
            int t2= edge[i].w;// 当前点 权值
            if(t2&&num[t1]==0)// 当前点存在 并且下一个点没有访问
            {
                num[t1]=num[t]+1;// 点=1
                if(t1==END)// 结束
                    return 1;
                Q.push(t1);
            }
        }
    }
    return 0;
}
int dfs(int u,int maxflow)
{
    if(u==END)
        return maxflow;
    int res=0;

    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int t1=edge[i].v;// 下一个节点
        int t2=edge[i].w;// 当前权值
        if(t2&&num[t1]==num[u]+1)
        {
            int temp=dfs(t1,min(maxflow-res,t2));// 选择 流小的

            edge[i].w-=temp;// 正向减少
            edge[i^1].w+=temp;//反向增加
            res+=temp;
            if(res==maxflow)
                return res;
        }
    }
    if(!res)
        num[u]=-1;
    return res;
}
void Dinic()
{
    int ans=0;
    while(bfs())
    {
        ans+=dfs(start,INF);
    }
    cout<<ans<<endl;
}
int main()
{
    int x,y,u;
    while(~scanf("%d %d %d",&n,&f,&d))
    {
        mem(head,-1);
        cnt=0;
        start=0,END=1000;
        for(int i=1;i<=f;i++)// 建立源点
            add(start,i+300,1);
        for(int i=1;i<=d;i++)// 建立汇点
            add(i+500,END,1);
        for(int i=1;i<=n;i++)
        {
            add(i,i+100,1);// 拆 点
            scanf("%d %d",&x,&y);
            for(int j=1;j<=x;j++)
            {
                scanf("%d",&u);
                add(u+300,i,1); // food - 牛
            }
            for(int j=1;j<=y;j++)
            {
                scanf("%d",&u);
                add(i+100,u+500,1);// 牛 --drink
            }
        }

        Dinic();
    }
    return 0;
}


posted @ 2017-09-07 23:53  Sizaif  阅读(126)  评论(0编辑  收藏  举报