• HDU1522 稳定婚姻匹配 模板


    Marriage is Stable

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 1047    Accepted Submission(s): 563
    Special Judge


    Problem Description
    Albert, Brad, Chuck are happy bachelors who are in love with Laura, Marcy, Nancy. They all have three choices. But in fact, they do have some preference in mind. Say Albert, he likes Laura best, but that doesn't necesarily mean Laura likes him. Laura likes Chuck more than Albert. So if Albert can't marry Laura, he thinks Nancy a sensible choice. For Albert, he orders the girls Laura > Nancy > Marcy.

    For the boys:

    Albert: Laura > Nancy > Marcy
    Brad: Marcy > Nancy > Laura
    Chuck: Laura > Marcy > Nancy

    For the girls:

    Laura: Chuck > Albert > Brad
    Marcy: Albert > Chuck > Brad
    Nancy: Brad > Albert > Chuck

    But if they were matched randomly, such as

    Albert <-> Laura 
    Brad <-> Marcy
    Chuck <-> Nancy

    they would soon discover it's not a nice solution. For Laura, she likes Chuck instead of Albert. And what's more, Chuck likes Laura better than Nancy. So Laura and Chuck are likely to come together, leaving poor Albert and Nancy.

    Now it's your turn to find a stable marriage. A stable marriage means for any boy G and girl M, with their choice m[G] and m[M], it will not happen that rank(G, M) < rank(G, m[G])and rank(M, G) < rank(M, m[M]).
     
    Input
    Each case starts with an integer n (1 <= n <= 500), the number of matches to make.

    The following n lines contain n + 1 names each, the first being name of the boy, and rest being the rank of the girls.

    The following n lines are the same information for the girls.

    Process to the end of file.
     
    Output
    If there is a stable marriage, print n lines with two names on each line. You can choose any one if there are multiple solution. Print "Impossible" otherwise.

    Print a blank line after each test.
     
    Sample Input
    3
    Albert Laura Nancy Marcy
    Brad Marcy Nancy Laura
    Chuck Laura Marcy Nancy
    Laura Chuck Albert Brad
    Marcy Albert Chuck Brad
    Nancy Brad Albert Chuck
     
    Sample Output
    Albert Nancy
    Brad Marcy
    Chuck Laura
     
    Author
    CHENG, Long
     
    Source
    题意:
    n位男士,n位女士,没人对各个异性有一个排序,代表对他们的喜欢程度。任务是将男生和女生一一配对,使得男生v和女生u不存在以下情况:(1)男生v和女生u不是舞伴;(2)他们喜欢对方的程度都大于喜欢各自当前舞伴的程度。
    输出最稳定配对,好像没有Impossible
    代码:
    //稳定婚姻匹配  详细:白书352页
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<map>
    #include<queue>
    using namespace std;
    const int maxn=505;
    int n;
    int pref[maxn][maxn],fum[maxn],fuw[maxn],ord[maxn][maxn],nex[maxn];
    //pref:编号为i的man第j喜欢的woman的编号;fum:编号为i的woman的未婚夫;
    //ord:编号为i的woman第j喜欢的man;
    map<string,int>vm,vw;
    map<int,string>um,uw;
    queue<int>q;//未订婚的男士队列
    //订婚函数
    void engage(int m,int w)
    {
        int pm=fum[w];
        if(pm){      //woman现有未婚夫
            fuw[pm]=0;//抛弃pm
            q.push(pm);//pm加入未定婚男士队列
        }
        fuw[m]=w;
        fum[w]=m;
    }
    int main()
    {
        char ch[102];
        while(~scanf("%d",&n)){
            vm.clear();vw.clear();um.clear();uw.clear();
            memset(pref,0,sizeof(pref));
            int cnt=0;
            for(int i=1;i<=n;i++){
                scanf("%s",ch);
                vm[ch]=i;um[i]=ch;
                for(int j=1;j<=n;j++){
                    scanf("%s",ch);
                    if(!vw[ch]){
                        vw[ch]=++cnt;
                        uw[cnt]=ch;
                    }
                    pref[i][j]=vw[ch];//编号为i的男士第j喜欢的人
                }
                nex[i]=1;//接下来应向排名为1的女士求婚
                fuw[i]=0;//没有未婚妻
                q.push(i);
            }
            for(int i=1;i<=n;i++){
                scanf("%s",ch);
                int k=vw[ch];
                for(int j=1;j<=n;j++){
                    scanf("%s",ch);
                    ord[k][vm[ch]]=j;//在编号为k的女士心中编号为vm[ch]的男士的排名
                }
                fum[k]=0;//没有未婚夫
            }
            while(!q.empty()){
                int m=q.front();
                q.pop();
                int w=pref[m][nex[m]++];//下一个求婚对象
                if(w==0) continue;
                if(!fum[w])//女士没有未婚夫,直接订婚
                    engage(m,w);
                else if(ord[w][m]<ord[w][fum[w]])//代替女士的现任未婚夫
                    engage(m,w);
                else q.push(m);//直接被拒绝,下次再来
                }
            while(!q.empty()) q.pop();
            for(int i=1;i<=n;i++){
                cout<<um[i]<<" "<<uw[fuw[i]]<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    学习规划
    一位十年的老司机告诉你什么是编程思想
    React开发
    一个简单的ipfs音乐播放器的实现
    React错误总结(三)
    React错误总结解决方案(二)
    mongoid模糊查询
    Rails accepts_nested_attributes_for表单嵌套的利器
    route_path
    "constantize" and "with_indifferent_access" method
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6295669.html
Copyright © 2020-2023  润新知