Hopcroft-Carp算法模板【二分图匹配】

本文介绍了一种基于Hopcroft-Carp算法和匈牙利算法实现的最大匹配问题解决方案,并通过具体代码展示了这两种算法在求解二分图最大匹配问题上的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模板://hdu 2063

Hopcroft-Carp 时间复杂度为 O(sqrt(V)*E);
而匈牙利算法为 O(V*E);

#include <stdio.h>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>

using namespace std;

const int N = 1005;
const int INF = 1 << 28;

int g[N][N];
int Mx[N];
int My[N];
int dx[N];
int dy[N];
bool used[N];

int Nx, Ny, dis;

bool searchP()
{
    dis = INF;
    int i, v, u;
    std::queue<int> Q;

    memset(dx, -1, sizeof(dx));
    memset(dy, -1, sizeof(dy));
    for (i = 0; i<Nx; i++)
    {
        if (Mx[i] == -1)
        {
            Q.push(i);
            dx[i] = 0;
        }
    }
    while (!Q.empty())
    {
        u = Q.front();
        Q.pop();
        if (dx[u]>dis) break;
        for (v = 0; v<Ny; v++)
        {
            if (g[u][v] && dy[v] == -1)
            {
                dy[v] = dx[u] + 1;
                if (My[v] == -1) dis = dy[v];
                else
                {
                    dx[My[v]] = dy[v] + 1;
                    Q.push(My[v]);
                }
            }
        }
    }
    return dis != INF;
}

bool DFS(int u)
{
    int v;
    for (v = 0; v<Ny; v++)
    {
        if (g[u][v] && !used[v] && dy[v] == dx[u] + 1)
        {
            used[v] = true;
            if (My[v] != -1 && dy[v] == dis) continue;
            if (My[v] == -1 || DFS(My[v]))
            {
                My[v] = u;
                Mx[u] = v;
                return true;
            }
        }
    }
    return false;
}

int Hungary()
{
    int u;
    int ret = 0;
    memset(Mx, -1, sizeof(Mx));
    memset(My, -1, sizeof(My));
    while (searchP())
    {
        memset(used, false, sizeof(used));
        for (u = 0; u<Nx; u++)
            if (Mx[u] == -1 && DFS(u))  ret++;
    }
    return ret;
}

int main()
{
    int k, u, v;
    while (~scanf("%d", &k) ,k)
    {
        scanf("%d%d", &Nx, &Ny);
        memset(g, 0, sizeof(g));
        Ny = Nx>Ny ? Nx : Ny;
        while (k--)
        {
            scanf("%d%d", &u, &v);
            u--; v--;
            g[u][v] = 1;
        }
        int ans = Hungary();
        printf("%d\n", ans);
    }
    return 0;
}
#define geo_max(a,b) (((a) > (b)) ? (a) : (b)) #define geo_min(a,b) (((a) < (b)) ? (a) : (b)) GEO_EXPORT mappoint geo_trangeo2map(georect* grt, geopoint* pt, maprect* mrt); GEO_EXPORT geopoint geo_tranmap2geo(maprect* mrt, mappoint* mpt, georect* grt); /* *判断点在不在矩形里.在返回0 * 不在返回-1 */ GEO_EXPORT int geo_gptingrt(geopoint* gpt, georect* grt); GEO_EXPORT int geo_mptinmrt(mappoint* mpt, maprect* mrt); GEO_EXPORT geobool geo_gptingpolygon(geopoint* gpt, geopoints *gpts); /* *返回单位为:米 */ GEO_EXPORT double geo_distance(geopoint* gptfrom, geopoint* gptto); GEO_EXPORT double geo_distance_pt2polyline(geopoint* gpt, geopoints* gpts, geopoint* rnearestpt); GEO_EXPORT double geo_distance_pt2polygon(geopoint* gpt, geopoints* gpts, geopoint* rnearestpt); /* *返回单位为:平方米 */ //GEO_EXPORT double geo_area(geopoints* gpts); /* *返回单位为:度 *正北为0度,顺时针为正 */ GEO_EXPORT double geo_direction(geopoint* gptfrom, geopoint* gptto); /* * 矩形与矩形的关系 * a 在 b 里面, 返回 0 * a 部份在 b 里面,返回 1 * a 没和 b 相交, 返回 -1 * /\ y * | * | * | * |------------> x * o */ int geo_grtingrt(georect* rta, georect* rtb); /* * 矩形与矩形的关系 * a 在 b 里面, 返回 0 * a 部份在 b 里面,返回 1 * a 没和 b 相交, 返回 -1 * * o * |------------> x * | * | * | * | * \/ y */ int geo_mrtinmrt(maprect* rta, maprect* rtb); //判断线段是否相关 //相交返回GEO_TRUE //不相交返回GEO_FALSE geobool geo_linecrossline(geopoint* lafrom, geopoint* lato, geopoint* lbfrom, geopoint* lbto); /* * 多边形与多边形的关系 * a 在 b 里面, 返回 0 * a 和 b 相交,返回 1 * a 没和 b 相交, 返回 -1 * b 在 a 里面, 返回 -2 * * /\ y * | * | * | * |------------> x * o */ int geo_gpolygoningpolygon(geopoints* gptsa, geopoints* gptsb);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值