HDU3605 Escape 题解 二分图多重匹配 模板题

题目链接:https://acm.hdu.edu.cn/showproblem.php?pid=3605

说明

hdu 的 G++ 编译器 比 C++ 编译器 慢,而这题比较卡时间,需要用 C++ 编译器。

但是 C++ 编译器不支持 万能头,所以你得手写 非万能头 的头文件,然后用 C++ 提交。


题目大意:二分图多重匹配 模板题。

其他一些优化:没有每次清空 vis 数组,而是开了一个时间戳 ts,这样快一点。

示例程序:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 5;

int n, m, g[maxn][15], a[15], match[15][maxn], cur[maxn], ts;
int vis[15];

void init() {
    for (int i = 1; i <= m; i++)
        cur[i] = 0;
}

bool dfs(int x) {
    for (int y = 1; y <= m; y++) {
        if (g[x][y] && vis[y] < ts) {
            vis[y] = ts;
            if (cur[y] < a[y]) {
                int idx = ++cur[y];
                match[y][idx] = x;
                return true;
            }
            for (int i = 1; i <= a[y]; i++) {
                if (dfs(match[y][i])) {
                    match[y][i] = x;
                    return true;
                }
            }
        }
    }
    return false;
}

bool check() {
    for (int x = 1; x <= n; x++) {
        ts++;
        if (!dfs(x))
            return false;
    }
    return true;
}

int main() {
    while (~scanf("%d%d", &n, &m)) {
        init();
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                scanf("%d", &g[i][j]);
        for (int i = 1; i <= m; i++)
            scanf("%d", a+i);
        puts(check() ? "YES" : "NO");
    }
    return 0;
}
posted @ 2026-04-29 14:47  quanjun  阅读(3)  评论(0)    收藏  举报