• Marriage is Stable HDU1522 稳定婚姻问题基础


    几对男女   给出每个人心中的优先级   进行最合理的匹配

    要打印名字的话必须有一个名字数组

    英文名用map

    稳定婚姻问题:

    每次循环遍历所有的男的

    每个男的对目前未被拒绝的并且优先级最高的进行预匹配  如果1女的没有伴侣2女的对该男的好感度比女的伴侣的好感度更高   满足任意一个则进行匹配

    不断循环直到所有男的都有伴侣

    #include<cstdio>
    #include<string>
    #include<map>
    #include<iostream>
    #include<cstring>
    using namespace std;
    #define N 20
    
    map<string,int>mp_boy,mp_girl;
    int girl[N][N],boy[N][N];//girl数组存放  第i个女对第j个男的好感度   boy数组存放第i个男的第j优先好感度为谁
    int match_girl[N],match_boy[N];
    string name_boy[N],name_girl[N];
    int rank1[N];
    
    int main()
    {
        mp_boy.clear();mp_girl.clear();
        int n;
        while(scanf("%d",&n)==1)
        {
            string name,str;
            int cnt=0;
            for(int i=1;i<=n;i++)
            {
                cin>>name;
                mp_boy[name]=i;
                name_boy[i]=name;
                for(int j=1;j<=n;j++)
                {
                    cin>>str;
                    int t=mp_girl[str];
                    if(!t)
                        mp_girl[str]=t=++cnt,name_girl[cnt]=str;
                    boy[i][j]=t;
                }
            }
            for(int i=1;i<=n;i++)
            {
                cin>>name;
                for(int j=1;j<=n;j++)
                {
                    cin>>str;
                    int t=mp_boy[str];
                    girl[ mp_girl[name] ][t]=n-j;
                }
            }
            memset(match_boy,0,sizeof match_boy);
            memset(match_girl,0,sizeof match_girl);
            memset(rank1,0,sizeof rank1);
    
            int flag=1;
            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;
                    }
                }
            }
                for(int i=1;i<=n;i++)
                 cout<<name_boy[i]<<" "<<name_girl[ match_boy[i] ]<<endl;
        }
    }
  • 相关阅读:
    sklearn: TfidfVectorizer 中文处理及一些使用参数
    sklearn: TfidfVectorizer 中文处理及一些使用参数
    python在文件中输入整数
    python在文件中输入整数
    Python文件操作,with open as追加文本内容实例
    Python文件操作,with open as追加文本内容实例
    Python中的Bunch模式
    Python中的Bunch模式
    python文本挖掘模版
    python文本挖掘模版
  • 原文地址:https://www.cnblogs.com/bxd123/p/10389724.html
Copyright © 2020-2023  润新知