hdu2063[过山车] 二分图匹配 匈牙利算法

来源:互联网 发布:河北秦淮数据有限公司 编辑:程序博客网 时间:2024/04/30 16:52

过山车
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10683 Accepted Submission(s): 4699

Problem Description
RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了。可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,
就是每个女生必须找个个男生做partner和她同坐。但是,每个女孩都有各自的想法,举个例子把,Rabbit只愿意和XHD或PQK做partner,Grass
只愿意和linle或LL做partner,PrincessSnow愿意和水域浪子或伪酷儿做partner。考虑到经费问题,boss刘决定只让找到partner的人去坐过
山车,其他的人,嘿嘿,就站在下面看着吧。聪明的Acmer,你可以帮忙算算最多有多少对组合可以坐上过山车吗?

Input
输入数据的第一行是三个整数K , M , N,分别表示可能的组合数目,女生的人数,男生的人数。0

solution:

二分图匹配 匈牙利算法

#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>using namespace std;int chk[550][550], line[550], map[550];int m, n;bool check( int x ){    for ( int i = 1; i <= n; i++ ){        if ( chk[x][i] && !map[i] ){            map[i] = 1;            if ( !line[i] || check( line[i] ) ){                line[i] = x;                return true;            }        }    }    return false;}int main(){    int k;    while ( scanf( "%d%d%d", &k, &m, &n )!= EOF && k != 0 ){        memset( chk, 0, sizeof(chk));        memset( line, 0, sizeof(map));        int a, b;        for ( int i = 1; i <= k; i++)  scanf( "%d%d", &a, &b ), chk[a][b] = 1;        int ans = 0;        for ( int i = 1; i <= m; i++) {            memset( map, 0, sizeof(map));            if ( check( i ) ) ans++;        }        printf( "%d\n", ans);    }    return 0;}