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> # ...
随机推荐
- HDOJ 2037简单的贪心算法
代码: #include<iostream> using namespace std; int main() { int n,s,t1[100],t2[100],i,t,j; while( ...
- [LeetCode OJ] Single Number之二 ——Given an array of integers, every element appears THREE times except for one. Find that single one.
class Solution { public: int singleNumber(int A[], int n) { ; ; ; i<=bits; i++) { ; ; ; j<n; j ...
- .getBoundingClientRect()
.getBoundingClientRect() 该方法获得页面中某个元素的左,上,右和下分别相对浏览器视窗的位置,他返回的是一个对象,即Object,该对象有4个属性:top,left,right, ...
- 分数拆分( Fractions Again, UVA 10976)-ACM
It is easy to see that for every fraction in the form (k > 0), we can always find two positive i ...
- nav
$(document).ready(function() { $(window).resize(function(){ var need=0; var ul_max_width = $(window) ...
- oe 仓库管理
需求情景: 销售电商, 其中有些产品 为代理销售(公司不管理库存,建立SO后直接由对应的供应商发货即可) 解决方案: SO 生成 DO 时候 , 源库存的取得逻辑 SO-->SHO ...
- 七天学会SALTSTACK自动化运维 (2)
七天学会SALTSTACK自动化运维 (2) 导读 Grains Pillar 总结 参考链接 导读 上一篇主要介绍了安装和基本的使用方法,但是我认为如果理解了相关概念的话,使用会更加顺手,因为毕竟每 ...
- getClassInstances --通过类的实例来得到数据
好东西,空了来研究 实例代码如下: for b in getClassInstances bitmaptex doformat "Name : %, Texture name : %, Af ...
- Node.js规范化应用
Node.js运行在一个单线程模式,但它使用一个事件驱动范例来处理并发.它还有助于创建子进程,以充分利用并行处理的多核CPU系统. 子进程总是有三个流child.stdin,child.stdout和 ...
- Atlantis
poj1151:http://poj.org/problem?id=1151 题意:求矩形面积的并题解:扫描线加线段树 #include<iostream> #include<cst ...