/**
题目: Ladies' Choice UVALive - 3989
链接:https://vjudge.net/problem/UVALive-3989
题意:稳定婚姻问题
思路:
gale_shapley算法,参考文档:https://wenku.baidu.com/view/7aa841f2fab069dc502201cb.html */ #include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;
const int MAXN = ;
const int INF = 0x3f3f3f3f; int blove[MAXN][MAXN];///存储女生编号 排在前面的女生编号越喜欢 !!!和glove[][]是不同的。
int glove[MAXN][MAXN];///表示第i个女生对第j个男生的的好感度 数值越大越喜欢
int boy[MAXN];///男生当前选择的女生。如果为-1,表示还没有选择。
int girl[MAXN];///女生当前选择的男生。如果为-1,表示还没有选择。
int boyrank[MAXN];///维护男生最中意的女生编号(排除已经拒绝他的女生之后)初始为1表示最喜欢最前面的那一个。
int N; void gale_shapley()///稳定婚姻问题 男生主动找女生 男生找的是最喜欢的,女生找的是稳定的最差的。交换数据可以反之。
{
memset(girl, -, sizeof girl);
memset(boy, -, sizeof boy);
for(int i = ; i <= N; i++) boyrank[i] = ;
int cnt = ;
while(cnt<N){///当cnt==N表示N个男生都选好了。
cnt = ;
for(int i = ; i <= N; i++){///所有男生向自己最中意的女生表白。
if(boy[i]!=-){
cnt++;
continue;///该男生已经选好了。暂时不用再选。
} int bestgirl = blove[i][boyrank[i]]; if(girl[bestgirl]==-){///该女生没有选择别人。
girl[bestgirl] = i;
boy[i] = bestgirl;
}else
{
if(glove[bestgirl][i]>glove[bestgirl][girl[bestgirl]]){
boyrank[girl[bestgirl]]++;///该女生找到更好的,被抛弃的男的要重新选择。更新男生最中意的女生。
boy[girl[bestgirl]] = -;///被抛弃的男生没有选择的女生了。
girl[bestgirl] = i;
boy[i] = bestgirl;
}else///女生坚持自己已经选过的,当前男生被拒绝。
{
boyrank[i]++;
}
}
}
}
}
int main()
{
int T, n;
cin>>T;
while(T--)
{
scanf("%d",&n);
N = n;
int x;
for(int i = ; i <= N; i++){
for(int j = ; j <= N; j++){
scanf("%d",&x);
blove[i][j] = x;
}
}
for(int i = ; i <= N; i++){
for(int j = ; j <= N; j++){
scanf("%d",&x);
glove[i][x] = N-j;
}
}
gale_shapley();
for(int i = ; i <= N; i++){
printf("%d\n",boy[i]);
}
if(T>){
printf("\n");
}
}
return ;
}

模板:

const int MAXN = ;
const int INF = 0x3f3f3f3f; int blove[MAXN][MAXN];///存储女生编号 排在前面的女生编号越喜欢 !!!和glove[][]是不同的。
int glove[MAXN][MAXN];///表示第i个女生对第j个男生的的好感度 数值越大越喜欢
int boy[MAXN];///男生当前选择的女生编号。如果为-1,表示还没有选择。
int girl[MAXN];///女生当前选择的男生。如果为-1,表示还没有选择。
int boyrank[MAXN];///维护男生最中意的女生编号(排除已经拒绝他的女生之后)初始为1表示最喜欢最前面的那一个。
int N; void gale_shapley()///稳定婚姻问题 男生主动找女生 男生找的是最喜欢的,女生找的是稳定的最差的。交换数据可以反之。
{
memset(girl, -, sizeof girl);
memset(boy, -, sizeof boy);
for(int i = ; i <= N; i++) boyrank[i] = ;
int cnt = ;
while(cnt<N){///当cnt==N表示N个男生都选好了。
cnt = ;
for(int i = ; i <= N; i++){///所有男生向自己最中意的女生表白。
if(boy[i]!=-){
cnt++;
continue;///该男生已经选好了。暂时不用再选。
} int bestgirl = blove[i][boyrank[i]]; if(girl[bestgirl]==-){///该女生没有选择别人。
girl[bestgirl] = i;
boy[i] = bestgirl;
}else
{
if(glove[bestgirl][i]>glove[bestgirl][girl[bestgirl]]){
boyrank[girl[bestgirl]]++;///该女生找到更好的,被抛弃的男的要重新选择。更新男生最中意的女生。
boy[girl[bestgirl]] = -;///被抛弃的男生没有选择的女生了。
girl[bestgirl] = i;
boy[i] = bestgirl;
}else///女生坚持自己已经选过的,当前男生被拒绝。
{
boyrank[i]++;
}
}
}
}
}

Ladies' Choice UVALive - 3989 稳定婚姻问题 gale_shapley算法的更多相关文章

  1. 训练指南 UVALive - 3989(稳定婚姻问题)

    ayout: post title: 训练指南 UVALive - 3989(稳定婚姻问题) author: "luowentaoaa" catalog: true mathjax ...

  2. 【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)

    Ladies' Choice Teenagers from the local high school have asked you to help them with the organizatio ...

  3. LA 3989 - Ladies' Choice 稳定婚姻问题

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  4. UVALive 3989 Ladies&#39; Choice

    经典的稳定婚姻匹配问题 UVALive - 3989 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format:  ...

  5. UVALive 3989 Ladies' Choice

    Ladies' Choice Time Limit: 6000ms Memory Limit: 131072KB This problem will be judged on UVALive. Ori ...

  6. UVA 1175 Ladies' Choice 稳定婚姻问题

    题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...

  7. UVALive-3989 Ladies' Choice (稳定婚姻问题)

    题目大意:稳定婚姻问题.... 题目分析:模板题. 代码如下: # include<iostream> # include<cstdio> # include<queue ...

  8. 【LA 3989 训练指南】女士的选择 【稳定婚姻问题】

    我们先来学一下稳定婚姻问题 什么是稳定婚姻问题? 有n个女士和n个男士,他们要一一进行配对.每个男士心中对这n个女士都有一个排名,同理,每个女士心里对n个男性也有一个排名.我们要做的是,在他们配对完成 ...

  9. UVA 1175 - Ladies' Choice

    1175 - Ladies' Choice 链接 稳定婚姻问题. 代码: #include<bits/stdc++.h> using namespace std; typedef long ...

随机推荐

  1. 关于windows下自带的forfile批量删除文件bat命令

    最近在开发的过程中,为了节省资源,需要用到windows下批量删除文件的批处理命令,也就是bat 主要内容: forfiles /p "E:\pictures" /m * /d - ...

  2. An incompatible version 1.1.14 of APR based Apache Tomcat Native library is installed, while Tomcat

    启动tomcat 7.0, 看到日志里出现严重警告, An incompatible version 1.1.14 of APR based Apache Tomcat Native library ...

  3. 在表单里面检查用户名是否存javascript

    function CheckUser(fn) { $.get("/Pages/Handler/CheckExistHander.ashx", { "txt_UserNo& ...

  4. ActiveRecord::StatementInvalid (Mysql2::Error: Incorrect string value:

    今天碰到一个相当棘手的问题,那就是ActiveRecord::StatementInvalid (Mysql2::Error: Incorrect string value . 本来在本地测试是没有任 ...

  5. 我为什么选择ANGULAR 2?

    没有选择是痛苦的,有太多的选择却更加痛苦.而后者正是目前前端领域的真实写照.新的框架层出不穷: 它难吗?它写得快吗?可维护性怎样?运行性能如何?社区如何?前景怎样?好就业吗?好招人吗?组建团队容易吗? ...

  6. C#秘密武器之多线程——基础

    多线程概述 什么是进程? 当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源.而一个进程又是由多个线程所组成的. 什么是线程? 线程是程序中的一个执行流,每个线程 ...

  7. ocx注册

    (1)服务器OCX注册 (2)IE浏览器,站点加入可信任站点. internet 选项->安全->可信任站点.把“对该区域中的所有站点要求服务器验证(https:)” 前面的勾去掉 (3) ...

  8. iOS 使用NSUserdefault 保存自定义的 对象

    一:NSUserDefaults支持的数据格式有:NSNumber(Integer.Float.Double),NSString,NSData,NSArray,NSDictionary,BOOL类型: ...

  9. Android 内存泄漏分析利器——leakcanary

    LeakCanary Android 和 Java 内存泄露检测. “A small leak will sink a great ship.” - Benjamin Franklin 千里之堤, 毁 ...

  10. malloc、calloc、realloc的区别(转)

    (1)C语言跟内存分配方式 <1>从静态存储区域分配.       内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量.static变量.<2> ...