The Hungarian algorithm Template

The Hungarian algorithm with The adjacency matrix :

计算最大匹配问题

int n1, n2, m, ans;

int res[MAXN];

bool vis[MAXN], map[MAXN][MAXN];



void init(){

    int t1, t2;

    memset(map, 0, sizeof(map));

    memset(res, 0, sizeof(res));

    ans = 0;

    scanf("%d%d",&n1,&n2);



    //get map[][]



}

bool find(int a) {

    for (int i = 1; i <= n2; ++i) {

        if (map[a][i] == 1 && !vis[i]) {

            vis[i] = true;

            if (res[i] == 0 || find(res[i])) {

                res[i] = a;

                return true;

            }

        }

    }

    return false;

}

int main() {

    init();

    for (int i = 1; i <= n1; ++i) {

        memset(vis, 0, sizeof(vis));

        if (find(i)) ++ans;

    }

    printf("%d\n",ans);

    return 0;

}    

 

你可能感兴趣的:(Algorithm)