[kuangbin带你飞]专题十一 网络流||POJ - 3281

题目链接:http://poj.org/problem?id=3281

Dining

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种饮料,每只奶牛只能吃某种食物和饮料,一种食物被一头牛吃了之后,其余牛就不能吃了,要求输出最多分配能够满足的牛的数量.

思路:因为是在专题里做的题,知道是网络流问题,问题就是怎么建图了,实际上食物和水之间没有什么直接联系,而牛吃食物喝水,与两者都有直接联系,所以把牛放在中间,为了保证,牛只能选择一组水和食物,把牛拆点,拆成食物牛和水牛,之间容量为1,然后直接抄个最大流板子...

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>

using namespace std;

#define inf 0x3f3f3f3f

const int maxn=1e9+7;

//****************************************************
//最大流模板
//初始化:g[][],start,end
//******************************************************
const int MAXN=500;
const int INF=0x3fffffff;
int g[MAXN][MAXN];//存边的容量,没有边的初始化为0
int path[MAXN],flow[MAXN],start,stop;
int n;//点的个数,编号0-n.n包括了源点和汇点。

void clear(queue<int>& q)
{
    queue<int> empty;
    swap(empty, q);
}

queue<int>q;
int bfs()
{
    int i,t;
    clear(q);//把队列清空
    //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==stop)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[stop]==-1)return -1;//即找不到汇点上去了。找不到增广路径了
    return flow[stop];
}
int Edmonds_Karp()
{
    int max_flow=0;
    int step,now,pre;
    while((step=bfs())!=-1)
    {
        max_flow+=step;
        now=stop;
        while(now!=start)
        {
            pre=path[now];
            g[pre][now]-=step;
            g[now][pre]+=step;
            now=pre;
        }
    }
    return max_flow;
}

//****************************************************
//EK算法求最大流
//****************************************************

int main()
{
    int N,F,D;
    while(~scanf("%d %d %d",&N,&F,&D))
    {
        memset(g,0,sizeof(g));
        n=F+D+2*N+1;
        start=0;
        stop=n;
        for(int i=1;i<=F;i++) g[0][i]=1;   //牛菜和源点连1
        for(int i=F+2*N+1;i<=F+2*N+D;i++) g[i][n]=1;  //牛水和汇点连1
        for(int i=1;i<=N;i++) g[F+2*i-1][F+2*i]=1;  //牛左和牛右连1
        int fi,ri,ci;
        for(int i=1;i<=N;i++)
        {
            scanf("%d %d",&fi,&ri);
            while(fi--)
            {
                scanf("%d",&ci);
                g[ci][F+2*i-1]=1;
            }
            while(ri--)
            {
                scanf("%d",&ci);
                g[F+2*i][F+2*N+ci]=1;
            }
        }
        printf("%d\n",Edmonds_Karp());
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值