匈牙利算法模板题
这里把男生和女生的位置换了
变成女生找男生了
code:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include<cmath>
#include<cstdlib>
#include<map>
#include<string>
#include<vector>
#include<queue>
#include<functional>
#pragma warning(disable:4996)
//最大边的最小权值
using namespace std;
typedef long long ll;
const int INF = 999999999;
typedef pair<int, int> P;//第一个值表示最大权值
struct edge {
int to, cost;
};
int mn;
int d[1001];
vector<edge>G[1005];
const int maxn = 510;
int line[maxn][maxn];//邻接矩阵
int used[maxn], nxt[510];
int k,m, n;
bool find(int x)
{
for (int j = 1; j <= n; j++)//遍历每一个妹子 看看有没有妹子能和x男生匹配
{
if (line[x][j] && !used[j])//j号女生没有被匹配过或者之前被匹配过了或者匹配失败
{
used[j] = 1;
if (nxt[j] == 0 || find(nxt[j]))//j号妹子名花无主或者之前与j号妹子匹配的男生能和其他妹子匹配
{
nxt[j] = x;
return true;
}
}
}
return false;
}
int match()
{
int sum = 0;
for (int i = 1; i <= m; i++)//给每一个人找一个配对的老婆
{
memset(used, 0, sizeof(used));
if (find(i))sum++;
}
return sum;
}
int main()
{
while (scanf("%d", &k) != EOF&&k)
{
scanf("%d%d",&m,&n);
memset(nxt,0,sizeof(nxt));
memset(line,0,sizeof(line));
for (int i = 1; i <= k; i++)
{
int x, y;
scanf("%d%d",&x,&y);
line[x][y] = 1;
}
printf("%d\n",match());
}
}