题目给出n个男的和n个女的各自喜欢对方的程度,让你输出一个最佳搭配,使得他们全部人的婚姻都是稳定的。

所谓不稳婚姻是说。比方说有两对夫妇M1,F1和M2,F2,M1的老婆是F1,但他更爱F2;而F2的老公虽说是M2.但她更爱M1,这种婚姻就是不稳婚姻,M1和F2理应结合,他们如今各自的婚姻都是错误的。

整个算法基于。男性轮流向女性求婚,每次求婚对象都是没有拒绝过自己且自己最喜欢的女性。而女性对于每一个求婚者。若她是单身,则接受。否则,就看她更喜欢当前求婚者还是她的未婚夫。选择更好的那个。

这样的运行过程之后。男的都会追到自己最喜欢的女性。而女性都会得到不会太差的男性= =,而且能够证明,这个婚姻是稳定的。。

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=1000+10;
int pref[maxn][maxn],order[maxn][maxn],next[maxn];
int future_husband[maxn],future_wife[maxn];
queue<int> q;
void engage(int man,int woman)
{
int m=future_husband[woman];
if(m)
{
future_wife[m]=0;
q.push(m);
}
future_wife[man]=woman;
future_husband[woman]=man;
}
int id_man[555];
int id_woman[555];
char str[555];
int main()
{
int cas,n;
char A;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
vector<char> v;
for(int i=1;i<=n;i++) {cin>>A;id_man[A]=i;v.push_back(A);}
for(int i=1;i<=n;i++){cin>>A;id_woman[A]=i;}
for(int i=1;i<=n;i++)
{
scanf("%s",str);
int idm=id_man[str[0]];
for(int j=1;j<=n;j++)
{
int idw=id_woman[str[j+1]];
pref[idm][j]=idw;
}
next[idm]=1;
future_wife[idm]=0;
q.push(idm);
}
for(int i=1;i<=n;i++)
{
scanf("%s",str);
int idw=id_woman[str[0]];
for(int j=1;j<=n;j++)
{
int idm=id_man[str[j+1]];
order[idw][idm]=j;
}
future_husband[idw]=0;
} while(!q.empty())
{
int man=q.front();q.pop();
int woman=pref[man][next[man]++];
if(!future_husband[woman]) engage(man,woman);
else if(order[woman][man]<order[woman][future_husband[woman]]) engage(man,woman);
else q.push(man);
}
while(!q.empty()) q.pop();
for(int i=0;i<n;i++)
{
printf("%c %c\n",v[i],future_wife[v[i]-'a'+1]+'A'-1);
}
if(cas) puts("");
}
return 0;
}

谁告白谁占廉价,屌丝男快去告白吧。

poj 3478 The Stable Marriage Problem 稳定婚姻问题的更多相关文章

  1. 【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)

    The Stable Marriage Problem   Description The stable marriage problem consists of matching members o ...

  2. POJ 3487 The Stable Marriage Problem(稳定婚姻问题 模版题)

    Description The stable marriage problem consists of matching members of two different sets according ...

  3. [POJ 3487]The Stable Marriage Problem

    Description The stable marriage problem consists of matching members of two different sets according ...

  4. 【转】稳定婚姻问题(Stable Marriage Problem)

    转自http://www.cnblogs.com/drizzlecrj/archive/2008/09/12/1290176.html 稳定婚姻是组合数学里面的一个问题. 问题大概是这样:有一个社团里 ...

  5. The Stable Marriage Problem

    经典稳定婚姻问题 “稳定婚姻问题(The Stable Marriage Problem)”大致说的就是100个GG和100个MM按照自己的喜欢程度给所有异性打分排序.每个帅哥都凭自己好恶给每个MM打 ...

  6. HDOJ 1914 The Stable Marriage Problem

    rt 稳定婚姻匹配问题 The Stable Marriage Problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 6553 ...

  7. 【HDU1914 The Stable Marriage Problem】稳定婚姻问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914 题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序, ...

  8. 【HDOJ】1914 The Stable Marriage Problem

    稳定婚姻问题,Gale-Shapley算法可解. /* 1914 */ #include <iostream> #include <sstream> #include < ...

  9. hdoj1435 Stable Match(稳定婚姻问题)

    简单稳定婚姻问题. 题目描述不够全面,当距离相同时容量大的优先选择. 稳定婚姻问题不存在无解情况. #include<iostream> #include<cstring> # ...

随机推荐

  1. 关于alarm函数

    #include<unistd.h> #include<signal.h> void handler() { printf("Hello\n"); sign ...

  2. SQL中distinct的用法和left join查询的含义

    SQL中distinct的用法   1.作用于单列 2.作用于多列 3.COUNT统计 4.distinct必须放在开头 5.其他 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出 ...

  3. 关于浮动float属性和position:absolute属性的区别

    最近返回头看了很多书籍,一直在纠结float属性和absolute绝对定位的区别和使用的情况,给大家分享一下自己的心得和体会吧. 1,float属性 float属性意义是让元素拜托独占一行的霸道总裁, ...

  4. import uno 错误

    安装aeroolib 模块后,提示没有 uno 相关段一些模块, 原因是这些模块是 openoffice 中段,需要先安装 openoffice. 1:清除所有 libreoffice 软件,  su ...

  5. Ruby自学笔记(二)— Ruby的一些基础知识

    Ruby安装好之后,我们就可以来实践Ruby语言了. 以下是一些学习到的简单基础知识: 1. 如何执行Ruby文件? 我们编写的Ruby文件是以rb为后缀名的,例如:XXX.rb.当要执行ruby文件 ...

  6. python 单元测试

    http://blog.csdn.net/five3/article/details/7104466

  7. Unity3d Web3d资源的动态加载

    Unity3d Web3d资源的动态加载 @灰太龙 参考了宣雨松的博客,原文出处http://www.xuanyusong.com/archives/2405,如果涉及到侵权,请通知我! Unity3 ...

  8. 转:我终于离开了年薪30w的IT行业

    题目乍看起来有点故意惹人眼球的味道,但是对于我事实就是如此,暂且请君听我细细道来!      先自曝家门,我03年毕业,05年来深,06年买车子,06年底结婚,07年买了房子,09年生了儿子,妻子.房 ...

  9. c# 哈希表集合;函数

    * 哈希表集合 1.先进去的后出来,最后进去的先出来 2.利用枚举类型打印出集合中的Key值和Value值 ** 函数 1.函数:能够独立完成某项功能的模块. 函数四要素:输入.输出.函数体.函数名 ...

  10. 快学Scala第一部分

    转载: 1.变量声明 val answer = 8 * 5 + 2; //常量 var counter = 0;    //变量 //在必要的时候 ,可以指定类型 val greeting:Strin ...