• poj 3487 The Stable Marriage Problem


    Description

    The stable marriage problem consists of matching members of two different sets according to the member’s preferences for the other set’s members. The input for our problem consists of:

    • a set M of n males;
    • a set F of n females;
    • for each male and female we have a list of all the members of the opposite gender in order of preference (from the most preferable to the least).

    A marriage is a one-to-one mapping between males and females. A marriage is called stable, if there is no pair (m, f) such that f ∈ F prefers m ∈ M to her current partner and m prefers f over his current partner. The stable marriage A is called male-optimal if there is no other stable marriage B, where any male matches a female he prefers more than the one assigned in A.

    Given preferable lists of males and females, you must find the male-optimal stable marriage.

    Input

    The first line gives you the number of tests. The first line of each test case contains integer n (0 < n < 27). Next line describes n male and n female names. Male name is a lowercase letter, female name is an upper-case letter. Then go n lines, that describe preferable lists for males. Next n lines describe preferable lists for females.

    Output

    For each test case find and print the pairs of the stable marriage, which is male-optimal. The pairs in each test case must be printed in lexicographical order of their male names as shown in sample output. Output an empty line between test cases.

    Sample Input

    2
    3
    a b c A B C
    a:BAC
    b:BAC
    c:ACB
    A:acb
    B:bac
    C:cab
    3
    a b c A B C
    a:ABC
    b:ABC
    c:BCA
    A:bac
    B:acb
    C:abc

    Sample Output

    a A
    b B
    c C
    Gale-Shapley算法模板题
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<queue>
     7 using namespace std;
     8 const int N=30;
     9 queue<int>Q;
    10 int n,T,manname[N],womanname[N],manlike[N][N],womanlike[N][N],manchoice[N],womanchoice[N];
    11 char str[N+5];
    12 int main()
    13 {int i,j;
    14   cin>>T;
    15   while (T--)
    16     {
    17       while (Q.empty()==0) Q.pop();
    18       cin>>n;
    19       for (i=0;i<n;i++)
    20     {
    21       scanf("%s",str);
    22       manname[i]=str[0]-'a';
    23       Q.push(manname[i]);
    24     }
    25       sort(manname+1,manname+n);
    26       for (i=0;i<n;i++)
    27     {
    28       scanf("%s",str);
    29       womanname[i]=str[0]-'A';
    30     }
    31       for (i=0;i<n;i++)
    32     {
    33       scanf("%s",str);
    34       for (j=0;j<n;j++)
    35         manlike[i][j]=str[j+2]-'A';
    36     }
    37       for (i=0;i<n;i++)
    38     {
    39       scanf("%s",str);
    40       for (j=0;j<n;j++)
    41         womanlike[i][str[j+2]-'a']=n-j;
    42       womanlike[i][n]=0;
    43     }
    44       memset(manchoice,0,sizeof(manchoice));
    45       for (i=0;i<n;i++)
    46     womanchoice[i]=n;
    47       while (!Q.empty())
    48     {
    49       int man=Q.front();
    50       int woman=manlike[man][manchoice[man]];
    51       if (womanlike[woman][man]>womanlike[woman][womanchoice[woman]])
    52         {
    53           Q.pop();
    54           if (womanchoice[woman]!=n)
    55         {
    56           Q.push(womanchoice[woman]);
    57           manchoice[womanchoice[woman]]++;
    58         }
    59           womanchoice[woman]=man;
    60         }
    61       else manchoice[man]++;
    62     }
    63       for (i=0;i<n;i++)
    64     printf("%c %c
    ",(char)(manname[i]+'a'),(char)(manlike[manname[i]][manchoice[i]]+'A'));                    
    65     cout<<endl;
    66     }
    67 }
    a B
    b A
    c C

  • 相关阅读:
    Vue的响应式和双向绑定的实现
    JS-跨域请求豆瓣API提供的数据
    豆瓣电影API接口
    JS/PHP-表单文件域上传文件和PHP后台处理
    jQuery-attr,prop及自定义属性
    PHP-关于php代码和html,js混编
    JS-Chrome控制台console.log会访问即时数据
    JS-time和timeEnd
    JS-用ID值直接操作DOM
    CSS-07 行内设置点击事件
  • 原文地址:https://www.cnblogs.com/Y-E-T-I/p/7919871.html
Copyright © 2020-2023  润新知