酒店之王 最大流

时间:2021-05-03 05:30:41

题目描述

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

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

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

输入输出格式

输入格式:

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

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

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

输出格式:

最大的顾客满意数。

输入输出样例

输入样例#1: 复制
2 2 2
1 0
1 0
1 1
1 1
输出样例#1: 复制
1
最大流;
建边,将人拆成两个点 p1,p2;
首先由源点向每道菜连容量为1的边,
每个房间向汇点连容量为1的边;
然后每个人的 p1 与他喜欢的菜连容量为1的边,p1与p2连容量为1的边,p2与他喜欢的房间连容量为1的边;
然后 dinic 跑一遍最大流即可;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 200005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
    ll x = 0;
    char c = getchar();
    bool f = false;
    while (!isdigit(c)) {
        if (c == '-') f = true;
        c = getchar();
    }
    while (isdigit(c)) {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    return f ? -x : x;
}

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
    if (!b) {
        x = 1; y = 0; return a;
    }
    ans = exgcd(b, a%b, x, y);
    ll t = x; x = y; y = t - a / b * y;
    return ans;
}
*/



ll qpow(ll a, ll b, ll c) {
    ll ans = 1;
    a = a % c;
    while (b) {
        if (b % 2)ans = ans * a%c;
        b /= 2; a = a * a%c;
    }
    return ans;
}

int n, p, q;
bool vis[maxn];
int x, y, f, z;


struct node {
    int to, nxt, val, u;
}edge[maxn << 2];

int head[maxn], cnt;


void addedge(int u, int v, int w) {
    edge[cnt].u = u; edge[cnt].to = v; edge[cnt].val = w;
    edge[cnt].nxt = head[u]; head[u] = cnt++;
}

int st, ed, rk[maxn];

int bfs() {
    queue<int>qq;
    ms(rk);
    rk[st] = 1;
    qq.push(st);
    while (!qq.empty()) {
        int  tmp = qq.front(); qq.pop();
        for (int i = head[tmp]; i != -1; i = edge[i].nxt) {
            int to = edge[i].to;
            if (rk[to] || edge[i].val <= 0)continue;
            rk[to] = rk[tmp] + 1; qq.push(to);
        }
    }
    return rk[ed];
}

int dfs(int u, int flow) {
    if (u == ed)return flow;
    int add = 0;
    for (int i = head[u]; i != -1 && add < flow; i = edge[i].nxt) {
        int v = edge[i].to;
        if (rk[v] != rk[u] + 1 || !edge[i].val)continue;
        int tmpadd = dfs(v, min(edge[i].val, flow - add));
        if (!tmpadd) {
            rk[v] = -1; continue;
        }
        edge[i].val -= tmpadd; edge[i ^ 1].val += tmpadd;
        add += tmpadd;
    }
    return add;
}

int ans;

void dinic() {
    while (bfs())ans += dfs(st, inf);
}


int main()
{
    //ios::sync_with_stdio(0);
    memset(head, -1, sizeof(head)); cnt = 0;
    rdint(n); rdint(p); rdint(q);
    st = p + q + 2 * n + 1; ed = st + 1;
    for (int i = 1; i <= p; i++)addedge(st, i, 1), addedge(i, st, 0);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= p; j++) {
            int x; rdint(x);
            if (x == 1) {
                addedge(p + i, j, 0);
                addedge(j, p + i, 1);
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= q; j++) {
            int x; rdint(x);
            if (x == 1) {
                addedge(p + q + i + n, p + j + n, 1);
                addedge(p + j + n, p + q + i + n, 0);
            }
        }
    }
    for (int i = p + 1 + n; i <= p + n + q; i++) {
        addedge(i, ed, 1); addedge(ed, i, 0);
    }
    for (int i = 1; i <= n; i++)
        addedge(p + i, p + q + i + n, 1), addedge(p + q + i + n, p + i, 0);

    dinic();
    cout << ans << endl;
    return 0;
}