Binary graph matching (Hungarian algorithm template)

Source: Internet
Author: User

Hungarian algorithm template with two-point maximum matching

/**************************************************** binary Map matching (DFS implementation of the Hungarian algorithm) init:g[][] Call:res=hungary () on both sides of the fixed-point division Output maximum Match number advantages: suitable for dense map, DFS find the augmented road fast, simple and easy to understand time complexity: O (VE); ****************************************************/Const intMAXN =510;intUN,VN;//Number of U,vintG[MAXN][MAXN];//the number is 1~n.intLINKER[MAXN];BOOLUSED[MAXN];BOOLDfsintu) {    intv;  for(v=1; v<=vn;v++){        if(g[u][v]&&!Used[v]) {Used[v]=true; if(linker[v]==-1||DFS (Linker[v])) {Linker[v]=T; return true; }        }    }    return false;}intHungary () {intres=0; intu; MEMSET (linker,-1,sizeof(linker));  for(u=1; u<=un;u++) {memset (used,false,sizeof(used)); if(Dfs (U)) res++; }    returnRes;}

Example: HDU 2063 roller Coaster (template problem)

#include <stdio.h>#include<string.h>#include<iostream>using namespacestd;/**************************************************** binary Map matching (DFS implementation of the Hungarian algorithm) init:g[][] Call:res=hungary () on both sides of the fixed-point division Output maximum Match number advantages: suitable for dense map, DFS find the augmented road fast, simple and easy to understand time complexity: O (VE); ****************************************************/Const intMAXN =510;intUN,VN;//Number of U,vintG[MAXN][MAXN];//the number is 1~n.intLINKER[MAXN];BOOLUSED[MAXN];BOOLDfsintu) {    intv;  for(v=1; v<=vn;v++){        if(g[u][v]&&!Used[v]) {Used[v]=true; if(linker[v]==-1||DFS (Linker[v])) {Linker[v]=u; return true; }        }    }    return false;}intHungary () {intres=0; intu; MEMSET (linker,-1,sizeof(linker));  for(u=1; u<=un;u++) {memset (used,false,sizeof(used)); if(Dfs (U)) res++; }    returnRes;}intMain () {intK;  while(SCANF ("%d", &k) = =1&&k) {scanf ("%d%d",&un,&VN); memset (G,0,sizeof(G));  for(intI=1; i<=k;i++){            intu,v; scanf ("%d%d",&u,&v); G[U][V]=1; } printf ("%d\n", Hungary ()); }    return 0;}

Binary graph matching (Hungarian algorithm template)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.