1 问题描述

何为稳定婚姻问题?

有一个男士的集合Y = {m1,m2,m3…,mn}和一个女士的计划X = {n1,n2,n3,…,nn}。每一个男士有一个排序的列表,把女士按照潜在的优先级进行排序。同样,每一个女士也有一个男士的优先级列表。现在,把男士和女士进行配对,要求尽可能的符合优先级的要求。使得最终的配对结果,男士对女士都可接受,不会出现拒绝的情况,即每对男士和女士都是稳定的。

2 解决方案

上述对于稳定婚姻问题的解释有点牵强,具体可以看一下下面截图:

下面代码所使用的男士和女士集合数据是上图的实例,即男士3人,女士3人。

package com.liuzhen.practice;

import java.util.Scanner;

public class Main {

    public void getResult(int[][] boys, int[][] girls) {
int[] result = new int[boys.length];
int[] used = new int[girls.length]; //最终女士配对的男士
for(int i = 0;i < girls.length;i++) {
used[i] = -1;
result[i] = -1;
}
int count = 0; //统计已完成配对个数
while(count < boys.length) {
for(int i = 0;i < boys.length;i++) {
if(result[i] != -1) //当男士i已完成配对时,进行下一个男士配对
continue;
for(int j = 0;j < boys[0].length;j++) {
if(used[boys[i][j]] == -1) {
used[boys[i][j]] = i; //女士boys[i][j]与男士i配对
result[i] = boys[i][j]; //男士i和女士boys[i][j]配对
break; //男士i完成配对,退出循环
} else {
int temp = 0, temp1 = 0;
for(;temp < girls[0].length;temp++) { //求出男士i在女士boys[i][j]心中的优先级
if(girls[boys[i][j]][temp] == i)
break;
}
for(;temp1 < girls[0].length;temp1++) { //求出当前女士已配对男士在其心中的优先级
if(girls[boys[i][j]][temp1] == used[boys[i][j]])
break;
}
if(temp < temp1) { //当男士i比目前已与女士配对的男士优先级要高时
result[used[boys[i][j]]] = -1; //已配对男士被踢
used[boys[i][j]] = i; //当前女士的配偶换成男士i
result[i] = boys[i][j];
break; //男士i完成配对,退出循环
}
}
}
}
count = 0;
for(int i = 0;i < used.length;i++) {
if(used[i] != -1)
count++;
}
}
//打印出结果
for(int i = 0;i < result.length;i++)
System.out.println("男士"+i+"和女士"+result[i]+"配对");
return;
} public static void main(String[] args) {
Main test = new Main();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[][] boys = new int[n][n]; //男士的心中对象优先级
int[][] girls = new int[n][n]; //女士的心中对象优先级
for(int i = 0;i < n;i++) {
int one = in.nextInt(); //优先级为1
int two = in.nextInt(); //优先级为2
int three = in.nextInt(); //优先级为3
boys[i][0] = one;
boys[i][1] = two;
boys[i][2] = three;
}
for(int i = 0;i < n;i++) {
int one = in.nextInt(); //优先级为1
int two = in.nextInt(); //优先级为2
int three = in.nextInt(); //优先级为3
girls[i][0] = one;
girls[i][1] = two;
girls[i][2] = three;
}
test.getResult(boys, girls);
}
}

运行结果:

1 0 2
2 0
1 0
2 0
0 1
2 0
男士0和女士0配对
男士1和女士2配对
男士2和女士1配对

Java实现稳定婚姻问题的更多相关文章

  1. 算法笔记_138:稳定婚姻问题(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 何为稳定婚姻问题? 有一个男士的集合Y = {m1,m2,m3...,mn}和一个女士的计划X = {n1,n2,n3,...,nn}.每一个男士有 ...

  2. HDU1522 稳定婚姻匹配 模板

    Marriage is Stable Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. 【HDU1914 The Stable Marriage Problem】稳定婚姻问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914 题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序, ...

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

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

  5. BZOJ2140: 稳定婚姻

    题解: 题意就是求二分图的必须边. 我们有结论: 在残量网络上跑tarjan,对于一条边(u,v) 如果该边满流||scc[u]==scc[v],那么该边是可行边. 因为如果scc[u]==scc[v ...

  6. 【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)

    The Stable Marriage Problem   Description The stable marriage problem consists of matching members o ...

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

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

  8. 【稳定婚姻问题】【HDU1435】【Stable Match】

    2015/7/1 19:48 题意:给一个带权二分图  求稳定匹配 稳定的意义是对于某2个匹配,比如,( a ---- 1) ,(b----2) , 如果 (a,2)<(a,1) 且(2,a)& ...

  9. poj 3487 稳定婚姻

    /** 稳定婚姻:男生不停的求婚,女生不停地拒绝 **/ #include <iostream> #include <queue> #include <cstdio> ...

随机推荐

  1. Selenium + Python + Chrome 自动化测试 环境搭建

    一.下载Python 相关的教程很多,此处不详细记录了,下面是官网下载地址: https://www.python.org/downloads/ 我使用的python版本为 Python 3.6.1 ...

  2. TP5 order排序

    order方法属于模型的连贯操作方法之一,用于对操作的结果排序. ->order('sort desc,id desc') 用法如下: Db::table('think_user')->w ...

  3. 学习docker的一点记录

    0x00 docker简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows 机器上,也可以实现虚拟化 ...

  4. 【WEB自动化】【第一节】【Xpath和CSS元素定位】

    目前自动化测试开始投入WEB测试,使用RF及其selenium库,模拟对WEB页面进行操作,此过程中首先面对的问题就是对WEB页面元素的定位,几乎所有的关键字都需要传入特定的WEB页面元素,因此掌握常 ...

  5. 解决 es CircuitBreakingException 问题

    比如频繁报如下错误, [2019-06-16T15:31:22,778][DEBUG][o.e.a.a.c.n.i.TransportNodesInfoAction] [node-xxx] faile ...

  6. django 两种创建模型实例的方法

    1. 添加一个classmethod from django.db import models class Book(models.Model): title = models.CharField(m ...

  7. 【python爬虫】scrapy入门2--自定义item

    items.py class LianhezaobaospyderItem(scrapy.Item): # define the fields for your item here like: # n ...

  8. 【python 爬虫】fake-useragent Maximum amount of retries reached解决方案

    前言 在用fake-useragent的时候发生报错,fake_useragent.errors.FakeUserAgentError: Maximum amount of retries reach ...

  9. web项目——启动时tomcat报错:Server Tomcat v7.0 Server at localhost failed to start.

    报错信息:Server Tomcat v7.0 Server at localhost failed to start. 报错截图: 原因分析:在使用SSM框架时,生成的mapping与系统配置文件不 ...

  10. sqoop-介绍及安装

    1.sqoop概述 sqoop是Apache旗下一款hadoop和关系数据库服务器之间传送数据的工具: 核心的功能: 导入,迁入(从关系型数据库-->hdfs hive hbase) 导出,迁出 ...