poj3487[延迟认可算法]
来源:互联网 发布:男生挎包 知乎 编辑:程序博客网 时间:2024/05/06 00:26
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
23a b c A B Ca:BACb:BACc:ACBA:acbB:bacC:cab3a b c A B Ca:ABCb:ABCc:BCAA:bacB:acbC:abc
Sample Output
a Ab Bc Ca Bb Ac C
Source
======================================================================================================
稳定婚姻系统-延迟认可算法(Gale-Shapley算法)
就我理解这个算法就是在模拟,每次在左集合中取一个点,让它找一个最优的匹配点,然后如果替换了别的点,就把它入队,重复~,这个过程一定是可以终止的(可以证明~囧),而且按照这种方法找到的匹配一定是左侧最优的~
代码写的比较挫~:
program poj3487;const MaxN = 30;var order: array['a'..'z'] of string; weight: array['A'..'Z', 'a'..'z'] of longint; partx: array['a'..'z'] of char; party: array['A'..'Z'] of char; n: longint; que: array[0..30] of char; top, last: longint; procedure Push(x: char); begin last := (last + 1) mod MaxN; que[last] := x; end; function Empty: boolean; begin if top = last then exit(True) else exit(False); end; function Pop: char; begin top := (top + 1) mod MaxN; Pop := que[top]; end; procedure Gale; var x, y: char; i: longint; begin for x := 'a' to 'z' do partx[x] := '#'; for y := 'A' to 'Z' do party[y] := '#'; while not Empty do begin x := Pop; for i := 1 to n do begin y := order[x, i]; if party[y] = '#' then begin party[y] := x; partx[x] := y; break; end; if weight[y, party[y]] > weight[y, x] then begin Push(party[y]); party[y] := x; partx[x] := y; break; end; end; end; end; procedure Scan; var i, j: longint; str: string; begin readln(n); readln; for i := 1 to n + n do begin readln(str); if (str[1] >= 'a') and (str[1] <= 'z') then begin order[str[1]] := Str; Delete(order[str[1]], 1, 2); Push(str[1]); end; if (str[1] >= 'A') and (str[1] <= 'Z') then begin for j := 1 to n do weight[str[1], str[2 + j]] := j; end; end; end; procedure Print; var ch: char; begin for ch := 'a' to 'z' do if partx[ch] <> '#' then writeln(ch, #32, partx[ch]); end; procedure Main; var CaseNo: longint; begin readln(CaseNo); while CaseNo > 0 do begin Dec(CaseNo); Scan; Gale; Print; if CaseNo > 0 then writeln; end; end;begin Assign(input, 'poj3487.in'); reset(input); Assign(output, 'poj3487.out'); rewrite(output); Main; Close(input); Close(output);end.
- poj3487[延迟认可算法]
- 稳定婚姻问题和延迟认可算法
- hdu 1914 The Stable Marriage Problem(延迟认可算法)
- poj 3487 zoj 3120 The Stable Marriage Problem 延迟认可算法(Gale-Shapley算法)
- poj3487(Gale-Shapley算法) The Stable Marriage Problem(稳定婚姻问题)
- poj3487 稳定婚姻问题
- poj3487稳定婚姻匹配
- POJ3487-稳定婚姻系统
- POJ3487 The Stable Marriage Problem
- poj3487-The Stable Marriage Problem
- 认可用量
- 关于认可
- Nagle算法遇上延迟ACK
- Nagle算法与ACK延迟算法
- 【稳定匹配】:poj3487,The Stable Marriage Problem
- TCP/IP延迟确认和Nagle算法
- 【贪心算法,最小延迟调度】:poj***,***
- TCP之Nagle算法&&延迟ACK
- Failed to load the JNI shared library jvm.dll
- 在Java中操纵UDP(转)
- 在eclipse中进行Android ndk开发时c代码中Unresolved inclusion的解决
- 一起去看小学期之Android编程 - 列表控件使用
- 八款开源 Android 游戏引擎 (巨好的资源)
- poj3487[延迟认可算法]
- startActivity的requires new task异常解析
- Windows套接字系列三:使用存档进行套接字编编程
- GestureDetector类的用法
- sp_addlogin 如果恢复的用户ID已经存在,且SID号和已经存在的SID不一样的两种解决办法
- spring2.5 + ibatis2.3.4 整合开发
- 记录点点滴滴
- 多队列网卡简介
- 浅谈Java枚举类型