1. /**
  2. 题目: Ladies' Choice UVALive - 3989
  3. 链接:https://vjudge.net/problem/UVALive-3989
  4. 题意:稳定婚姻问题
  5. 思路:
  6. gale_shapley算法,参考文档:https://wenku.baidu.com/view/7aa841f2fab069dc502201cb.html
  7.  
  8. */
  9.  
  10. #include <iostream>
  11. #include <cstring>
  12. #include <cstdio>
  13. #include <vector>
  14. #include <cmath>
  15. #include <queue>
  16. using namespace std;
  17. const int MAXN = ;
  18. const int INF = 0x3f3f3f3f;
  19.  
  20. int blove[MAXN][MAXN];///存储女生编号 排在前面的女生编号越喜欢 !!!和glove[][]是不同的。
  21. int glove[MAXN][MAXN];///表示第i个女生对第j个男生的的好感度 数值越大越喜欢
  22. int boy[MAXN];///男生当前选择的女生。如果为-1,表示还没有选择。
  23. int girl[MAXN];///女生当前选择的男生。如果为-1,表示还没有选择。
  24. int boyrank[MAXN];///维护男生最中意的女生编号(排除已经拒绝他的女生之后)初始为1表示最喜欢最前面的那一个。
  25. int N;
  26.  
  27. void gale_shapley()///稳定婚姻问题 男生主动找女生 男生找的是最喜欢的,女生找的是稳定的最差的。交换数据可以反之。
  28. {
  29. memset(girl, -, sizeof girl);
  30. memset(boy, -, sizeof boy);
  31. for(int i = ; i <= N; i++) boyrank[i] = ;
  32. int cnt = ;
  33. while(cnt<N){///当cnt==N表示N个男生都选好了。
  34. cnt = ;
  35. for(int i = ; i <= N; i++){///所有男生向自己最中意的女生表白。
  36. if(boy[i]!=-){
  37. cnt++;
  38. continue;///该男生已经选好了。暂时不用再选。
  39. }
  40.  
  41. int bestgirl = blove[i][boyrank[i]];
  42.  
  43. if(girl[bestgirl]==-){///该女生没有选择别人。
  44. girl[bestgirl] = i;
  45. boy[i] = bestgirl;
  46. }else
  47. {
  48. if(glove[bestgirl][i]>glove[bestgirl][girl[bestgirl]]){
  49. boyrank[girl[bestgirl]]++;///该女生找到更好的,被抛弃的男的要重新选择。更新男生最中意的女生。
  50. boy[girl[bestgirl]] = -;///被抛弃的男生没有选择的女生了。
  51. girl[bestgirl] = i;
  52. boy[i] = bestgirl;
  53. }else///女生坚持自己已经选过的,当前男生被拒绝。
  54. {
  55. boyrank[i]++;
  56. }
  57. }
  58. }
  59. }
  60. }
  61. int main()
  62. {
  63. int T, n;
  64. cin>>T;
  65. while(T--)
  66. {
  67. scanf("%d",&n);
  68. N = n;
  69. int x;
  70. for(int i = ; i <= N; i++){
  71. for(int j = ; j <= N; j++){
  72. scanf("%d",&x);
  73. blove[i][j] = x;
  74. }
  75. }
  76. for(int i = ; i <= N; i++){
  77. for(int j = ; j <= N; j++){
  78. scanf("%d",&x);
  79. glove[i][x] = N-j;
  80. }
  81. }
  82. gale_shapley();
  83. for(int i = ; i <= N; i++){
  84. printf("%d\n",boy[i]);
  85. }
  86. if(T>){
  87. printf("\n");
  88. }
  89. }
  90. return ;
  91. }

模板:

  1. const int MAXN = ;
  2. const int INF = 0x3f3f3f3f;
  3.  
  4. int blove[MAXN][MAXN];///存储女生编号 排在前面的女生编号越喜欢 !!!和glove[][]是不同的。
  5. int glove[MAXN][MAXN];///表示第i个女生对第j个男生的的好感度 数值越大越喜欢
  6. int boy[MAXN];///男生当前选择的女生编号。如果为-1,表示还没有选择。
  7. int girl[MAXN];///女生当前选择的男生。如果为-1,表示还没有选择。
  8. int boyrank[MAXN];///维护男生最中意的女生编号(排除已经拒绝他的女生之后)初始为1表示最喜欢最前面的那一个。
  9. int N;
  10.  
  11. void gale_shapley()///稳定婚姻问题 男生主动找女生 男生找的是最喜欢的,女生找的是稳定的最差的。交换数据可以反之。
  12. {
  13. memset(girl, -, sizeof girl);
  14. memset(boy, -, sizeof boy);
  15. for(int i = ; i <= N; i++) boyrank[i] = ;
  16. int cnt = ;
  17. while(cnt<N){///当cnt==N表示N个男生都选好了。
  18. cnt = ;
  19. for(int i = ; i <= N; i++){///所有男生向自己最中意的女生表白。
  20. if(boy[i]!=-){
  21. cnt++;
  22. continue;///该男生已经选好了。暂时不用再选。
  23. }
  24.  
  25. int bestgirl = blove[i][boyrank[i]];
  26.  
  27. if(girl[bestgirl]==-){///该女生没有选择别人。
  28. girl[bestgirl] = i;
  29. boy[i] = bestgirl;
  30. }else
  31. {
  32. if(glove[bestgirl][i]>glove[bestgirl][girl[bestgirl]]){
  33. boyrank[girl[bestgirl]]++;///该女生找到更好的,被抛弃的男的要重新选择。更新男生最中意的女生。
  34. boy[girl[bestgirl]] = -;///被抛弃的男生没有选择的女生了。
  35. girl[bestgirl] = i;
  36. boy[i] = bestgirl;
  37. }else///女生坚持自己已经选过的,当前男生被拒绝。
  38. {
  39. boyrank[i]++;
  40. }
  41. }
  42. }
  43. }
  44. }

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. 【笔记】选择框 change 事件获取内容

    还记得之前做过一次js 的联动效果,在获取下拉框change事件后的 value 时,当时的我做得比较费劲. 现在看了高程的表单脚本那一章之后发现有一个更好的方法,那就是直接获取下拉框change 事 ...

  2. Mac Finder 显示路径和复制路径

    Mac Finder 显示路径和复制路径 学习了:https://www.jianshu.com/p/757f9ffc5acf 设置 defaults write com.apple.finder _ ...

  3. elastic不错的官方文档(中文)

    https://www.blog-china.cn/template/documentHtml/1484101683485.html http://www.open-open.com/doc/list ...

  4. 图片转为byte[]、String、图片之间的转换

    package com.horizon.action; import java.io.ByteArrayOutputStream; import java.io.File; import java.i ...

  5. 【转】Android之Adapter用法总结

    1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(ListView,GridView)等地方都需要用到Adapter.如下图直 ...

  6. WCF 之 概述

    WCF全称是Windows Communication Foundation,它是.NET3.0的重要组成部分,用来解决Windows下的一些通信方面的问题.WCF是Microsoft平台上的SOA架 ...

  7. java中的Annotation

    java中包含5个基本的Annotation: @Override @Deprecated @SuppressWarnings @SafeVarargs @FunctionalInterface …… ...

  8. Direcshow中视频捕捉和參数设置报告

    Direcshow中视频捕捉和參数设置报告 1.      关于视频捕捉(About Video Capture in Dshow) 1视频捕捉Graph的构建 一个能够捕捉音频或者视频的graph图 ...

  9. iOS活动倒计时的两种实现方式

    代码地址如下:http://www.demodashi.com/demo/11076.html 在做些活动界面或者限时验证码时, 经常会使用一些倒计时突出展现. 现提供两种方案: 一.使用NSTime ...

  10. js对数组按顺序排序

    console.log("------默认排序(ASCII字符排序)------"); ,,,,]; arr.sort(); //ASCII字符代码从小到大排序 console.l ...