稳定婚姻匹配问题模板

用于:

给出所有男的和女的 对对方的好感度 

要求不出现:

如果a与b匹配   c与d匹配     但是a相比b来说更加爱c  且c相比d来说更加爱a

主要需要处理的是两个数组

boy[i][j]  第i个男的的第j个喜欢的女的的编号

girl[i][j] 第i个女的 对第j个男的的好感度

#include<bits/stdc++.h>
using namespace std;
#define N 1000

int rank1[N];//归零
int match_boy[N],match_girl[N];//匹配情况
int boy[N][N],girl[N][N];//已说明
 
 
int gs()
{
        memset(match_boy,0,sizeof match_boy);
        memset(match_girl,0,sizeof match_girl);
        memset(rank1,0,sizeof rank1);

        while(flag)
        {
            flag=0;
            for(int i=1;i<=n;i++)
            {
                if(!match_boy[i])
                {
                    int temp=boy[i][rank1[i]++];
                    if(!match_girl[temp])
                      match_boy[i]=temp,match_girl[temp]=i;
                     else if( girl[temp][i]>girl[temp][match_girl[temp]] )
                       {
                           match_boy[ match_girl[temp] ]=0;
                            match_boy[i]=temp;
                            match_girl[temp]=i;
                       }
                       flag=1;
                }
            }
        }
}
原文地址:https://www.cnblogs.com/bxd123/p/10392259.html