hdu 2063 过山车(水题,二分图匹配)

小记:很久没写匈牙利算法了,有点生疏,WA了一次


思路:匈牙利算法求最大匹配


代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

#define mst(a,b) memset(a,b,sizeof(a))
#define eps 10e-8

const int MAX_ = 510;
const int N = 100010;
const int INF = 0x7fffffff;

int mp[MAX_][MAX_];
int link[MAX_];
bool vis[MAX_];
int n, m;

int dfs(int x){
    for(int i = 1; i <= m; ++i){
        if(!vis[i] && mp[x][i]){
            vis[i] = 1;

            if(link[i] == -1 || dfs(link[i])){
                link[i] = x;
                return 1;
            }
        }
    }
    return 0;
}

int main(){
    int  ans, k, s, t;
	while(scanf("%d",&k) && k){
	    scanf("%d%d",&n,&m);
	    mst(mp,0);
        for(int i = 1; i <= k; ++i){
            scanf("%d%d",&s,&t);
            mp[s][t] = 1;
        }
        mst(link,-1);

        ans = 0;

        for(int i = 1; i <= n; ++i){
            mst(vis,0);
            if(dfs(i)){
                ans++;
            }
        }
        printf("%d\n",ans);
	}
	return 0;
}


你可能感兴趣的:(hdu 2063 过山车(水题,二分图匹配))