今晚学习了hopcroft_karp算法,留个模板,顺便也留个匈牙利算法模板。
题目为POJ 1469,在本题中,两种算法所用时间基本一致
//hopcroft_karp算法,复杂度O(sqrt(n)*m)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int N = 320;
const int INF = 0x3f3f3f3f;
struct edge
{
int to, next;
}g[N*N];
int match[N], head[N];
bool used[N];
int p, n;
int nx, ny, cnt, dis; //nx,ny分别是左点集和右点集的点数
int dx[N], dy[N], cx[N], cy[N]; //dx,dy分别维护左点集和右点集的标号
//cx表示左点集中的点匹配的右点集中的点,cy正好相反
void add_edge(int v, int u)
{
g[cnt].to = u, g[cnt].next = head[v], head[v] = cnt++;
}
bool bfs() //寻找增广路径集,每次只寻找当前最短的增广路
{
queue<int> que;
dis = INF;
memset(dx, -1, sizeof dx);
memset(dy, -1, sizeof dy);
for(int i = 1; i <= nx; i++)
if(cx[i] == -1) //将未遍历的节点