这是链接:https://blog.csdn.net/qq_41730082/article/details/81162561
上面是作者的链接:下面内容是复制过来的
匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名。匈牙利算法是基于Hall定理中充分性证明的思想,它是部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最大匹配的算法。
-------等等,看得头大?那么请看下面的版本:
通过数代人的努力,你终于赶上了剩男剩女的大潮,假设你是一位光荣的新世纪媒人,在你的手上有N个剩男,M个剩女,每个人都可能对多名异性有好感-_-||暂时不考虑特殊的性取向),如果一对男女互有好感,那么你就可以把这一对撮合在一起,现在让我们无视掉所有的单相思,你拥有的大概就是下面这样一张关系图,每一条连线都表示互有好感。
本着救人一命,胜造七级浮屠的原则,你想要尽可能地撮合更多的情侣,匈牙利算法的工作模式会教你这样做:
一: 先试着给1号男生找妹子,发现第一个和他相连的1号女生还名花无主,got it,连上一条蓝线
二:接着给2号男生找妹子,发现第一个和他相连的2号女生名花无主,got it
三:接下来是3号男生,很遗憾1号女生已经有主了,怎么办呢?
我们试着给之前1号女生匹配的男生(也就是1号男生)另外分配一个妹子。
(黄色表示这条边被临时拆掉)
与1号男生相连的第二个女生是2号女生,但是2号女生也有主了,怎么办呢?我们再试着给2号女生的原配重新找个妹子(注意这个步骤和上面是一样的,这是一个递归的过程)
此时发现2号男生还能找到3号女生,那么之前的问题迎刃而解了,回溯回去,2号男生可以找3号妹子~~~ 1号男生可以找2号妹子了~~~ 3号男生可以找1号妹子
所以第三步最后的结果就是:
四: 接下来是4号男生,很遗憾,按照第三步的节奏我们没法给4号男生腾出来一个妹子,我们实在是无能为力了……香吉士同学走好。
bool find(int x){
int i,j;
for (j=1;j<=m;j++){ //扫描每个妹子
if (line[x][j]==true && used[j]==false)
//如果有暧昧并且还没有标记过(这里标记的意思是这次查找曾试图改变过该妹子的归属问题,但是没有成功,所以就不用瞎费工夫了)
{
used[j]=1;
if (girl[j]==0 || find(girl[j])) {
//名花无主或者能腾出个位置来,这里使用递归
girl[j]=x;
return true;
}
}
}
return false;
}
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=505;
int line[N][N];
int girl[N],used[N];
int k,m,n;
bool found(int x)
{
for(int i=1; i<=n; i++)
{
if(line[x][i]&&!used[i])
{
used[i]=1;
if(girl[i]==0||found(girl[i]))
{
girl[i]=x;
return 1;
}
}
}
return 0;
}
int main()
{
int x,y;
while(scanf("%d",&k)&&k)
{
scanf("%d %d",&m,&n);
memset(line,0,sizeof(line));
memset(girl,0,sizeof(girl));
for(int i=0; i<k; i++)
{
scanf("%d %d",&x,&y);
line[x][y]=1;
}
int sum=0;
for(int i=1; i<=m; i++)
{
memset(used,0,sizeof(used));
if(found(i)) sum++;
}
printf("%d\n",sum);
}
return 0;
}
以下是本人的内容,哈哈哈
下面举个例子:
题目链接:http://poj.org/problem?id=1274
The Perfect Stall
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 29810 Accepted: 13039
Description
Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in the new barn are different. For the first week, Farmer John randomly assigned cows to stalls, but it quickly became clear that any given cow was only willing to produce milk in certain stalls. For the last week, Farmer John has been collecting data on which cows are willing to produce milk in which stalls. A stall may be only assigned to one cow, and, of course, a cow may be only assigned to one stall.
Given the preferences of the cows, compute the maximum number of milk-producing assignments of cows to stalls that is possible.
Input
The input includes several cases. For each case, the first line contains two integers, N (0 <= N <= 200) and M (0 <= M <= 200). N is the number of cows that Farmer John has and M is the number of stalls in the new barn. Each of the following N lines corresponds to a single cow. The first integer (Si) on the line is the number of stalls that the cow is willing to produce milk in (0 <= Si <= M). The subsequent Si integers on that line are the stalls in which that cow is willing to produce milk. The stall numbers will be integers in the range (1..M), and no stall will be listed twice for a given cow.
Output
For each case, output a single line with a single integer, the maximum number of milk-producing stall assignments that can be made.
Sample Input
5 5
2 2 5
3 2 3 4
2 1 5
3 1 2 5
1 2
Sample Output
4
Source
USACO 40
/*bipartite_matching矩阵表示暧昧关系*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=210,maxm=210;
bool map[maxn][maxm];//表示是否有暧昧关系
bool used[maxn];
int girl[maxn];
int m,n;
bool find(int x)
{
for(int y=1;y<=m;y++)
{
if(map[x][y] && used[y]==false)
{
used[y]=true;
if(girl[y]<0 || find(girl[y]))
{
girl[y]=x;
return true;
}
}
}
return false;
}
int bipartite_matching()
{
int num=0;//找到女友的人数
memset(girl,-1,sizeof(girl));
for(int i=1;i<=n;i++)
{
memset(used,0,sizeof(used));
if(find(i)) num++;
}
return num;
}
int main(void)
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(map,0,sizeof(map));
for(int i=1;i<=n;i++)
{
int num,x;
scanf("%d",&num);
for(int j=1;j<=num;j++)
{
scanf("%d",&x);
map[i][x]=true;
}
}
int res=bipartite_matching();
printf("%d\n",res);
}
return 0;
}
/*bipartite_matching向量表示暧昧关系*/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
const int maxn=210;
int m,n,t,x;
vector<int>G[maxn];
int match[maxn];
bool used[maxn];
bool dfs(int v)
{
for(int i=0;i<G[v].size();i++)
{
int u=G[v][i];
if(!used[u])
{
used[u]=true;
if(match[u]<0 || dfs(match[u]))
{
match[u]=v;
return true;
}
}
}
return false;
}
int bipartite_matching()
{
int res=0;
memset(match,-1,sizeof(match));
for(int i=1;i<=n;i++)
{
memset(used,0,sizeof(used));
if(dfs(i)) res++;
}
return res;
}
int main(void)
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)
{
cin>>t;
for(int j=0;j<t;j++)
{
cin>>x;
G[i].push_back(x);
}
}
int res=bipartite_matching();
for(int i=1;i<=n;i++)
{
G[i].resize(0);
}
cout<<res<<endl;
}
return 0;
}