10.1.3 The Stable Marriage Problem

The Stable Marriage Problem

Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 32 Accepted Submission(s): 23

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

a B
b A
c C

思路:稳定婚配

详见上一道题的链接~~~

http://www.cnblogs.com/cssystem/p/3335151.html

贴下代码

  1 /*
  2 Author:wuhuajun
  3 */
  4 #include <cmath>
  5 #include <cstdio>
  6 #include <algorithm>
  7 #include <cstring>
  8 #include <string>
  9 #include <cstdlib>
 10 #include <queue>
 11 using namespace std;
 12 
 13 typedef long long ll;
 14 typedef double dd;
 15 const int maxn=30;
 16 int husband[maxn],wife[maxn],pre[maxn][maxn],a[maxn][maxn];
 17 int man,woman,Man[130],Woman[130],next[maxn],n,T,cnt;
 18 char r1[maxn],r2[maxn];
 19 char s[5];
 20 queue<int> q;
 21 
 22 void close()
 23 {
 24 exit(0);
 25 }
 26 
 27 void engage()
 28 {
 29     if (husband[woman]!=0)
 30         q.push(husband[woman]);//原先的男人被踢出去了
 31     husband[woman]=man;
 32     wife[man]=woman;
 33 }
 34 
 35 void work()
 36 {
 37     cnt=0;
 38     while (!q.empty()) q.pop();
 39     for (int i=1;i<=n;i++)
 40         q.push(i);
 41     while (!q.empty() && cnt<=2000000)
 42     {
 43         cnt++;
 44         man=q.front(); q.pop(); //男生选择权
 45         next[man]++;
 46         woman=a[man][next[man]];//当前最好的女生
 47     //    printf("man:%d next:%d woman:%d husband:%d pre1:%d pre2:%d
",man,next[man],woman,husband[woman],pre[woman][man],pre[woman][husband[woman]]);
 48         if (pre[woman][man]>pre[woman][husband[woman]])
 49             engage();
 50         else
 51             q.push(man);
 52     }
 53     for (int i=1;i<=n;i++)
 54         printf("%c %c
",r1[i],r2[wife[i]]);
 55     if (T) printf("
");
 56 }
 57 
 58 void init()
 59 {
 60     while (scanf("%d",&T)!=EOF)
 61     {
 62         while (T--)
 63         {
 64             memset(Man,0,sizeof(Man));
 65             memset(Woman,0,sizeof(Woman));
 66             memset(next,0,sizeof(next));
 67             memset(husband,0,sizeof(husband));
 68             memset(wife,0,sizeof(husband));
 69             scanf("%d",&n);
 70             for (int i=1;i<=n;i++)
 71             {
 72                 scanf("%s",s);
 73                 Man[(int)s[0]]=i;
 74                 r1[i]=s[0];
 75             }
 76             for (int i=1;i<=n;i++)
 77             {
 78                 scanf("%s",s);
 79                 Woman[(int)s[0]]=i;
 80                 r2[i]=s[0];
 81             }
 82             for (int i=1;i<=n;i++)
 83             {
 84                 scanf("%s",s);
 85                 man=Man[(int)s[0]];//这个男生的标号
 86                 for (int j=2;j<=n+1;j++)
 87                 {
 88                     woman=Woman[(int)s[j]];
 89                     //pre[man][woman]=(n-j+1)*10;
 90                     a[man][j-1]=woman;//这个男生最喜欢的女生1->n
 91                 }
 92             }
 93             for (int i=1;i<=n;i++)
 94             {
 95                 scanf("%s",s);
 96                 woman=Woman[(int)s[0]];//这个女生的标号
 97                 for (int j=2;j<=n+1;j++)
 98                 {
 99                     man=Man[(int)s[j]];
100                     pre[woman][man]=(n-j+2)*10;//女生喜欢男生的程度
101                 }
102             }
103             /*
104             for (int i=1;i<=n;i++)
105             {
106                 for (int j=1;j<=n;j++)
107                 {
108                     printf("%d ",pre[i][j]);
109                 }
110                 puts("");
111             }
112             puts("");
113             for (int i=1;i<=n;i++)
114             {
115                 for (int j=1;j<=n;j++)
116                 {
117                     printf("%d ",a[i][j]);
118                 }
119                 puts("");
120             }
121             */
122             //Not Ended
123             work();
124         }
125     }
126 }
127 
128 int main ()
129 {
130     init();
131     close();
132     return 0;
133 }
原文地址:https://www.cnblogs.com/cssystem/p/3335161.html