POJ 3487 The Stable Marriage Problem(稳定婚姻问题 模版题)
Description
The stable marriage problem consists of matching members of two different sets according to the member’s preferences for the other set’s members. The input for our problem consists of:
- a set M of n males;
- a set F of n females;
- for each male and female we have a list of all the members of the opposite gender in order of preference (from the most preferable to the least).
A marriage is a one-to-one mapping between males and females. A marriage is called stable, if there is no pair (m, f) such that f ∈ F prefers m ∈ M to her current partner and m prefers f over his current partner. The stable marriage A is called male-optimal if there is no other stable marriage B, where any male matches a female he prefers more than the one assigned in A.
Given preferable lists of males and females, you must find the male-optimal stable marriage.
Input
The first line gives you the number of tests. The first line of each test case contains integer n (0 < n < 27). Next line describes n male and n female names. Male name is a lowercase letter, female name is an upper-case letter. Then go n lines, that describe preferable lists for males. Next n lines describe preferable lists for females.
Output
For each test case find and print the pairs of the stable marriage, which is male-optimal. The pairs in each test case must be printed in lexicographical order of their male names as shown in sample output. Output an empty line between test cases.
题目大意:就是稳定婚姻问题,要求男士最优
思路:直接套用Gale-Shapley算法即可
PS:直接用数字不就好了吗非要用字符……
#include <cstdio>
#include <queue>
#include <cstring>
#include <map>
using namespace std; const int MAXN = ; int pref[MAXN][MAXN], order[MAXN][MAXN], next[MAXN];
int future_husband[MAXN], future_wife[MAXN];
queue<int> que;
map<char, int> mp; void engage(int man, int woman){
int &m = future_husband[woman];
if(m){
future_wife[m] = ;
que.push(m);
}
future_husband[woman] = man;
future_wife[man] = woman;
} int n, T; void GaleShapley(){
while(!que.empty()){
int man = que.front(); que.pop();
int woman = pref[man][next[man]++];
if(!future_husband[woman] || order[woman][man] < order[woman][future_husband[woman]])
engage(man, woman);
else que.push(man);
}
for(char c = 'a'; c <= 'z'; ++c) if(mp[c])
printf("%c %c\n", c, future_wife[mp[c]] + 'A' - );
} int main(){
char s[MAXN], c[];
scanf("%d", &T);
while(T--){
if(!que.empty()) que.pop();
mp.clear();
memset(pref,,sizeof(pref));
memset(order,,sizeof(order));
memset(future_husband,,sizeof(future_husband));
memset(future_wife,,sizeof(future_wife));
scanf("%d", &n);
for(int i = ; i <= n; ++i) scanf("%s", c), mp[c[]] = i;
for(int i = ; i <= n; ++i) scanf("%s", c), mp[c[]] = i;
for(int i = ; i < n; ++i){
scanf("%s", s);
for(int j = ; s[j]; ++j) pref[mp[s[]]][j-] = mp[s[j]];
next[mp[s[]]] = ;
que.push(mp[s[]]);
}
for(int i = ; i < n; ++i){
scanf("%s", s);
for(int j = ; s[j]; ++j) order[mp[s[]]][mp[s[j]]] = j-;
}
GaleShapley();
if(T) printf("\n");
}
}
16MS
POJ 3487 The Stable Marriage Problem(稳定婚姻问题 模版题)的更多相关文章
- poj 3478 The Stable Marriage Problem 稳定婚姻问题
题目给出n个男的和n个女的各自喜欢对方的程度,让你输出一个最佳搭配,使得他们全部人的婚姻都是稳定的. 所谓不稳婚姻是说.比方说有两对夫妇M1,F1和M2,F2,M1的老婆是F1,但他更爱F2;而F2的 ...
- 【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 稳定婚姻是组合数学里面的一个问题. 问题大概是这样:有一个社团里 ...
- 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> # ...
随机推荐
- 异常笔记:运行hdfs copyFromLocal 上传文件报错
把本地文件系统,复制到dfs文件系统时报错的错 [hadoop@localhost ~]$ hdfs dfs -copyFromLocal /home/hadoop/mk.txt /xg_test/ ...
- Android中的AutoCompleteTextView(随笔提示文本)组件的简单使用
Android中的随笔提示文本组件AutoCompleteTextView的使用,此组件用于输入文本,然后就会在所配置的适配器中的数据进行查找显示在组件下面. 这里值得注意的是AutoComplete ...
- 【Java】集合遍历--List和Map的多种遍历方式
1. List的两种遍历方式 package com.nova.test; import java.util.ArrayList; import java.util.Iterator; import ...
- python的基本知识
1. python的简介 python的创始⼈人为吉多·范罗苏姆(Guido van Rossum).1989年年的圣诞节期间,吉多· 范罗苏姆为了了在阿姆斯特丹丹打发时间,决⼼心开发⼀个新的脚 ...
- 转载:CSS的组成,三种样式(内联式,嵌入式,外部式),优先级
(仅供自己备份) 原文地址:http://blog.csdn.net/chq11106004389/article/details/50515717 CSS的组成 选择符/选择器+声明(属性+值) 选 ...
- python学习笔记:第21天 常用内置模块之collections和time
目录 一.collections模块 二.时间模块 也可以在我的个人博客上阅读 一.collections模块 1. Counter Counter是⼀个计数器,主要⽤统计字符的数量,之前如果我们要统 ...
- python学习笔记:第13天 内置函数(一)
详细文件查看点击这里:详细地址
- python3 练习题100例 (三)
题目三:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? #!/usr/bin/env python3 # -*- coding: utf-8 -*- &qu ...
- UART学习之路(二)基本时序介绍
这次我们来介绍一下UART的基本时序,了解一下底层信号怎么传送的.方便以后使用Verilog HDL实现收发逻辑. 9600bit/s 的意思是每秒发送9600bit,因此可以理解为将1s分解为960 ...
- Spark入门(Python版)
Hadoop是对大数据集进行分布式计算的标准工具,这也是为什么当你穿过机场时能看到”大数据(Big Data)”广告的原因.它已经成为大数据的操作系统,提供了包括工具和技巧在内的丰富生态系统,允许使用 ...