Ladies' Choice UVALive - 3989 稳定婚姻问题 gale_shapley算法
/**
题目: 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算法的更多相关文章
- 训练指南 UVALive - 3989(稳定婚姻问题)
ayout: post title: 训练指南 UVALive - 3989(稳定婚姻问题) author: "luowentaoaa" catalog: true mathjax ...
- 【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)
Ladies' Choice Teenagers from the local high school have asked you to help them with the organizatio ...
- LA 3989 - Ladies' Choice 稳定婚姻问题
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVALive 3989 Ladies' Choice
经典的稳定婚姻匹配问题 UVALive - 3989 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: ...
- UVALive 3989 Ladies' Choice
Ladies' Choice Time Limit: 6000ms Memory Limit: 131072KB This problem will be judged on UVALive. Ori ...
- UVA 1175 Ladies' Choice 稳定婚姻问题
题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...
- UVALive-3989 Ladies' Choice (稳定婚姻问题)
题目大意:稳定婚姻问题.... 题目分析:模板题. 代码如下: # include<iostream> # include<cstdio> # include<queue ...
- 【LA 3989 训练指南】女士的选择 【稳定婚姻问题】
我们先来学一下稳定婚姻问题 什么是稳定婚姻问题? 有n个女士和n个男士,他们要一一进行配对.每个男士心中对这n个女士都有一个排名,同理,每个女士心里对n个男性也有一个排名.我们要做的是,在他们配对完成 ...
- UVA 1175 - Ladies' Choice
1175 - Ladies' Choice 链接 稳定婚姻问题. 代码: #include<bits/stdc++.h> using namespace std; typedef long ...
随机推荐
- iis服务器配置 url rewrite 模块
从网上找了一下,原来微软IIS官方网站给IIS7及以后续版本提供了个URL重写组件. 下载地址:https://www.iis.net/downloads/microsoft/url-rewrite ...
- Ubuntu中网络配置问题
今天,机器做IP变更配置ubuntu网卡的时候出现了: RTNETLINK answers: File exists 网络network service 无法重启 google一下找到 rm etc ...
- 【Web】前台传送JSON格式数据到后台Shell处理
1.js中的json对象和字符串之间的转化:http://www.oschina.net/code/snippet_175925_6288 代码片段: var obj = JSON.parse(des ...
- centos查看哪些包提供指定头文件
[问题]:项目迁移时,原来在suse上正常的代码在centos上报错: g++ -g -Wall -fPIC -I../include -I./ -I../src -I/share/comm_ext ...
- rocketmq的线程服务基类
RocketMQ有很多的线程服务,这些服务都继承自抽象类ServiceThread. 这个抽象类可以单独抽出来用到我们其他的项目中来,仅仅需要修改下日志模块: /** * Licensed to th ...
- Android笔记——UI开发
概述: 布局(Layout)的概念是针对Activity的,Activity就是布满整个Android设备的窗体或者悬浮于其它窗体上的交互界面.在一个应用程序中通常由多个Activity构成.每一个须 ...
- C#注释——爱你不是两三天
说到注释这个东东,我不得不说:爱你不是两三天,每天却想你很多遍...原来梁静茹同学这首歌不全然是情歌啊~ 一句注释也没有的一大片的代码有木有 看着那些无名者写的神秘代码,有没有骂一句,你妹的... ...
- unicode 编码在线转换工具
字符串 unideo的16进制值
- 【Firefly API文档】—— Package DBentrust
http://bbs.gameres.com/thread_219653_1_1.html package dbentrust 该包下面主要是数据库的处理与memcached存储.里面封装了,从mem ...
- Apache服务器配置https协议/SSL证书的方法
转载于:http://www.server110.com/apache/201309/1542.html