1. 程式人生 > >poj 3281Dining最大流匹配+建圖

poj 3281Dining最大流匹配+建圖

Dining

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 24006   Accepted: 10618

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: NF, 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

USACO 2007 Open Gold

 有F種食物和D種飲料,每種食物或飲料只能供一頭牛享用,且每頭牛隻享用一種食物和一種飲料。現在有N頭牛,每頭牛都有自己喜歡的食物種類列表和飲料種類列表,問最多能使幾頭牛同時享用到自己喜歡的食物和飲料。 (1 <= F <= 100, 1 <= D <= 100, 1 <= N <= 100)
 

                  食物                       牛                     牛            飲料

建立上圖,然後最大流即可,所有邊容量均為1

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define maxn 550
#define inf 1e9
struct edge
{int from,to,cap,flow;
edge(){}
edge(int f,int t,int c,int fl):from(f),to(t),cap(c),flow(fl){}
};
struct din
{
    int n,m,s,t;
    vector<edge>edges;
    vector<int>g[maxn];
    int d[maxn];
    bool vis[maxn];
    int cur[maxn];
    void init(int n,int s,int t)
    {
        this->n=n;
        this->s=s;
        this->t=t;
        edges.clear();
        for(int i=0;i<n;i++)
            g[i].clear();
    }
    void addedge(int from,int to,int cap)
    {
        edges.push_back(edge(from,to,cap,0));
        edges.push_back(edge(to,from,0,0));
        m=edges.size();
        g[from].push_back(m-2);
        g[to].push_back(m-1);
    }
    bool bfs()
    {
        queue<int>q;
        memset(vis,0,sizeof(vis));
        vis[s]=true;
        d[s]=0;
        q.push(s);
        while(!q.empty())
        {
            int x=q.front();
            q.pop();
            for(int i=0;i<g[x].size();i++)
            {
                edge&e=edges[g[x][i]];
                if(!vis[e.to]&&e.cap>e.flow)
                {
                    vis[e.to]=true;
                    d[e.to]=d[x]+1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }
    int dfs(int x,int a)
    {if(x==t||a==0)
    return a;
    int flow=0,f;
    for(int &i=cur[x];i<g[x].size();i++)
    {
        edge&e=edges[g[x][i]];
        if(d[e.to]==d[x]+1&&(f=dfs(e.to,min(a,e.cap-e.flow))>0))
        {
            e.flow+=f;
            edges[g[x][i]^1].flow-=f;
            flow+=f;
            a-=f;
            if(a==0)
                break;
        }

    }
    return flow;

    }
    int maxflow()
    {
        int ans=0;
        while(bfs())
        {
            memset(cur,0,sizeof(cur));
            ans+=dfs(s,inf);

        }
        return ans;
    }
}Dc;
int main()
{int n,f,d;
while(~scanf("%d%d%d",&n,&f,&d))
{int src=0,dst=2*n+f+d+1;
    Dc.init(2+2*n+f+d,src,dst);
    for(int i=1;i<=f;i++)
        Dc.addedge(src,i,1);
    for(int i=1;i<=d;i++)
        Dc.addedge(i+2*n+f,dst,1);
    int fn,dn;
    for(int i=f+1;i<=f+n;i++)
    {
        Dc.addedge(i,i+n,1);
    scanf("%d%d",&fn,&dn);
    while(fn--)
    {int ff;
    scanf("%d",&ff);
        Dc.addedge(ff,i,1);
    }
    while(dn--)
    {
        int dd;
        scanf("%d",&dd);
        Dc.addedge(i+n,dd+2*n+f,1);
    }
}
printf("%d\n",Dc.maxflow());

}
return 0;
}