Esempio n. 1
0
int main()
{
	int xmatch[MAX], ymatch[MAX];
	bipartite b1(4, 5);
	b1.b_g[0][0] = 1; b1.b_g[0][1] = 1; b1.b_g[0][2] = 1; b1.b_g[0][3] = 1;
	b1.b_g[1][0] = 1; b1.b_g[1][1] = 1; b1.b_g[1][3] = 1; b1.b_g[1][4] = 1;
	b1.b_g[2][1] = 1; b1.b_g[2][2] = 1; b1.b_g[2][3] = 1; b1.b_g[2][4] = 1;
	b1.b_g[3][0] = 1; b1.b_g[3][1] = 1; b1.b_g[3][2] = 1; b1.b_g[3][4] = 1;
	cout << "hungarian 1 max binary match: " << hungarian1(b1, xmatch, ymatch) << endl;	//1
	print_match(b1, ymatch);
	cout << "hungarian 2 max binary match: " << hungarian2(b1, ymatch) << endl;
	print_match(b1, ymatch);
	cout << "hopcroft karp max binary match: " << hopcroft_karp(b1, xmatch, ymatch) << endl;	//2
	print_match(b1, ymatch);

	bipartite b2(5, 5);
	b2.b_g[0][0] = 3; b2.b_g[0][1] = 5; b2.b_g[0][2] = 5; b2.b_g[0][3] = 4; b2.b_g[0][4] = 1;
	b2.b_g[1][0] = 2; b2.b_g[1][1] = 2; b2.b_g[1][2] = 0; b2.b_g[1][3] = 2; b2.b_g[1][4] = 2;
	b2.b_g[2][0] = 2; b2.b_g[2][1] = 4; b2.b_g[2][2] = 4; b2.b_g[2][3] = 1; b2.b_g[2][4] = 0;
	b2.b_g[3][0] = 0; b2.b_g[3][1] = 1; b2.b_g[3][2] = 1; b2.b_g[3][3] = 0; b2.b_g[3][4] = 0;
	b2.b_g[4][0] = 1; b2.b_g[4][1] = 2; b2.b_g[4][2] = 1; b2.b_g[4][3] = 3; b2.b_g[4][4] = 3;
	cout << "kuhn munkres: " << kuhn_munkres(b2, ymatch) << endl; //5
	print_match(b2, ymatch);

	graph_matrix g(6);
	g.g_m[0][0] = 0; g.g_m[0][1] = 1; g.g_m[0][2] = 1; g.g_m[0][3] = 1; g.g_m[0][4] = INF; g.g_m[0][5] = INF;
	g.g_m[1][0] = INF; g.g_m[1][1] = 0; g.g_m[1][2] = INF; g.g_m[1][3] = 1; g.g_m[1][4] = 1; g.g_m[1][5] = INF;
	g.g_m[2][0] = INF; g.g_m[2][1] = INF; g.g_m[2][2] = 0; g.g_m[2][3] = 1; g.g_m[2][4] = INF; g.g_m[2][5] = 1;
	g.g_m[3][0] = 0; g.g_m[3][1] = INF; g.g_m[3][2] = INF; g.g_m[3][3] = 0; g.g_m[3][4] = 1; g.g_m[3][5] = 1;
	g.g_m[4][0] = INF; g.g_m[4][1] = INF; g.g_m[4][2] = INF; g.g_m[4][3] = INF; g.g_m[4][4] = 0; g.g_m[4][5] = 1;
	g.g_m[5][0] = INF; g.g_m[5][1] = INF; g.g_m[5][2] = INF; g.g_m[5][3] = INF; g.g_m[5][4] = INF; g.g_m[5][5] = 0;
	cout << "disjoint path covering: " << minimum_disjoint_path_covering(g) << endl;//3
	cout << "joint path covering: " << minimum_joint_path_covering(g) << endl; //4
	return(0);
}
Esempio n. 2
0
int main() {
	testcase t;
	t = read_input();
	hopcroft_karp(t);
	printf("%d\n", t->matching);
	testcase_destroy(t);
	return 0;
}
int minimum_disjoint_path_covering(graph_matrix g)
{//简单有向图G有g_cnt个节点,下标从0到g_cnt-1
 //若从节点i指向节点j存在有向边则g_m[i][j]为1
 //同一节点的g_m值为0,不相连节点的g_m值为INF
 //返回最小不相交路径覆盖数
	bipartite b;
	int xmatch[MAX], ymatch[MAX];
	memset(xmatch, -1, MAX * sizeof(int));
	memset(ymatch, -1, MAX * sizeof(int));
	construct_b(g, b);
	return(g.g_cnt - hopcroft_karp(b, xmatch, ymatch));
}