匈牙利算法趣解 暨 HDU 1068 Girls and Boys

最近做的几道题都是关于一种名叫匈牙利算法的在图上的应用,一时间很费解,连别人的源码也不知道是什么意思。然后心血来潮在网上搜到一篇关于匈牙利算法的趣解,让我稍稍有了一丝理解,至少在原理上知道它是怎么求二分图的最大匹配了。。。在此转载这篇帖子并给出一道题目加深理解,希望能给自己留个印象,如果能够帮助别人就更好了。。。

趣写算法系列之--匈牙利算法

我留下一些内容给自己使用。。。

匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名。匈牙利算法是基于Hall定理中充分性证明的思想,它是部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最大匹配的算法。

-------等等,看得头大?那么请看下面的版本:

通过数代人的努力,你终于赶上了剩男剩女的大潮,假设你是一位光荣的新世纪媒人,在你的手上有N个剩男,M个剩女,每个人都可能对多名异性有好感(惊讶-_-||暂时不考虑特殊的性取向),如果一对男女互有好感,那么你就可以把这一对撮合在一起,现在让我们无视掉所有的单相思(好忧伤的感觉快哭了),你拥有的大概就是下面这样一张关系图,每一条连线都表示互有好感。


本着救人一命,胜造七级浮屠的原则,你想要尽可能地撮合更多的情侣,匈牙利算法的工作模式会教你这样做:

===============================================================================

 先试着给1号男生找妹子,发现第一个和他相连的1号女生还名花无主,got it,连上一条蓝线


===============================================================================

接着给2号男生找妹子,发现第一个和他相连的2号女生名花无主,got it


===============================================================================

接下来是3号男生,很遗憾1号女生已经有主了,怎么办呢?

我们试着给之前1号女生匹配的男生(也就是1号男生)另外分配一个妹子。

(黄色表示这条边被临时拆掉)

与1号男生相连的第二个女生是2号女生,但是2号女生也有主了,怎么办呢?我们再试着给2号女生的原配(发火发火)重新找个妹子(注意这个步骤和上面是一样的,这是一个递归的过程)


此时发现2号男生还能找到3号女生,那么之前的问题迎刃而解了,回溯回去

2号男生可以找3号妹子~~~                  1号男生可以找2号妹子了~~~                3号男生可以找1号妹子

所以第三步最后的结果就是:


===============================================================================

 接下来是4号男生,很遗憾,按照第三步的节奏我们没法给4号男生出来一个妹子,我们实在是无能为力了……香吉士同学走好。

===============================================================================

这就是匈牙利算法的流程,其中找妹子是个递归的过程,最最关键的字就是“ ”字

其原则大概是:有机会上,没机会创造机会也要上

【code】

[cpp]  view plain copy
  1. bool find(int x){  
  2.     int i,j;  
  3.     for (j=1;j<=m;j++){    //扫描每个妹子  
  4.         if (line[x][j]==true && used[j]==false)        
  5.         //如果有暧昧并且还没有标记过(这里标记的意思是这次查找曾试图改变过该妹子的归属问题,但是没有成功,所以就不用瞎费工夫了)  
  6.         {  
  7.             used[j]=1;  
  8.             if (girl[j]==0 || find(girl[j])) {   
  9.                 //名花无主或者能腾出个位置来,这里使用递归  
  10.                 girl[j]=x;  
  11.                 return true;  
  12.             }  
  13.         }  
  14.     }  
  15.     return false;  
  16. }  

在主程序我们这样做:每一步相当于我们上面描述的一二三四中的一步

[cpp]  view plain copy
  1. for (i=1;i<=n;i++)  
  2. {  
  3.     memset(used,0,sizeof(used));    //这个在每一步中清空  
  4.     if find(i) all+=1;  
  5. }  
好了,现在给出一道题目并给出AC代码,希望能靠实际应用加深理解

Girls and Boys

Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 212 Accepted Submission(s): 142
 
Problem Description
the second year of the university somebody started a study on the romantic relations between the students. The relation “romantically involved” is defined between one girl and one boy. For the study reasons it is necessary to find out the maximum set satisfying the condition: there are no two students in the set who have been “romantically involved”. The result of the program is the number of students in such a set.

The input contains several data sets in text format. Each data set represents one set of subjects of the study, with the following description:

the number of students
the description of each student, in the following format
student_identifier:(number_of_romantic_relations) student_identifier1 student_identifier2 student_identifier3 ...
or
student_identifier:(0)

The student_identifier is an integer number between 0 and n-1, for n subjects.
For each given data set, the program should write to standard output a line containing the result.
 
 
Output

            
 
Sample Input
7
0: (3) 4 5 6
1: (2) 4 6
2: (0)
3: (0)
4: (2) 0 1
5: (1) 0
6: (2) 0 1
3
0: (2) 1 2
1: (1) 0
2: (1) 0
 
Sample Output
5
2
 
 
Source
Southeastern Europe 2000
 
Recommend
JGShining
 

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <string.h>
#include <cstring>
#include <stdlib.h>
#include <algorithm>
#include <cstdlib>
#include <math.h>
#include <vector>
#define MAXN 1010
using namespace std;
int match[MAXN];
vector<int> map[MAXN];
int vis[MAXN];
int n;

int find(int x){
    for(int i = 1;i < map[x].size();i++){
        int k = map[x][i];
        if(map[x][i]&&!vis[k]){
            vis[k] = 1;
            if(!match[k]||find(match[k])){
                match[k] = x;
                return 1;
            }
        }
    }
    return 0;
}

int main(){
    int a,b,c;
    int i,j;
    while(~scanf("%d",&n)){
        for(i = 0;i <= n;i++){
            map[i].clear();
            match[i] = 0;
        }
        for(i = 0;i < n;i++){
            scanf("%d: (%d)",&a,&b);
            map[a+1].push_back(0);
            for(j = 0;j < b;j++){
                scanf("%d",&c);
                map[a+1].push_back(c+1);
            }
        }
        int ans = 0;
        for(i = 1;i <= n;i++){
            memset(vis,0,sizeof(vis));
            ans += find(i);
        }
        printf("%d\n",n-ans/2);//之所以除以二是因为此题不是明显的二分图,那么1->2,2->1,除以二是为了去重
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值