"Luogu1402"호텔 킹

포털
Luogu

문제 해결 아이디어

보드 네트워크 흐름 문제.
내장 된지도 정보가 코드를 볼 수, 당신은 참조 할 수 있습니다 이 경로 유사한 질문

세부 사항

  • Gugu 구.

참조 코드

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <queue>
#define rg register
using namespace std;
template < class T > inline void read(T& s) {
    s = 0; int f = 0; char c = getchar();
    while (!isdigit(c)) f |= c == '-', c = getchar();
    while (isdigit(c)) s = s * 10 + c - 48, c = getchar();
    s = f ? -s : s;
}

const int _ = 502;
const int __ = 30002;
const int INF = 2147483647;

int tot = 1, head[_], nxt[__ << 1], ver[__ << 1], cap[__ << 1];
inline void Add_edge(int u, int v, int d)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v, cap[tot] = d; }
inline void link(int u, int v, int d) { Add_edge(u, v, d), Add_edge(v, u, 0); }

int n, p, q, s, t, dep[_], cur[_];

inline int bfs() {
    static queue < int > Q;
    memset(dep, 0, sizeof (int) * (t - s + 1));
    dep[s] = 1, Q.push(s);
    while (!Q.empty()) {
        int u = Q.front(); Q.pop();
        for (rg int i = head[u]; i; i = nxt[i]) {
            int v = ver[i];
            if (dep[v] == 0 && cap[i] > 0)
                dep[v] = dep[u] + 1, Q.push(v);
        }
    }
    return dep[t] > 0;
}

inline int dfs(int u, int flow) {
    if (u == t) return flow;
    for (rg int& i = cur[u]; i; i = nxt[i]) {
        int v = ver[i];
        if (dep[v] == dep[u] + 1 && cap[i] > 0) {
            int res = dfs(v, min(flow, cap[i]));
            if (res) { cap[i] -= res, cap[i ^ 1] += res; return res; }
        }
    }
}

inline int Dinic() {
    int res = 0;
    while (bfs()) {
        for (rg int i = s; i <= t; ++i) cur[i] = head[i];
        while (int d = dfs(s, INF)) res += d;
    }
    return res;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("cpp.in", "r", stdin);
    freopen("cpp.out", "w", stdout);
#endif
    read(n), read(p), read(q);
    s = 0, t = p + 2 * n + q + 1;
    int f;
    for (rg int i = 1; i <= n; ++i)
        for (rg int j = 1; j <= p; ++j) {
            read(f); if (f) link(j, i + p, 1);
        }
    for (rg int i = 1; i <= n; ++i)
        for (rg int j = 1; j <= q; ++j) {
            read(f); if (f) link(i + p + n, j + p + 2 * n, 1);
        }
    for (rg int i = 1; i <= n; ++i) link(i + p, i + p + n, 1);
    for (rg int i = 1; i <= p; ++i) link(s, i, 1);
    for (rg int i = 1; i <= q; ++i) link(i + p + 2 * n, t, 1);
    printf("%d\n", Dinic());
    return 0;
}

끝 일러스트 sahua \ (qwq \)

추천

출처www.cnblogs.com/zsbzsb/p/11827417.html