Hopcroft-Carp 算法模板 自用

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn = 500, INF = 0xfffffff;
int cx[maxn], cy[maxn], dx[maxn], dy[maxn], used[maxn], g[maxn][maxn];
int nx, ny, dis;
int bfs()
{
    queue<int> Q;
    dis = INF;
    mem(dx,-1);
    mem(dy,-1);
    for(int i=1; i<=nx; i++)
    {
        if(cx[i] == -1)
        {
            Q.push(i);
            dx[i] = 0;
        }
    }

    while(!Q.empty())
    {
        int u = Q.front(); Q.pop();
        if(dx[u] > dis) break;
        for(int i=1; i<=ny; i++)
        {
            if(g[u][i] && dy[i] == -1)
            {
                dy[i] = dx[u] + 1;
                if(cy[i] == -1) dis = dy[i];
                else
                {
                    dx[cy[i]] = dy[i] + 1;
                    Q.push(cy[i]);

                }
            }
        }
    }
    return dis != INF;
}

int dfs(int u)
{
    for(int i=1; i<=ny; i++)
    {
        if(g[u][i] && !used[i] && dy[i] == dx[u] + 1)
        {
            used[i] = 1;
            if(cy[i] != -1 && dis == dy[i]) continue;
            if(cy[i] == -1 || dfs(cy[i]))
            {
                cy[i]= u;
                cx[u] = i;
                return 1;
            }
        }
    }
    return 0;
}
int hc()
{
    int res = 0;
    mem(cx,-1);
    mem(cy,-1);
    while(bfs())
    {
        mem(used,0);
        for(int i=1; i<=nx; i++)
        {
            if(cx[i] == -1 && dfs(i))
                res++;
        }
    }
    return res;
}
int main()
{




    return 0;
}

 

posted @ 2018-06-12 13:00  WTSRUVF  阅读(300)  评论(0编辑  收藏  举报