POJ 3281 Dining(最大流)


Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6586   Accepted: 3015

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

 
本題能夠想到用最大流做,那真的是太絕了。建模的方法很妙!
題意就是有N頭牛,F個食物,D個飲料。
N頭牛每頭牛有一定的喜好,只喜歡幾個食物和飲料。
每個食物和飲料只能給一頭牛。一頭牛只能得到一個食物和飲料。
而且一頭牛必須同時獲得一個食物和一個飲料才能滿足。問至多有多少頭牛可以獲得滿足。
最初相當的是二分匹配。但是明顯不行,因為要分配兩個東西,兩個東西還要同時滿足。
最大流建圖是把食物和飲料放在兩端。一頭牛拆分成兩個點,兩點之間的容量為1.喜歡的食物和飲料跟牛建條邊,容量為1.
加個源點和匯點。源點與食物、飲料和匯點的邊容量都是1,表示每種食物和飲料只有一個。
這樣話完全是最大流問題了,。
/*
POJ 3281 最大流
//源點-->food-->牛(左)-->牛(右)-->drink-->匯點
//精髓就在這里,牛拆點,確保一頭牛就選一套food和drink的搭配

*/

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;

//****************************************************
//最大流模板
//初始化:g[][],start,end
//******************************************************
const int MAXN=500;
const int INF=0x3fffffff;
int g[MAXN][MAXN];//存邊的容量,沒有邊的初始化為0
int path[MAXN],flow[MAXN],start,end;
int n;//點的個數,編號0-n.n包括了源點和匯點。

queue<int>q;
int bfs()
{
    int i,t;
    while(!q.empty())q.pop();//把清空隊列
    memset(path,-1,sizeof(path));//每次搜索前都把路徑初始化成-1
    path[start]=0;
    flow[start]=INF;//源點可以有無窮的流流進
    q.push(start);
    while(!q.empty())
    {
        t=q.front();
        q.pop();
        if(t==end)break;
        //枚舉所有的點,如果點的編號起始點有變化可以改這里
        for(i=0;i<=n;i++)
        {
            if(i!=start&&path[i]==-1&&g[t][i])
            {
                flow[i]=flow[t]<g[t][i]?flow[t]:g[t][i];
                q.push(i);
                path[i]=t;
            }
        }
    }
    if(path[end]==-1)return -1;//即找不到匯點上去了。找不到增廣路徑了
    return flow[end];
}
int Edmonds_Karp()
{
    int max_flow=0;
    int step,now,pre;
    while((step=bfs())!=-1)
    {
        max_flow+=step;
        now=end;
        while(now!=start)
        {
            pre=path[now];
            g[pre][now]-=step;
            g[now][pre]+=step;
            now=pre;
        }
    }
    return max_flow;
}
int main()
{
    int N,F,D;
    while(scanf("%d%d%d",&N,&F,&D)!=EOF)
    {
        memset(g,0,sizeof(g));
        n=F+D+2*N+1;
        start=0;
        end=n;
        for(int i=1;i<=F;i++)g[0][i]=1;
        for(int i=F+2*N+1;i<=F+2*N+D;i++)g[i][n]=1;
        for(int i=1;i<=N;i++)g[F+2*i-1][F+2*i]=1;
        int k1,k2;
        int u;
        for(int i=1;i<=N;i++)
        {
            scanf("%d%d",&k1,&k2);
            while(k1--)
            {
                scanf("%d",&u);
                g[u][F+2*i-1]=1;
            }
            while(k2--)
            {
                scanf("%d",&u);
                g[F+2*i][F+2*N+u]=1;
            }
        }
        printf("%d\n",Edmonds_Karp());
    }
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM