Problem:给出n个男人心中n个女人的排名,和n个女人心中n个男人的排名,求一种匹配使之形成稳定匹配。(稳定指的是形成的集合中,任意两对之间,不存在某个人和某个人在一起 比集合中的分配更优)
详见维基百科:Stable_marriage_problem
matrix67的分析非常有趣:matrix67
Solution:
function stableMatching {
Initialize all m ∈ M and w ∈ W to free
while ∃ free man m who still has a woman w to propose to {
w = m's highest ranked such woman to whom he has not yet proposed
if w is free
(m, w) become engaged
else some pair (m', w) already exists
if w prefers m to m'
(m, w) become engaged
m' becomes free
else
(m', w) remain engaged
}
}
Think:这个算法中,man做的选择总是越来越差的,woman做的选择总是越来越好的。
Practice:
1.Hiring in Chefland (昨天晚上做比赛遇到的,现在好像还不能提交,目测应该能过)
Code:poj3487.cpp
p.s. woman 和 man 的名字可能相同
Code:zoj1576.cpp