UVALive3989 Ladies' Choice —— 稳定婚姻问题 Gale - Shapely算法
题目链接:https://vjudge.net/problem/UVALive-3989


题解:
题意:有n个男生和n个女生。每个女生对男神都有个好感度排行,同时每个男生对每个女生也有一个好感度排行。问:怎样配对,才能使的每个女生尽可能幸福。规定在配对的过程中,如果一对男女不是舞伴,且他们喜欢对方的程度都大于当前的舞伴,那么他么会“私奔”,留下他们的舞伴孤零零。由于是要每个女生尽可能幸福,所以女生根据喜欢程度,主动去男生。而男生只能处于被动的状态。
代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXN = 1e3+; int pref[MAXN][MAXN], order[MAXN][MAXN], Next[MAXN];
int future_husband[MAXN], future_wife[MAXN];
queue<int>q; void engage(int woman, int man)
{
int w = future_wife[man];
if(w)
{
future_husband[w] = ;
q.push(w);
}
future_wife[man] = woman;
future_husband[woman] = man;
} int main()
{
int T, n;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = ; i<=n; i++)
{
for(int j = ; j<=n; j++)
scanf("%d", &pref[i][j]);
Next[i] = ;
future_husband[i] = ;
q.push(i);
} for(int i = ; i<=n; i++)
{
for(int j = ; j<=n; j++)
{
int x;
scanf("%d", &x);
order[i][x] = j;
}
future_wife[i] = ;
} while(!q.empty())
{
int woman = q.front(); q.pop();
int man = pref[woman][Next[woman]++];
if(!future_wife[man])
engage(woman, man);
else if(order[man][woman]<order[man][future_wife[man]])
engage(woman, man);
else q.push(woman);
}
while(!q.empty()) q.pop(); for(int i = ; i<=n; i++)
printf("%d\n", future_husband[i]);
if(T) printf("\n");
}
}
UVALive3989 Ladies' Choice —— 稳定婚姻问题 Gale - Shapely算法的更多相关文章
- UVA 1175 Ladies' Choice 稳定婚姻问题
题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...
- LA 3989 - Ladies' Choice 稳定婚姻问题
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVALive-3989 Ladies' Choice (稳定婚姻问题)
题目大意:稳定婚姻问题.... 题目分析:模板题. 代码如下: # include<iostream> # include<cstdio> # include<queue ...
- 稳定婚姻问题和Gale-Shapley算法(转)
什么是算法?每当有人问作者这样的问题时,他总会引用这个例子:假如你是一个媒人,有若干个单身男子登门求助,还有同样多的单身女子也前来征婚.如果你已经知道这些女孩儿在每个男孩儿心目中的排名,以及男孩儿们在 ...
- UVA 1175 Ladies' Choice 女士的选择(稳定婚姻问题,GS算法)
题意: 给出每个男的心目中的女神排序,给出每个女的心目中的男神排序,即两个n*n的矩阵,一旦任意两个非舞伴的男女同学觉得对方都比现任舞伴要好,他们就会抛弃舞伴而在一起.为了杜绝这种现象,求每个男的最后 ...
- UVALive 3989Ladies' Choice(稳定婚姻问题)
题目链接 题意:n个男生和女生,先是n行n个数,表示每一个女生对男生的好感值排序,然后是n行n列式每一个男生的好感值排序,输出N行,即每个女生在最好情况下的男生的编号 分析:如果是求女生的最好情况下, ...
- Ladies' Choice UVALive - 3989 稳定婚姻问题 gale_shapley算法
/** 题目: Ladies' Choice UVALive - 3989 链接:https://vjudge.net/problem/UVALive-3989 题意:稳定婚姻问题 思路: gale_ ...
- 【稳定婚姻问题】【HDU1435】【Stable Match】
2015/7/1 19:48 题意:给一个带权二分图 求稳定匹配 稳定的意义是对于某2个匹配,比如,( a ---- 1) ,(b----2) , 如果 (a,2)<(a,1) 且(2,a)& ...
- 【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)
Ladies' Choice Teenagers from the local high school have asked you to help them with the organizatio ...
随机推荐
- Python+selenium(操作隐藏元素)
测试过程中,偶尔会碰到一些页面的隐藏元素,如下,是小编举的一个简单的例子: test.html <html> <head></head> <body> ...
- UVALive 2957 Bring Them There
Bring Them There Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. O ...
- zoj 2727 List the Books
List the Books Time Limit: 2 Seconds Memory Limit: 65536 KB Jim is fond of reading books, and h ...
- php 数据库的增删改查
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>&l ...
- websocket个人理解总结
WebSocket 释义:聊天室.服务.套接字.协议 引用:https://www.ibm.com/developerworks/cn/web/1112_huangxa_websocket/index ...
- PatentTips - Wear Leveling for Erasable Memories
BACKGROUND Erasable memories may have erasable elements that can become unreliable after a predeterm ...
- msp430入门编程25
msp430中C语言开发环境搭建 msp430入门学习 msp430入门编程
- Linux--进程组、会话、守护进程(转)
http://www.cnblogs.com/forstudy/archive/2012/04/03/2427683.html 进程组 一个或多个进程的集合 进程组ID: 正整数 两个函数 getpg ...
- 操作redis有关的命令
)连接操作命令 quit:关闭连接(connection) auth:简单密码认证 help cmd: 查看cmd帮助,例如:help quit )持久化 save:将数据同步保存到磁盘 bgsave ...
- 集群FULL GC导致服务不可用
FULL GC会导致stop-the-world,频繁的FULL GC会影响系统的可用性.stackoverflow上有个提问是这么描述这个问题的:当服务器集群批量启动后,执行FULL GC的频率和时 ...