POJ---3281 Dining【拆点最大流】

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

SOURCE:点击打开链接


题意:

     有n个牛,f种食物,d种饮料(食物和饮料只能选用一次)。每个牛都有自己喜爱的食物和饮料,问最大能使多少头牛吃到自己喜爱的食物和饮料。


解析:

       建立超级源点和超级汇点,把每头牛拆分成左,右牛(否则会贪吃)。让源点指向食物,食物指向左牛,左牛指向右牛,右牛再指向饮料,最后让饮料指向汇点,所有边权为1,求最大流。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define MAXN 405//食物从2*n+1开始 到 2*n+f 饮料从2*n+f+1 开始 到2*n+1+f+d
#define MAXM 200000+100//最多有100*100*100*2=2000000条边
#define INF 0x3f3f3f3f
using namespace std;

struct Edge
{
    int from, to, cap, flow, next;
} edge[MAXM];

int dist[MAXN], head[MAXN],vis[MAXN];
int n, f, d,num,end;

void addedge(const int u,const int v,const int w);
bool BFS(const int start);
int DFS(const int x,const int low);

int main(void)
{
    int fnum, dnum,flow,x;
    while(scanf("%d%d%d", &n, &f, &d) != EOF)
    {
        num = 0,flow=0;
        end=2*n+f+d+1;
        memset(head, -1, sizeof(head));
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d", &fnum, &dnum);
            while(fnum--)//食物 编号2*n+j
            {
                scanf("%d", &x);
                addedge(2*n+x, i, 1);//食物和对应奶牛连通
            }
            while(dnum--)//饮料 编号2*n+f+j
            {
                scanf("%d", &x);
                addedge(n+i, 2*n+f+x, 1);//奶牛和对应饮料连通
            }
            addedge(i, n+i, 1);//奶牛自己和自己连一次
        }
        for(int i = 1; i <= f; i++)
            addedge(0, 2*n+i, 1);//超级源点连通所有食物
        for(int i = 1; i <= d; i++)
            addedge(2*n+f+i, 2*n+f+d+1, 1);//所有饮料连通超级汇点
        while(BFS(0))
            flow += DFS(0, INF);
        printf("%d\n",flow);
    }
    return 0;
}

void addedge(const int u,const int v,const int w)
{
    Edge E1 = {u, v, w, 0, head[u]};
    edge[num] = E1;
    head[u] = num++;
    Edge E2 = {v, u, 0, 0, head[v]};
    edge[num] = E2;
    head[v] = num++;
}

bool BFS(const int start)
{
    queue<int> Q;
    int u;
    memset(dist, -1, sizeof(dist));
    memset(vis, 0, sizeof(vis));
    Q.push(start);
    dist[start] = 0;
    vis[start] = 1;
    while(Q.size())
    {
        u = Q.front();
        Q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            Edge E = edge[i];
            if(!vis[E.to] && E.cap > E.flow)
            {
                vis[E.to] = 1;
                dist[E.to] = dist[u] + 1;
                if(E.to == end)
                    return true;
                Q.push(E.to);
            }
        }
    }
    return false;
}
int DFS(const int x,const int low)
{
    if(x == end )
        return low;
    int f;
    for(int i = head[x]; i != -1; i = edge[i].next)
    {
        Edge &E = edge[i];
        if(dist[E.to] == dist[x]+1)
        {
            f = DFS(E.to, min(low, E.cap-E.flow));
            if(f>0)
            {
                E.flow += f;
                edge[i^1].flow -= f;
                return f;
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值