Marriage is Stable HDU1522 稳定婚姻问题基础
几对男女 给出每个人心中的优先级 进行最合理的匹配
要打印名字的话必须有一个名字数组
英文名用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 稳定婚姻问题基础的更多相关文章
- HDU 1522 Marriage is Stable 【稳定婚姻匹配】(模板题)
<题目链接> 题目大意: 给你N个男生和N个女生,并且给出所有男生和女生对其它所有异性的喜欢程度,喜欢程度越高的两个异性越容易配对,现在求出它们之间的稳定匹配. 解题分析: 稳定婚姻问题的 ...
- HDU1522 稳定婚姻匹配 模板
Marriage is Stable Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)
The Stable Marriage Problem Description The stable marriage problem consists of matching members o ...
- POJ 3487 The Stable Marriage Problem(稳定婚姻问题 模版题)
Description The stable marriage problem consists of matching members of two different sets according ...
- 【转】稳定婚姻问题(Stable Marriage Problem)
转自http://www.cnblogs.com/drizzlecrj/archive/2008/09/12/1290176.html 稳定婚姻是组合数学里面的一个问题. 问题大概是这样:有一个社团里 ...
- 【HDU1914 The Stable Marriage Problem】稳定婚姻问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914 题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序, ...
- 【稳定婚姻问题】【HDU1435】【Stable Match】
2015/7/1 19:48 题意:给一个带权二分图 求稳定匹配 稳定的意义是对于某2个匹配,比如,( a ---- 1) ,(b----2) , 如果 (a,2)<(a,1) 且(2,a)& ...
- hdoj1435 Stable Match(稳定婚姻问题)
简单稳定婚姻问题. 题目描述不够全面,当距离相同时容量大的优先选择. 稳定婚姻问题不存在无解情况. #include<iostream> #include<cstring> # ...
- POJ3487[稳定婚姻]
The Stable Marriage Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2974 Accepted ...
随机推荐
- 解决360WiFi有时候手机连接不上
有可能是无线网卡的问题: 右击“计算机”->选择“管理”->“设备管理器”->网络适配器->选择“Broadcom 802.11n 网络适配器”,或者你实在不知道哪个是无线网 ...
- GROUP BY和 HAVING 及 统计函数 执行顺序等
[我理解:where是对最外层结果进行条件筛选,而having是对分组时分组中的数据进行 组内条件筛选,注意:只能进行筛选,不能进行统计或计算,所有统计或计算都要放在最外层的select 后面,无论是 ...
- DNA序列编码中Hairpin的定义和计算
DNA序列编码中Hairpin的定义和计算 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 [1] 张凯. DNA计算核酸编码优化及算法设计[D]. 2008. [2] Shin, ...
- java.uti.Random类nextInt方法中随机数种子为47的奇怪问题
一,问题描述 需要生成一个[0,1]的随机数.即随机生成 0 或者 1.使用java.util.Random类的 nextInt(int)方法,当构造Random类的对象并提供随机数种子时,发现了一个 ...
- [整理]IIS 6.0 下部署 Asp.net MVC Web Api 后 HTTP PUT and DELETE 请求失败
http://guodong.me/?p=1560 ASP.NET MVC 4 has a new feature called WebAPI which makes it much easier t ...
- css左右等高问题
先看看预览效果:http://lgdy.whut.edu.cn/index.php?c=home&a=detail&id=3394 再来谈谈css左右等高的应用场景:在内容管理系统(c ...
- javascript构造函数模块
var Person = (function(){ var Constr; Constr = function(){ this.name = 'carl'; } Constr.prototype = ...
- struts2 中 Preparable 接口实现数据准备
今天才知道struts还有Preparable接口,实现此接口需要实现其prepare()方法,调用action中其他方法之前会先调用prepare()方法.此接口和方法可以用于初始化一些数据. 测试 ...
- Python-Analysis-Malware
Python恶意软件分析应用-PEfile 0x1.前言 要想对恶意代码快速分析,Python是一门必须要掌握的编程技能.因为它是跨平台的,而且容易阅读和编写.许多开源安全工具也是用Python写的. ...
- CentOS安装SVN客户端(rpm)
http://mirrors.163.com/centos/6/os/x86_64/Packages/ 1.检查是已经安装了svn: rpm -qa subversion subversion-1.7 ...