ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

餐饮(最大流,拆点,三分图)

2021-02-15 16:34:59  阅读:188  来源: 互联网

标签:ver idx int 拆点 flow que 奶牛 餐饮 三分


题意

有\(n\)头奶牛,每头奶牛都有喜欢的食品和饮料。每头牛只能吃一种食品、喝一种饮料,每种食品、饮料都只能使用\(1\)次。

问最多能让多少头牛得到自己喜欢的食品、饮料。

思路

因为是奶牛匹配食品、奶牛匹配饮料,因此将奶牛放在中间。

设置源点\(S\),向每款食品连容量是\(1\)的边,原因是只能使用\(1\)次;设置汇点\(T\),每款饮料向\(T\)连容量是\(1\)的边,原因是只能使用\(1\)次。

因为奶牛也只能使用\(1\)次,为了满足这个条件,可以采用拆点的方法,即将一个点拆成一个入点和一个出点,入点向出点连容量是\(1\)的边。

对于每头奶牛,它喜欢的食品向它的入点连容量是\(1\)的边(其实任意正整数都可);它的出点向它喜欢的饮料连容量是\(1\)的边。

跑最大流即可。

代码

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

using namespace std;

const int N = 410, M = (300 + 20010) * 2, inf = 1e8;

int n, F, D, S, T;
int h[N], e[M], f[M], ne[M], idx;
int cur[N], d[N];

void add(int a, int b, int c)
{
    e[idx] = b, f[idx] = c, ne[idx] = h[a], h[a] = idx ++;
    e[idx] = a, f[idx] = 0, ne[idx] = h[b], h[b] = idx ++;
}

bool bfs()
{
    memset(d, -1, sizeof(d));
    queue<int> que;
    que.push(S);
    d[S] = 0, cur[S] = h[S];
    while(que.size()) {
        int t = que.front();
        que.pop();
        for(int i = h[t]; ~i; i = ne[i]) {
            int ver = e[i];
            if(d[ver] == -1 && f[i]) {
                d[ver] = d[t] + 1;
                cur[ver] = h[ver];
                if(ver == T) return true;
                que.push(ver);
            }
        }
    }
    return false;
}

int find(int u, int limit)
{
    int flow = 0;
    if(u == T) return limit;
    for(int i = cur[u]; ~i && flow < limit; i = ne[i]) {
        cur[u] = i;
        int ver = e[i];
        if(d[ver] == d[u] + 1 && f[i]) {
            int t = find(ver, min(f[i], limit - flow));
            if(!t) d[ver] = -1;
            f[i] -= t, f[i ^ 1] += t, flow += t;
        }
    }
    return flow;
}

int dinic()
{
    int res = 0, flow;
    while(bfs()) {
        while(flow = find(S, inf)) {
            res += flow;
        }
    }
    return res;
}

int main()
{
    scanf("%d%d%d", &n, &F, &D);
    memset(h, -1, sizeof(h));
    S = 0, T = 2 * n + F + D + 1;
    for(int i = 1; i <= F; i ++) add(S, i, 1);
    for(int i = 2 * n + F + 1; i <= 2 * n + F + D; i ++) add(i, T, 1);
    for(int i = 1; i <= n; i ++) {
        int n1, n2;
        scanf("%d%d", &n1, &n2);
        for(int j = 1; j <= n1; j ++) {
            int x;
            scanf("%d", &x);
            add(x, F + i, 1);
        }
        for(int j = 1; j <= n2; j ++) {
            int x;
            scanf("%d", &x);
            add(F + n + i, F + 2 * n + x, 1);
        }
    }
    for(int i = 1; i <= n; i ++) add(F + i, F + n + i, 1);
    printf("%d\n", dinic());
    return 0;
}

标签:ver,idx,int,拆点,flow,que,奶牛,餐饮,三分
来源: https://www.cnblogs.com/miraclepbc/p/14403899.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有