HDU 1522 Marriage is Stable 稳定婚姻匹配

Marriage is Stable

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 60 Accepted Submission(s): 40
 
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

题目大意:

有 N  个男生和 N 个女生,已知每个男生对每个女生的喜欢程度,和每个女生对每个男生的喜欢程度。

         找到一种搭配方式使得总的满意程度最高。

思路:


这是一篇非常好的文章,看完这个问题就懂了。


戳这里


这个题没什么难度,就是输入很烦。


AC代码:


#include<stdio.h>
#include<string.h>
#include<string>
#include<map>
#include<iostream>
#include<algorithm>
#define maxn 555
#define clr(x)memset(x,0,sizeof(x))
using namespace std;
int tot;
int n;
int b[maxn][maxn];///男生 i 第 j  个喜欢的女生是几号  
int g[maxn][maxn];///女生 i 对男生  j  的排名
int by[maxn],gl[maxn];  ///匹配情况
int r[maxn];
char s[100];
string bo[maxn],gi[maxn];///记录第 i  个男生或者女生的名字
int main()
{
    int bn,gn,t,flag,i,j,f;
    while(scanf("%d",&n)!=EOF)
    {
        f=0;
        getchar();
        gn=1;
        clr(b);
        clr(g);
        for(i=1;i<=n;i++)
            r[i]=1;
        map<string,int>boy,girl;
          boy.clear(),girl.clear();
        tot=1;
        for(i=1;i<=n;i++)
        {
            scanf("%s",s);
            boy[s]=i;
            bo[i]=s;
            for(j=1;j<=n;j++)
            {
                scanf("%s",s);
                if(!girl[s])
                {
                    girl[s]=j;
                    gi[j]=s;
                    b[i][j]=j;
                }
                else b[i][j]=girl[s];
            }
        }
        for(i=1;i<=n;i++)
        {
            scanf("%s",s);
            t=girl[s];
            for(j=1;j<=n;j++)
            {
                scanf("%s",s);
                g[t][boy[s]]=j;
            }
        }
        clr(by);  clr(gl);
        while(1)
        {
            flag=1;
            for(i=1;i<=n;i++)
                if(!by[i])
                {
                    t=b[i][r[i]++];///t 是 他喜欢的女生且他还没有匹配过
                    if(!gl[t])///如果这个女生刚好没对象,就结成一对
                    {
                        by[i]=t;
                        gl[t]=i;
                    }
                    else if(g[t][gl[t]]>g[t][i])///如果她有对象了,但是她更喜欢当前这个男生,则女生抛弃之前的男生和这个结成一对
                    {
                        by[gl[t]]=0;
                        gl[t]=i;
                        by[i]=t;
                    }
                    flag=0;
                }
            if(flag)
                break;
        }
        for(i=1;i<=n;i++)
            cout<<bo[i]<<' '<<gi[by[i]]<<endl;
        printf("\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值