洛谷P2891 Dining P1402 酒店之王【类二分图匹配】题解+代码

时间:2022-06-16 06:29:33

洛谷P2891 Dining P1402 酒店之王【类二分图匹配】题解+代码


酒店之王

题目描述

XX酒店的老板想成为酒店之王,本着这种希望,第一步要将酒店变得人性化。由于很多来住店的旅客有自己喜好的房间色调、阳光等,也有自己所爱的菜,但是该酒店只有p间房间,一天只有固定的q道不同的菜。

有一天来了n个客人,每个客人说出了自己喜欢哪些房间,喜欢哪道菜。但是很不幸,可能做不到让所有顾客满意(满意的条件是住进喜欢的房间,吃到喜欢的菜)。

这里要怎么分配,能使最多顾客满意呢?

输入格式:

第一行给出三个正整数表示n,p,q(<=100)。

之后n行,每行p个数包含0或1,第i个数表示喜不喜欢第i个房间(1表示喜欢,0表示不喜欢)。

之后n行,每行q个数,表示喜不喜欢第i道菜。

输出格式:

最大的顾客满意数。

输入样例

2 2 2

1 0

1 0

1 1

1 1

输出样例

1


题目分析

这个题目一眼就看得出是匹配

但是点集有三个

怎么办呢

以这题为例

我们把n个客人拆成2*n个点

i与i+n连一条连一条容量为1的边

(因为要保证,每个客人只被匹配一次)

每个客人由自己喜欢的菜连一条容量为1的入边

每个客人向喜欢的房间连一条容量为1的出边

建立超级汇点与超级源点

超级源点向每个菜连一条容量为1的边

每个房间向超级汇点连一条容量为1的边

然后跑最大流即可


#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;

const int inf=1e9;
int n,p,q;
int s=0,t;
int tot=1;
struct node{int v,f,nxt;}E[1000010];
int head[1000010];
int lev[1000010];
int maxf;

void add(int u,int v,int cap)
{
    E[++tot].nxt=head[u];
    E[tot].v=v;
    E[tot].f=cap;
    head[u]=tot;
}

bool bfs()
{
    memset(lev,-1,sizeof(lev));
    queue<int> q;
    q.push(s);
    lev[s]=0;

    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u];i;i=E[i].nxt)
        {
            int v=E[i].v;
            if(lev[v]==-1&&E[i].f)
            {
                lev[v]=lev[u]+1;
                if(v==t) return true;
                q.push(v);
            }
        }
    }
    return false;
}

int dfs(int u,int cap)
{
    if(u==t)
    return cap;

    int flow=cap;
    for(int i=head[u];i;i=E[i].nxt)
    {
        int v=E[i].v;
        if(lev[v]==lev[u]+1&&flow&&E[i].f>0)
        {
            int f=dfs(v,min(flow,E[i].f));
            flow-=f;
            E[i].f-=f;
            E[i^1].f+=f;
        }
    }
    return cap-flow;
}

int main()
{
    cin>>n>>p>>q;
    t=n*2+q+p+1;//超级汇点

    for(int i=1;i<=n;i++)
    {
        add(i,i+n,1);
        add(i+n,i,0);
        //客人拆点
    }

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=p;j++)
        {
            int temp;
            cin>>temp;
            if(temp==1)
            {
                add(i+n,2*n+j,1);
                add(2*n+j,i+n,0);
                //客人向自己喜欢的房间连边
                //注意由于拆点,这里客人编号为i+n
            }
        }
    }

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=q;j++)
        {
            int temp;
            cin>>temp;
            if(temp==1)
            {
                add(2*n+p+j,i,1);
                add(i,2*n+p+j,0);
                //每个喜欢的菜向客人连边
                //这里客人编号为i
            }
        }
    }

    for(int i=2*n+p+1;i<=2*n+p+q;i++)
    {
        add(s,i,1);
        add(i,s,0);//超源向菜连边
    }

    for(int i=2*n+1;i<=2*n+p;i++)
    {
        add(i,t,1);
        add(t,i,0);//房间向超汇连边
    }

    while(bfs())//最大流
    maxf+=dfs(s,inf);

    cout<<maxf;
    return 0;
}

[USACO07OPEN] 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).

有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料。现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和饮料。(1 <= f <= 100, 1 <= d <= 100, 1 <= n <= 100)

输入格式:

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.

输出格式:

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

输入样例

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

输出样例

3


题目分析

其实和上面那题是一样的。。。。

改改输入,双倍经验


#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;

const int inf=1e9;
int n,f,d;
int s=0,t;
int tot=1;
struct node{int v,f,nxt;}E[1000010];
int head[1000010];
int lev[1000010];
int maxf;

void add(int u,int v,int cap)
{
    E[++tot].nxt=head[u];
    E[tot].v=v;
    E[tot].f=cap;
    head[u]=tot;
}

bool bfs()
{
    memset(lev,-1,sizeof(lev));
    queue<int> q;
    q.push(s);
    lev[s]=0;

    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u];i;i=E[i].nxt)
        {
            int v=E[i].v;
            if(lev[v]==-1&&E[i].f)
            {
                lev[v]=lev[u]+1;
                if(v==t) return true;
                q.push(v);
            }
        }
    }
    return false;
}

int dfs(int u,int cap)
{
    if(u==t)
    return cap;

    int flow=cap;
    for(int i=head[u];i;i=E[i].nxt)
    {
        int v=E[i].v;
        if(lev[v]==lev[u]+1&&flow&&E[i].f>0)
        {
            int f=dfs(v,min(flow,E[i].f));
            flow-=f;
            E[i].f-=f;
            E[i^1].f+=f;
        }
    }
    return cap-flow;
}

int main()
{
    cin>>n>>f>>d;
    t=n*2+f+d+1;
    for(int i=1;i<=n;i++)
    {
        add(i,i+n,1);
        add(i+n,i,0);
    }
    for(int i=1;i<=n;i++)
    {
        int food,drink;
        cin>>food>>drink;
        while(food--)
        {
            int fnum;
            cin>>fnum;
            add(i+n,2*n+fnum,1);
            add(2*n+fnum,i+n,0);
        }

        while(drink--)
        {
            int dnum;
            cin>>dnum;
            add(2*n+f+dnum,i,1);
            add(i,2*n+f+dnum,0);
        }
    }

    for(int i=2*n+f+1;i<=n*2+f+d;i++)
    {
        add(s,i,1);
        add(i,s,0);
    }

    for(int i=2*n+1;i<=2*n+f;i++)
    {
        add(i,t,1);
        add(t,i,0);
    }

    while(bfs())
    maxf+=dfs(s,inf);

    cout<<maxf;
    return 0;
}