匈牙利算法 求最大匹配

不断找增广路,直到没有增广路,每找到一条增广路匹配数就加1

 

//hungary 
const int X=100,Y=100;
int match[Y];// initial to -1
bool vis[Y];
int g[X][Y];
bool dfs(int x){
    for(int y=1;y<=Y;y++){
        if(g[x][y]&&!vis[y]){
            vis[y]=1;
            if(match[y]==-1||dfs(match[y])){
                match[y]=x;
                return true;
            }
        }
    }
    return false;
}
void hungary(){
    int cnt=0;
    for(int i=1;i<=X;i++){
        memset(vis,0,sizeof(vis));
        if(dfs(i))cnt++;
    }
}

posted on 2014-05-31 11:12  wanggp3  阅读(224)  评论(0编辑  收藏  举报

导航