您的位置:首页 > 其它

二分图最大匹配匈牙利算法模板两种

2014-06-04 14:27 253 查看
1.邻接表(vector向量法)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 1007

int path

,vis
;
int match
;
int n,m;

int Search_Path(int s)
{
for(int i=1;i<=m;i++)
{
if(path[s][i] && !vis[i])
{
vis[i] = 1;
if(match[i] == -1 || Search_Path(match[i]))
{
match[i] = s;
return 1;
}
}
}
return 0;
}

int main()
{
int cnt,i,j,k;
int a,b;
while(scanf("%d",&k)!=EOF && k)
{
scanf("%d%d",&n,&m);
memset(match,-1,sizeof(match));
memset(path,0,sizeof(path));
for(i=0;i<k;i++)
{
scanf("%d%d",&a,&b);
path[a][b] = 1;
}
cnt = 0;
for(i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
if(Search_Path(i))
cnt++;
}
printf("%d\n",cnt);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: