ACM学习-匈牙利匹配

匈牙利匹配先说一种简单情况,多了也是一样,比如A,B,C找对象,一开始A问B有没有对象,B说没有,OK,A告诉B以后你的对象是我,如果C也开始在周围(相连的节点)找对象,假如找到B,发现B已经有对象,就问B你周围有人单身吗,B发现A单身,就告诉A以后你的对象是我,否则再去询问A周围有没有人单身,依次类推下去,一旦找到类似A这样的单身B必须忍痛割爱承认以后他的对象是C。总结起来是这样的,一旦有节点找对象,找到的话,它访问过的节点就是一条长链,头是要找对象的节点,尾巴是单身的人,如果有中间节点的话,中间是已经有对象的人。执行完之后这条链除了头没有对象,其他人都有对象,也就是说找对象是让别人承认他的对象是我,而我自己却没对象。

// ACM学习-匈牙利匹配.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include<iostream>

#include<vector>

using namespace std;

const int v = 13;

int edge[v][v] = {

//A  B  C  D  E  F  G  H  I  J  K  L  M

{ 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 },//A

{ 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 },//B

{ 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },//C

{ 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 },//D

{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },//E

{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },//F

{ 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0 },//G

{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 },//H

{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },//I

{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 },//J

{ 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },//K

{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1 },//L

{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0 }//M

};

char t[] = { ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘ };

const int n = 100;

vector<int> g[v];

int from[v], tot;

bool use[v];

bool match(int x){

for (int i = 0; i < g[x].size(); i++){

if (!use[g[x][i]])

{

use[g[x][i]] = true;

if (from[g[x][i]] == -1 || match(from[g[x][i]]))

{

if (from[g[x][i]] == -1)

{

cout << t[g[x][i]] << "老公是:" << t[x] << endl;

}

else{

cout << t[g[x][i]] << "旧老公是:" << t[from[g[x][i]]] << endl;

cout << t[g[x][i]] << "新老公是:" << t[x] << endl;

}

from[g[x][i]] = x;

return true;

}

}

}

return false;

}

int hungary(){

tot = 0;

memset(from,255,sizeof from);

for (int i = 0; i < v; i++){

memset(use,0,sizeof use);

if (match(i))tot++;

}

return tot;

}

int _tmain(int argc, _TCHAR* argv[])

{

for (int i = 0; i < v; i++){

for (int j = 0; j < v; j++){

if (edge[i][j])

g[i].push_back(j);

}

}

for (int i = 0; i < v; i++){

for (int j = 0; j < g[i].size(); j++){

cout << t[i] << ":" << t[g[i][j]] << ends<<ends;

}

cout << endl;

}

cout << "tot=" << hungary() << endl;

for (int i = 0; i < v; i++)

{

if (from[i] == -1){

cout << t[i] << "最早与 " << from[i] << endl;

}

else{

cout << t[i] << "最早与 " << t[from[i]] << endl;

}

//

}

return 0;

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-30 18:38:07

ACM学习-匈牙利匹配的相关文章

ACM学习资料整理

ACM学习资料整理 声明:参考泥瓦匠BYSocket.POJ题目分类推荐 (很好很有层次感)整理所得 1 推荐题库 ?http://ace.delos.com/usaco/ 美国的OI 题库,如果是刚入门的新手,可以尝试先把它刷通,能够学到几乎全部的基础算法极其优化,全部的题解及标程还有题目翻译可以baidu 一个叫NOCOW 的网站.   ?http://livearchive.onlinejudge.org/ 上面有全部的赛区真题,绝大部分都可以提交,不适合当题库刷,不过在这里找题非常方便.

ACM学习-图双连通子图

// ACM学习-割点和桥.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include<queue> #include<vector> #include<algorithm> using namespace std; const int v = 13; int edge[v][v] = { { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0

HDOJ 5352 MZL&#39;s City 匈牙利匹配

求年份和城市的匹配 MZL's City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 539    Accepted Submission(s): 180 Problem Description MZL is an active girl who has her own country. Her big country has N

ACM学习历程—HDU 4726 Kia&#39;s Calculation( 贪心&amp;&amp;计数排序)

DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 12

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

ACM学习参考资料

ACM学习参考资料 各种图论模型及其解答

ACM 学习心得

ACM 学习心得 STL:完美的艺术品 STL 由四大部分组成:算法.容器.迭代器.仿函数. 算法(algorithm) 算法定义了一组与实现无关的操作,也是 ACM 学习的核心.C++ 算法库的内容全都是一些比较基本的算法,包括移动.转换.遍历.删除.过滤等等.C++ 算法库本身是基于抽象的,在迭代器的抽象下,使得这些算法可以在不同结构的容器中重用.一个比较坑的地方就是我高中的时候学完 C++ 之后报名了 NOIP.那一年刚刚允许用 STL(之前一直不准用),然后我对于标准库的依赖很严重,连快

算法学习 - 括号匹配(栈实现)C++

括号匹配是栈最典型的应用了. 其实思路很简单,就是遇到一个左括号就压栈,遇到一个右括号就弹栈,看是否匹配就好了.最后检查下栈里是不是有剩余的括号就行了. 上代码~: // // main.cpp // bracketMatch // // Created by Alps on 14-7-28. // Copyright (c) 2014年 chen. All rights reserved. // #include <iostream> #define ElementType char usi

ACM学习历程—UESTC 1226 Huatuo&#39;s Medicine(数学)(2015CCPC L)

题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目就是构造一个对称的串,除了中间的那个只有1个,其余的两边都是对称的两个,自然答案就是2*n-1. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #