【HDU1914 The Stable Marriage Problem】稳定婚姻问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914
题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序,同时每位男生也按照自己的偏爱程度将女生排序。然后将这n个女生和n个男生配成完备婚姻。
如果存在两位女生A和B,两位男生a和b,使得A和a结婚,B和b结婚,但是A更偏爱b而不是a,b更偏爱A而不是B,则这个婚姻就是不稳定的,A和b可能背着别人相伴而走,因为他俩都认为,与当前配偶比起来他们更偏爱各自的新伴侣。
如果完备婚姻不是不稳定的,则称其是稳定的。通过证明,可以得到每一个n女n男的社团,都存在稳定婚姻的结论。但是这种情况只在异性的社团中存在。也就是说在同性的社团里面,稳定婚姻的存在性将不再被保证。
思路:先把所有男士加入队列当中,对于第一个出队列的男士从他喜爱度最高的女士开始求婚,如果找到一个女士还没有结婚,则和她匹配,如果找到一个女士,该女士对他的满意度高于这个女士的未婚夫,则该女士抛弃未婚夫和他进行匹配,她的未婚夫则进队列。已经匹配过的要进行标记,下次不能再匹配了。
因为每个男士最多和一个女士匹配一次。时间复杂度接近于O(n^2)。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include <queue>
#include <map>
using namespace std; const int maxn=;
int g[maxn][maxn], b[maxn][maxn], visit[maxn][maxn];
int bf[maxn], gf[maxn];
char ch[maxn], str[maxn];
map<char,int>G,M;
map<int,char>GG,MM;
queue<int>q;
int n, T; void init()
{
G.clear(), M.clear(), GG.clear(), MM.clear();
memset(visit,,sizeof(visit));
memset(bf,,sizeof(bf));
while(!q.empty()) q.pop();
} void find(int x)
{
for(int i=n; i>=; i--)
{
if(visit[x][i]) continue;
visit[x][i]=;
int y=b[x][i];
if(!bf[y])
{
bf[y]=x;
gf[x]=y;
return ;
}
else
{
if(g[y][x]>g[y][ bf[y] ])
{
q.push(bf[y]);
bf[y]=x;
gf[x]=y;
return ;
}
}
}
} void Solve()
{
for(int i=; i<=n; i++) q.push(i);
while(!q.empty())
{
int x=q.front();
q.pop();
find(x);
}
sort(ch+,ch+n+);
for(int i=; i<=n; i++)
printf("%c %c\n",ch[i],MM[ gf[ G[ch[i]] ] ]);
} int main()
{
cin >> T;
while(T--)
{
cin >> n;
init();
for(int i=; i<=n; i++) cin >> ch[i], G[ ch[i] ]=i, GG[i]=ch[i];
for(int i=; i<=n; i++) cin >> ch[n+i], M[ ch[n+i] ]=i, MM[i]=ch[n+i];
for(int i=; i<=n; i++)
{
scanf("%s",str+);
int x=G[ str[] ];
for(int j=; j<=n+; j++)
{
int y=M[ str[j] ];
b[x][n-j+]=y;
}
}
for(int i=; i<=n; i++)
{
scanf("%s",str+);
int x=M[ str[] ];
for(int j=; j<=n+; j++)
{
int y=G[ str[j] ];
g[x][y]=n-j+;
}
}
Solve();
if(T)puts("");
}
}
【HDU1914 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 3478 The Stable Marriage Problem 稳定婚姻问题
题目给出n个男的和n个女的各自喜欢对方的程度,让你输出一个最佳搭配,使得他们全部人的婚姻都是稳定的. 所谓不稳婚姻是说.比方说有两对夫妇M1,F1和M2,F2,M1的老婆是F1,但他更爱F2;而F2的 ...
- 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 ...
- [POJ 3487]The Stable Marriage Problem
Description The stable marriage problem consists of matching members of two different sets according ...
- 【HDOJ】1914 The Stable Marriage Problem
稳定婚姻问题,Gale-Shapley算法可解. /* 1914 */ #include <iostream> #include <sstream> #include < ...
- hdoj1435 Stable Match(稳定婚姻问题)
简单稳定婚姻问题. 题目描述不够全面,当距离相同时容量大的优先选择. 稳定婚姻问题不存在无解情况. #include<iostream> #include<cstring> # ...
随机推荐
- Java反射详解及应用示例
反射是Java中最重要的内容之一,了解反射原理对我们学习各种框架具有很大的帮助 反射的原理: 反射应用示例: import java.lang.reflect.Constructor; import ...
- 廖雪峰js教程笔记7 基本类型和包装类型
在JavaScript的世界里,一切都是对象. 但是某些对象还是和其他对象不太一样.为了区分对象的类型,我们用typeof操作符获取对象的类型,它总是返回一个字符串: typeof 123; // ' ...
- Android学习系列(41)--Android Studio简单使用
1. 环境 UBUNTU 14.04 + Android Studio 0.8.2 2. 安装jdk openjdk-7是一个很好的选择: sudo apt-get update sudo apt-g ...
- Swift3.0语言教程获取字符串长度
Swift3.0语言教程获取字符串长度 Swift3.0语言教程获取字符串长度,当在一个字符串中存在很多的字符时,如果想要计算字符串的长度时相当麻烦的一件事情,在NSString中可以使用length ...
- vs 颜色设置
工具-选项-字体和颜色:在项背景点击自定义-色调85 饱和度123 亮度205, 字体则是选择Calibri,个人认为看起来非常舒服.前景字体我选择了偏紫色,会很搭配背景浅绿色以及不会和关键字颜色搞混 ...
- 读取Excel数据到Table表中
方法一: try { List<DBUtility.CommandInfo> list = new List<DBUtility.CommandInfo>(); string ...
- Dijkstra+计算几何 POJ 2502 Subway
题目传送门 题意:列车上行驶40, 其余走路速度10.问从家到学校的最短时间 分析:关键是建图:相邻站点的速度是40,否则都可以走路10的速度.读入数据也很变态. #include <cstdi ...
- Redis Java API
package cn.ac.iscas.pebble.dc.redispool; import java.io.File; import java.io.FileOutputStream; impor ...
- 编写unit test以及自动化测试WebDriver
http://msdn.microsoft.com/en-us/library/hh694602.aspx#BKMK_Quick_starts http://www.seleniumhq.org/ ...
- Code[VS]1021 玛丽卡题解
Code[VS]1021 玛丽卡题解 SPFA Algorithm 题目传送门:http://codevs.cn/problem/1021/ 题目描述 Description 麦克找了个新女朋友,玛丽 ...