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 ...
随机推荐
- 【笔记】选择框 change 事件获取内容
还记得之前做过一次js 的联动效果,在获取下拉框change事件后的 value 时,当时的我做得比较费劲. 现在看了高程的表单脚本那一章之后发现有一个更好的方法,那就是直接获取下拉框change 事 ...
- Mac Finder 显示路径和复制路径
Mac Finder 显示路径和复制路径 学习了:https://www.jianshu.com/p/757f9ffc5acf 设置 defaults write com.apple.finder _ ...
- elastic不错的官方文档(中文)
https://www.blog-china.cn/template/documentHtml/1484101683485.html http://www.open-open.com/doc/list ...
- 图片转为byte[]、String、图片之间的转换
package com.horizon.action; import java.io.ByteArrayOutputStream; import java.io.File; import java.i ...
- 【转】Android之Adapter用法总结
1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(ListView,GridView)等地方都需要用到Adapter.如下图直 ...
- WCF 之 概述
WCF全称是Windows Communication Foundation,它是.NET3.0的重要组成部分,用来解决Windows下的一些通信方面的问题.WCF是Microsoft平台上的SOA架 ...
- java中的Annotation
java中包含5个基本的Annotation: @Override @Deprecated @SuppressWarnings @SafeVarargs @FunctionalInterface …… ...
- Direcshow中视频捕捉和參数设置报告
Direcshow中视频捕捉和參数设置报告 1. 关于视频捕捉(About Video Capture in Dshow) 1视频捕捉Graph的构建 一个能够捕捉音频或者视频的graph图 ...
- iOS活动倒计时的两种实现方式
代码地址如下:http://www.demodashi.com/demo/11076.html 在做些活动界面或者限时验证码时, 经常会使用一些倒计时突出展现. 现提供两种方案: 一.使用NSTime ...
- js对数组按顺序排序
console.log("------默认排序(ASCII字符排序)------"); ,,,,]; arr.sort(); //ASCII字符代码从小到大排序 console.l ...