zoukankan      html  css  js  c++  java
  • 匈牙利算法 最大匹配 模板

    #include <bits/stdc++.h>
    using namespace std;
    const int MAXN = 1005;
    int n, m, e;
    
    int match[MAXN];
    bool g[MAXN][MAXN], vis[MAXN];
    
    bool dfs(int u)
    {
        for(int v = 1; v <= m; v++)
            if(g[u][v] && !vis[v])
            {
                vis[v] = 1;
                if(match[v] == -1 || dfs(match[v]))
                {
                    match[v] = u; return 1;
                }
            }
        return 0;
    }
    
    inline int Maxmatch()
    {
        memset(match, -1, sizeof match);
        int ret = 0;
        for(int i = 1; i <= n; i++)
        {
            memset(vis, 0, sizeof vis);
            ret += dfs(i);
        }
        return ret;
    }
    
    int main()
    {
        int x, y;
        scanf("%d%d%d", &n, &m, &e);
        while(e--)
        {
            scanf("%d%d", &x, &y);
            if(x <= n && y <= m) g[x][y] = 1;
        }
        printf("%d
    ", Maxmatch());
    }
    

    O(n^3)

  • 相关阅读:
    Shell Sort
    Insertion Sort
    Notations
    Nakamori Akina
    QuickSort
    Onedrive File Open Problem
    JSON Introduction
    System Call
    进程软中断通信
    Bubble Sort
  • 原文地址:https://www.cnblogs.com/Orz-IE/p/12039495.html
Copyright © 2011-2022 走看看