The Stable Marriage Problem(POJ-3487)
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
題意:
穩定婚姻系統問題如下:
1)集合 M 表示 n 個男性
2)集合 F 表示 n 個女性
3)對于每個人我們都按異性的中意程度給出一份名單(從最中意的到最不中意的)?
如果沒有?,f 對 m 比對她的配偶中意的同時 m 對 f 比對他的配偶更加中意,那么這個婚姻是穩定的。
如果一個穩定配對不存在另一個穩定婚姻配對,其中所有的男性的配偶都比現在強,那么這樣的穩定配對稱為男性最優配對。
給出 n 對男女,以小寫字母表示男性名字,大寫字母表示女性名字,再給出每個男性的中意名單(從大到小),最后是女性的中意名單(從大到小)。
對于每組測試數據輸出一組男性最優配對,男性按字典序從小到大排序,每行為:“男姓名 女性名” 的形式。
思路:裸的延遲認可算法,此題為模版題
Source Program
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<string> #include<cstdlib> #include<queue> #include<set> #include<map> #include<stack> #include<ctime> #include<vector> #define INF 0x3f3f3f3f #define PI acos(-1.0) #define N 10001 #define MOD 123 #define E 1e-6 using namespace std; int couple;//總共多少對 int Male_Like[N][N];//男性對女性的喜歡程度 int Female_Like[N][N];//女性對男性的喜歡程度 int Male_Choice[N];//男性的選擇 int Female_Choice[N];//女性的選擇 int Male_Name[N],Female_Name[N];//姓名的hash queue<int> Free_Male;//沒有配對的男性 char str[N]; int main() {int t;while(scanf("%d",&t)!=EOF){scanf("%d",&couple);while(!Free_Male.empty())//清空隊列Free_Male.pop();for(int i=0;i<couple;i++)//存入男性名字,初始都沒有配對{scanf("%s",str);Male_Name[i]=str[0]-'a';Free_Male.push(Male_Name[i]);}for(int i=0;i<couple;i++)//存入女性名字,初始都沒有配對{scanf("%s",str);Female_Name[i]=str[0]-'A';}sort(Male_Name,Male_Name+couple);//對名字排序for(int i=0;i<couple;i++)//男性對女性的印象,按降序排列{scanf("%s",str);for(int j=0;j<couple;j++)Male_Like[i][j]=str[j+2]-'A';}for(int i=0;i<couple;i++)//女性對男性的印象,添加一虛擬人物,編號為couple,為女性的初始對象{scanf("%s",str);for(int j=0;j<couple;j++)Female_Like[i][str[j+2]-'a']=couple-j;Female_Like[i][couple]=0;}memset(Male_Choice,0,sizeof(Male_Choice));//所有男性初始選擇都是最喜歡的女性for(int i=0;i<couple;i++)//先添加女性的初始對象Female_Choice[i]=couple;while(!Free_Male.empty()){int male=Free_Male.front();//找出一個未配對的男性int female=Male_Like[male][Male_Choice[male]];//男性心儀的女性if(Female_Like[female][male]>Female_Like[female][Female_Choice[female]])//女性對男性的喜愛度大于當前對象{Free_Male.pop();//男性脫單if(Female_Choice[female]!=couple)//如果有前男友且不為虛擬對象couple{Free_Male.push(Female_Choice[female]);//其前男友進入隊列,重新變為光棍Male_Choice[Female_Choice[female]]++;//其前男友考慮其下一對象}Female_Choice[female]=male;//當前男友為當前男性}else//如果被女性拒絕Male_Choice[male]++;//考慮下一對象}for(int i=0;i<couple;i++)printf("%c %c\n",Male_Name[i]+'a',Male_Like[Male_Name[i]][Male_Choice[Male_Name[i]]]+'A');printf("\n");}return 0; }?
總結
以上是生活随笔為你收集整理的The Stable Marriage Problem(POJ-3487)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Haybale Guessing (PO
- 下一篇: 最低通行费(信息学奥赛一本通-T1287