poj 3478 The Stable Marriage Problem 稳定婚姻问题
题目给出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 稳定婚姻问题的更多相关文章
- 【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 ...
- [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 稳定婚姻是组合数学里面的一个问题. 问题大概是这样:有一个社团里 ...
- The Stable Marriage Problem
经典稳定婚姻问题 “稳定婚姻问题(The Stable Marriage Problem)”大致说的就是100个GG和100个MM按照自己的喜欢程度给所有异性打分排序.每个帅哥都凭自己好恶给每个MM打 ...
- HDOJ 1914 The Stable Marriage Problem
rt 稳定婚姻匹配问题 The Stable Marriage Problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 6553 ...
- 【HDU1914 The Stable Marriage Problem】稳定婚姻问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914 题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序, ...
- 【HDOJ】1914 The Stable Marriage Problem
稳定婚姻问题,Gale-Shapley算法可解. /* 1914 */ #include <iostream> #include <sstream> #include < ...
- hdoj1435 Stable Match(稳定婚姻问题)
简单稳定婚姻问题. 题目描述不够全面,当距离相同时容量大的优先选择. 稳定婚姻问题不存在无解情况. #include<iostream> #include<cstring> # ...
随机推荐
- 扩展欧几里得算法(extended Euclidean algorithm)的一个常犯错误
int exGcd(int x,int y,int& a,int& b) //ax+by=gcd(x,y) { ; b=; return x; } int res=exGcd(y,x% ...
- busybox下mount nfs的命令
busybox下mount nfs的命令 mount -f nfs -o nolock 10.130.30.2:/nfs/nuc970/rootfs /mnt/test
- Dev gridview 调整字体大小
//调整表头字体大小 this.gridView1.Appearance.HeaderPanel.Font = new Font("Tahoma", 20, FontStyle.R ...
- Invoke()/BeginInvoke()区别
查看MSDN如下: Control..::.Invoke ---> 在拥有此控件的基础窗口句柄的线程上执行委托. Control..::.BeginInvoke ---> ...
- PHP框架_Smarty_实现登录功能
1.项目框架 |--mvc |--data 数据 |--cache 缓存 |--template_c 模板生成目录 |--framework |--function |--function.php 功 ...
- linux安装时提示发生不正常错误问题
跳过md5系统较检(每个系统版本都有一个md5编码唯一) 在安装CentOS时 提示 找不到磁盘,是否安装程序,选择安装程序进行"下一步" 提示: 发生不规则,不正常错误 原因:没 ...
- 谨慎使用php的strtotime()函数
我们在日常业务中,针对业务量,经常会采用对数据库按时间做横向分表,分表后的查询往往会涉及到时间问题.例如,我们想查询某个用户距离当前时间1个月的订单情况,在这个时候,我们有些会用到strtotime( ...
- hibernate spring sturts2配置
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hi ...
- BZOJ 2423 最长公共子序列
Description 字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x0,x1,…,xm-1”,序列Y=“y0, ...
- java 线程基本概念 可见性 同步
开发高性能并发应用不是一件容易的事情.这类应用的例子包括高性能Web服务器.游戏服务器和搜索引擎爬虫等.这样的应用可能需要同时处理成千上万个请求.对于这样的应用,一般采用多线程或事件驱动的架构.对于J ...