POJ3281 Dining (结点容量模型)

题目链接

http://poj.org/problem?id=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: 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.

题意

农夫为他的 N 头(1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 100) 种饮料。每头牛都有各自喜欢的食物和饮料,而每种食物或饮料只能分配给一头牛。最多能有多少头牛可以同时得到喜欢的食物和饮料?

分析

结点容量模型。
先回顾下结点容量的定义和解决。
问题描述:每个结点都有一个允许通过的最大流量,称为结点容量。

把每个结点u分裂成两个结点u1和u2。从u1向u2引一条有向弧,容量为结点u的结点容量。对于原先指向u的有向边,改成指向u1;对于原先从u指出的有向边,改成从u2指出;容量都不变。

显然每头牛都有一个结点容量,并且这个容量为1.
那么图就可以这么建:
源点到食物连一条容量为1的有向弧;
汇点到饮料连一条容量为1的有向弧;
根据输入信息连一条食物到牛的容量为1的有向弧;
牛到牛连一条容量为1的有向弧;
根据输入信息连一条牛的饮料的容量为1的有向弧。

建完后直接调用dinic算法即可。

AC代码

//0ms 0.2MB
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define INF 0x3f3f3f3f
using namespace std;

int N,F,D;
int S,T;//源点和汇点
const int maxe=1e4+1000;
const int maxv=1100;
struct edge
{
    int to,w,next;
}e[maxe<<1];
int head[maxv<<1],depth[maxv],cnt;
void init()
{
    memset(head,-1,sizeof(head));
    cnt=-1;
}
void add_edge(int u,int v,int w)
{
    e[++cnt].to=v;
    e[cnt].w=w;
    e[cnt].next=head[u];
    head[u]=cnt;
}
void _add(int u,int v,int w)
{
    add_edge(u,v,w);
    add_edge(v,u,0);
}

bool bfs()
{
    queue<int> q;
    while(!q.empty()) q.pop();
    memset(depth,0,sizeof(depth));
    depth[S]=1;
    q.push(S);

    while(!q.empty())
    {
        int u=q.front();q.pop();
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            if(!depth[e[i].to] && e[i].w>0)
            {
                depth[e[i].to]=depth[u]+1;
                q.push(e[i].to);
            }
        }
    }
    if(!depth[T]) return false;
    return true;
}
int dfs(int u,int flow)
{
    if(u==T) return flow;
    int ret=0;
    for(int i=head[u];i!=-1 && flow;i=e[i].next)
    {
        if(depth[u]+1==depth[e[i].to] && e[i].w!=0)
        {
            int tmp=dfs(e[i].to,min(e[i].w,flow));
            if(tmp>0)
            {
                flow-=tmp;
                ret+=tmp;
                e[i].w-=tmp;
                e[i^1].w+=tmp;
            }
        }
    }
    return ret;
}
void Dinic()
{
    int ans=0;
    while(bfs())
    {
        ans+=dfs(0,INF);
    }
    printf("%d\n",ans);
}
int main()
{
    init();
    S=0,T=600;
    scanf("%d%d%d",&N,&F,&D);
    for(int i=1;i<=F;i++) _add(S,i,1);//源点到食物
    for(int i=1;i<=D;i++) _add(100+i,T,1);//饮料到汇点
    for(int i=1;i<=N;i++)//i+200和i+300表示牛的编号
    {
        int f,d;
        scanf("%d%d",&f,&d);
         _add(i+200,i+300,1);//牛到牛
        while(f--)//食物
        {
            int u;
            scanf("%d",&u);
            _add(u,i+200,1);//食物到牛
        }
        while(d--)//饮料
        {
            int u;
            scanf("%d",&u);
            _add(i+300,u+100,1);
        }
    }
    Dinic();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值