几对男女   给出每个人心中的优先级   进行最合理的匹配

要打印名字的话必须有一个名字数组

英文名用map

稳定婚姻问题:

每次循环遍历所有的男的

每个男的对目前未被拒绝的并且优先级最高的进行预匹配  如果1女的没有伴侣2女的对该男的好感度比女的伴侣的好感度更高   满足任意一个则进行匹配

不断循环直到所有男的都有伴侣

#include<cstdio>
#include<string>
#include<map>
#include<iostream>
#include<cstring>
using namespace std;
#define N 20 map<string,int>mp_boy,mp_girl;
int girl[N][N],boy[N][N];//girl数组存放  第i个女对第j个男的好感度   boy数组存放第i个男的第j优先好感度为谁
int match_girl[N],match_boy[N];
string name_boy[N],name_girl[N];
int rank1[N]; int main()
{
mp_boy.clear();mp_girl.clear();
int n;
while(scanf("%d",&n)==)
{
string name,str;
int cnt=;
for(int i=;i<=n;i++)
{
cin>>name;
mp_boy[name]=i;
name_boy[i]=name;
for(int j=;j<=n;j++)
{
cin>>str;
int t=mp_girl[str];
if(!t)
mp_girl[str]=t=++cnt,name_girl[cnt]=str;
boy[i][j]=t;
}
}
for(int i=;i<=n;i++)
{
cin>>name;
for(int j=;j<=n;j++)
{
cin>>str;
int t=mp_boy[str];
girl[ mp_girl[name] ][t]=n-j;
}
}
memset(match_boy,,sizeof match_boy);
memset(match_girl,,sizeof match_girl);
memset(rank1,,sizeof rank1); int flag=;
while(flag)
{
flag=;
for(int i=;i<=n;i++)
{
if(!match_boy[i])
{
int temp=boy[i][ rank1[i]++ ];
if(!match_girl[temp])
match_boy[i]=temp,match_girl[temp]=i;
else if( girl[temp][i]>girl[temp][ match_girl[temp] ] )
{
match_boy[ match_girl[temp] ]=;
match_boy[i]=temp;
match_girl[temp]=i;
}
flag=;
}
}
}
for(int i=;i<=n;i++)
cout<<name_boy[i]<<" "<<name_girl[ match_boy[i] ]<<endl;
}
}

Marriage is Stable HDU1522 稳定婚姻问题基础的更多相关文章

  1. HDU 1522 Marriage is Stable 【稳定婚姻匹配】(模板题)

    <题目链接> 题目大意: 给你N个男生和N个女生,并且给出所有男生和女生对其它所有异性的喜欢程度,喜欢程度越高的两个异性越容易配对,现在求出它们之间的稳定匹配. 解题分析: 稳定婚姻问题的 ...

  2. HDU1522 稳定婚姻匹配 模板

    Marriage is Stable Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

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

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

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

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

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

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

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

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

  7. 【稳定婚姻问题】【HDU1435】【Stable Match】

    2015/7/1 19:48 题意:给一个带权二分图  求稳定匹配 稳定的意义是对于某2个匹配,比如,( a ---- 1) ,(b----2) , 如果 (a,2)<(a,1) 且(2,a)& ...

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

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

  9. POJ3487[稳定婚姻]

    The Stable Marriage Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2974 Accepted ...

随机推荐

  1. 【BZOJ3139】[HNOI2013]比赛(搜索)

    [BZOJ3139][HNOI2013]比赛(搜索) 题面 BZOJ 洛谷 题解 双倍经验

  2. Python GIL全局解释器锁

    '''在python原始解释器Cpython中存在GIL(Global Interpreter Lock,全局解释器锁),因此在执行Python代码 时,会产生互斥锁来限制线程对共享资源的访问,指导接 ...

  3. apigateway-kong(四)负载均衡理论及实现

    负载均衡(Load balancing)是一种计算机网络技术,用来在多个计算机(计算机集群).网络连接.CPU.磁盘驱动器或其他资源中分配负载,以达到最佳化资源使用.最大化吞吐率.最小化响应时间.同时 ...

  4. ASP.NET MVC验证框架中关于属性标记的通用扩展方法

    http://www.cnblogs.com/wlb/archive/2009/12/01/1614209.html 之前写过一篇文章<ASP.NET MVC中的验证>,唯一的遗憾就是在使 ...

  5. gdb调试2—单步执行和跟踪函数

    int add_range(int low, int high); int main(int argc, char *argv[]) { int result[100]; result[0] = ad ...

  6. OpenStack 计算服务 Nova介绍和控制节点部署(七)

    介绍 Nova是openstack最早的两块模块之一,另一个是对象存储swift.在openstack体系中一个叫做计算节点,一个叫做控制节点.这个主要和nova相关,我们把安装为计算节点nova-c ...

  7. oracle之回滚数据表 笔记

    alter table [tableName] enable row movement;flashback table [tableName] to timestamp to_timestamp('2 ...

  8. CSS变量试玩儿

    CSS很美妙,能够为您的页面穿上衣裳,各种各样五彩斑斓的衣裳,但是对于开发者来说,他又不够灵动,于是乎有了各种各样的预处理器Sass.LESS.Stylus(笔者建议Sass的SCSS语法),这些预处 ...

  9. python学习笔记7-网络编程

    import urllib.request import json,requests #urlib模块,不常用 url = 'http://api.nnzhp.cn/api/user/stu_info ...

  10. Selenium学习(Python)

    #从Selenium中导入Webdriver类,该类中定义了selenium支持的浏览器 # webdriver.Firefox # webdriver.FirefoxProfile # webdri ...