【Dinic最大流】POJ - 3281 B - Dining

B - Dining  POJ - 3281

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 Fiintegers 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.

有n头牛,f种食物,d种饮料
每种食物和饮料都只有一个
告诉你每头奶牛喜欢的食物和饮料
要求输出最多有几头奶牛能够得到一种喜欢的食物和一种喜欢的饮料
最大流套板子就行,用的Dinic
建边:st->食物左->食物右->奶牛左->奶牛右->饮料左->饮料右->en
特别注意:食物,奶牛,饮料自身建边时容量为1!


#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAX_V=2005;
const int st=0;
const int en=2000;
const int INF=0x3f3f3f3f;

struct edge
{
    int to,cap,rev; //用于表示边的结构体(终点,容量,反向边)
};

vector <edge> G[MAX_V]; //图的邻接表表示
int level[MAX_V];   //顶点到源点的距离标号
int iter[MAX_V];    //当前弧,在其之前的边已经没有用了
char t[222];

void add_edge(int from,int to,int cap)
{
    G[from].push_back((edge){to,cap,G[to].size()});
    G[to].push_back((edge){from,0,G[from].size()-1});
}

void bfs(int s)  //通过bfs计算从源点出发的距离标号
{
    memset(level,-1,sizeof(level));
    queue <int> q;
    level[s]=0;
    q.push(s);
    while(!q.empty())
    {
        int v=q.front();q.pop();
        for(int i=0;i<G[v].size();i++)
        {
            edge &e=G[v][i];
            if(e.cap>0&&level[e.to]<0)
            {
                level[e.to]=level[v]+1;
                q.push(e.to);
            }
        }
    }
}

int dfs(int v,int t,int f)  //通过dfs寻找增广路
{
    if(v==t) return f;
    for(int &i=iter[v];i<G[v].size();i++)
    {
        edge &e=G[v][i];
        if(e.cap>0&&level[v]<level[e.to])
        {
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0)
            {
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s,int t)
{
    int flow=0;
    for(;;)
    {
        bfs(s); //计算层次图
        if(level[t]<0) return flow; //找不到s-t路径
        memset(iter,0,sizeof(iter)); //初始化当前弧
        int f;
        while((f=dfs(s,t,INF))>0)  //更新最大流
            flow+=f;
    }
    return flow;
}

int main()
{
    int n,f,d;
    while(scanf("%d%d%d",&n,&f,&d)!=EOF)
    {
        for(int i=0;i<MAX_V;i++) G[i].clear();
        for(int i=1;i<=f;i++)
        {
            add_edge(st,i,1);
            add_edge(i,i+200,1);
        }
        for(int i=1;i<=d;i++)
        {
            add_edge(i+800,i+1000,1);
            add_edge(i+1000,en,1);
        }
        for(int i=1;i<=n;i++)
        {
            add_edge(i+400,i+600,1);
        }
        for(int i=1;i<=n;i++)
        {
            int p1,p2;
            scanf("%d%d",&p1,&p2);
            for(int j=1;j<=p1;j++)
            {
                int x;scanf("%d",&x);
                add_edge(x+200,i+400,1);
            }
            for(int j=1;j<=p2;j++)
            {
                int x;scanf("%d",&x);
                add_edge(i+600,x+800,1);
            }
        }
        printf("%d\n",max_flow(st,en));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41037114/article/details/81490946
今日推荐