二分图匹配-HK算法
先把代码贴上,其他南京回来再补了。。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <ctime>
#include <vector>
#include <list>
#include <queue>
#define M0(a) memset(a, 0, sizeof(a))
#define Inf 0x7fffffff
#define MXN 2110
using namespace std;
vector<int> edge[MXN];
int dx[MXN], dy[MXN];
int a[][];
bool state[];
int match[], Mx[];
int n, m; void add(int x, int y){
edge[x].push_back(y);
edge[y].push_back(x);
} void init(){
M0(a);
int x, y;
for (int i = ; i <= n + m; ++i)
edge[i].clear();
for (int i = ; i <= n; ++i){
scanf("%d%d", &x, &y);
a[x][y] = i;
a[x + ][y] = i;
}
for (int i = ; i <= m; ++i){
scanf("%d%d", &x, &y);
int now = a[x][y];
if (now) add(i + n, now);
now = a[x][y + ];
if (now) add(i + n, now);
}
} bool bfs(){
queue<int> q;
bool flag = false;
M0(dx);
M0(dy);
for (int i = ; i <= n; ++i)
if (!Mx[i]){
q.push(i);
dx[i] = ;
}
int u, v;
while (!q.empty()){
u = q.front();
for (int i = ; i < edge[u].size(); ++i){
v = edge[u][i];
if (dy[v]) continue;
dy[v] = dx[u] + ;
if (match[v] == -) flag = true;
else {
q.push(match[v]);
dx[match[v]] = dy[v] + ;
}
}
q.pop();
}
return flag;
} bool search_path(int u){
int v;
for (int i = ; i < edge[u].size(); ++i){
v = edge[u][i];
if (dy[v] != dx[u] + ) continue;
if (!state[v]){
state[v] = true;
if (match[v] == - || search_path(match[v])){
Mx[u] = v;
match[v] = u;
return true;
}
}
}
return false;
} void HK(){
M0(Mx);
int M = ;
memset(match, -, sizeof(match));
while (bfs()){
M0(state);
for (int i = ; i <= n; ++i)
if(!Mx[i] && search_path(i)) ++M;
}
printf("%d\n", n + m - M);
} int main(){
// freopen("hdu4619.in","r", stdin);
// freopen("hdu4619.out","w", stdout);
while (scanf("%d%d", &n, &m) != EOF){
if (!n) break;
init();
HK();
}
// fclose(stdin); fclose(stdout);
}
二分图匹配-HK算法的更多相关文章
- hdu2389 Rain on your Parade 二分图匹配--HK算法
You’re giving a party in the garden of your villa by the sea. The party is a huge success, and every ...
- HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))
Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)
The Perfect StallHal Burch Farmer John completed his new barn just last week, complete with all the ...
- 训练指南 UVALive - 4043(二分图匹配 + KM算法)
layout: post title: 训练指南 UVALive - 4043(二分图匹配 + KM算法) author: "luowentaoaa" catalog: true ...
- HDU 5727 - Necklace - [全排列+二分图匹配][Hopcroft-Karp算法模板]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5727 Problem DescriptionSJX has 2*N magic gems. ...
- Codevs 1222 信与信封问题 二分图匹配,匈牙利算法
题目: http://codevs.cn/problem/1222/ 1222 信与信封问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 ...
- HDU1507 Uncle Tom's Inherited Land* 二分图匹配 匈牙利算法 黑白染色
原文链接http://www.cnblogs.com/zhouzhendong/p/8254062.html 题目传送门 - HDU1507 题意概括 有一个n*m的棋盘,有些点是废的. 现在让你用1 ...
- (转)二分图匹配匈牙利算法与KM算法
匈牙利算法转自于: https://blog.csdn.net/dark_scope/article/details/8880547 匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名 ...
- BZOJ1059 [ZJOI2007]矩阵游戏 二分图匹配 匈牙利算法
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1059 题意概括 有一个n*n(n<=200)的01矩阵,问你是否可以通过交换整行和整列使得左 ...
随机推荐
- andorid 表格布局
tablelayout.xml表格布局 <?xml version="1.0" encoding="utf-8"?> <TableLayout ...
- Android.FamousBlogs
1. cyrilmottier http://cyrilmottier.com/ http://cyrilmottier.com/about/ 2. greenrobot http://greenro ...
- Centos PS1
PS1="[\[\e[35m\]\u\[\e[m\]\[\e[31m\]->\[\e[m\]\[\e[33m\]\H\[\e[m\]\[\e[31m\]->\[\e[m\]\[\ ...
- 码代码的小女孩(来自noip贴吧)
天冷极了,下着雪,又快黑了.这是NOIP的前夜.在这又冷又黑的晚上,一个衣衫破烂的小女孩在机房敲着代码.她从班里逃出来的时候还拿着一本算导,但是有什么用呢?那是一本很破旧的书--那么大,一向是她妈妈垫 ...
- Servlet开发的三种方法
第一种 实现 Servlet 接口,需要覆写 Servlet 的5个方法,并将ServletConfig对象保存到类级变量中 package app01a; import java.io.IOExce ...
- How to convert a PDF file to JPEGs using PHP
Hey, Today I would like to show you how we can convert PDF to JPEG using imagick extension. Imagick ...
- macOS X Mount NFS Share / Set an NFS Client
last updated November 3, 2018 in CategoriesLinux, Mac OS X, UNIX How do I access my enterprise NAS s ...
- rap2与postman自动化测试
rap2的接口数据可以全部导入postman: 在collections集合里面点击run;
- tomcat探索
双击apache-tomcat-7.0.64.exe,一路默认点. 静态页路径 C:\Program Files\Apache Software Foundation\Tomcat 7.0\webap ...
- 【Linux】zlib安装
zlib简介 zlib是提供数据压缩用的函式库,由Jean-loup Gailly与Mark Adler所开发,初版0.9版在1995年5月1日发表.zlib使用DEFLATE算法,最初是为libpn ...