先把代码贴上,其他南京回来再补了。。

 #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算法的更多相关文章

  1. 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 ...

  2. HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

    Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  3. USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)

    The Perfect StallHal Burch Farmer John completed his new barn just last week, complete with all the ...

  4. 训练指南 UVALive - 4043(二分图匹配 + KM算法)

    layout: post title: 训练指南 UVALive - 4043(二分图匹配 + KM算法) author: "luowentaoaa" catalog: true ...

  5. HDU 5727 - Necklace - [全排列+二分图匹配][Hopcroft-Karp算法模板]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5727 Problem DescriptionSJX has 2*N magic gems. ...

  6. Codevs 1222 信与信封问题 二分图匹配,匈牙利算法

    题目: http://codevs.cn/problem/1222/ 1222 信与信封问题   时间限制: 1 s   空间限制: 128000 KB   题目等级 : 钻石 Diamond 题解 ...

  7. HDU1507 Uncle Tom's Inherited Land* 二分图匹配 匈牙利算法 黑白染色

    原文链接http://www.cnblogs.com/zhouzhendong/p/8254062.html 题目传送门 - HDU1507 题意概括 有一个n*m的棋盘,有些点是废的. 现在让你用1 ...

  8. (转)二分图匹配匈牙利算法与KM算法

    匈牙利算法转自于: https://blog.csdn.net/dark_scope/article/details/8880547 匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名 ...

  9. BZOJ1059 [ZJOI2007]矩阵游戏 二分图匹配 匈牙利算法

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1059 题意概括 有一个n*n(n<=200)的01矩阵,问你是否可以通过交换整行和整列使得左 ...

随机推荐

  1. TCP窗口扩大选项

    TCP窗口扩大选项(TCP Window Scale Option) TCP窗口扩大选项 TCP Window Scale Option (WSopt) 窗口扩大选项用于扩大TCP通告窗口,使TCP的 ...

  2. 探索未知种族之osg类生物---器官初始化一

    我们把ViewerBase::frame()比作osg这类生物的肺,首先我们先来大概的看一下‘肺’长什么样子,有哪几部分组成.在这之前得对一些固定的零件进行说明,例如_done代表osg的viewer ...

  3. 可迭代对象(Iterable)和迭代器(Iterator)

     迭代是访问集合元素的一种方式. 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一 个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退. 1. 可迭代对象 以直接作用于 ...

  4. RAC初步使用

    信号基本流程: //1:创建信号 RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSub ...

  5. Spring IOC(七)类型推断

    Spring IOC(七)类型推断 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring 容器中可以根据 bean ...

  6. idea部署tomcat-404错误

    今天在部署数据质量web工程在tomcat后,访问时报404错误,在网上搜了很多相关配置,也都是一步一步来的 Application Context 也配置好了,Artifact也添加了,就是访问不了 ...

  7. GBDT-梯度提升树

    随机森林:bagging思想,可以并行,训练集权值相同 可以是分类树,回归树 输出结果(分类树):多数投票         (回归树):均值 减少方差 对异常数据不敏感 GBDT:拟合损失函数 boo ...

  8. 解决textarea 输出有空格问题

    我们在使用textarea标签输出的时候,经常会出现前后都有空格.使用trim()处理也不行. 这个原因是因为 我们在编写textarea标签对的时候使用了换行. 解决方法:就是<textare ...

  9. XAML中用一字符即可展示漂亮的图型

    XAML中用一字符即可展示漂亮的图型 例如:Symbol Icon: People http://www.geekchamp.com/icon-explorer/action-icons/icon?c ...

  10. AJAX-php-json数组

    1.在php中有个数组,响应回前端 $array=["习大大","川普","金三胖"];2.JS对象数据格式 ex: 数组: var TOM ...