HDU1914(稳定婚姻)
http://acm.hdu.edu.cn/showproblem.php?pid=1914 思路:Gale-Shapley算法。算法过程是男士不停地求婚,女士不停地拒绝。在每一轮中,每个尚未订婚的男士在他还没有求过婚的女士中选一个自己最喜欢的求婚(不管她有没有订婚)。然后每个女士在向她求婚的人之中选择她最喜欢的一个订婚,并且拒绝其他人。注意,这些向她求婚的人中包含她的未婚夫,因此她可以选择另一个自己更喜欢的人订婚,而抛弃自己的现任未婚夫。这个算法的结果是使男士都能娶到自己有可能娶到的最好的妻子,所以是对男士的理想配对。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
using namespace std;
struct male
{
int f,rev[],tag;
} m[];
struct female
{
int tag,temp,val,wait[];
} f[];
int _,n,t,k,mf[][],fm[][];
char ch[];
bool ok()
{
int i;
for (i=;i<=;i++)
if (m[i].f==&&m[i].tag>) return true;
return false;
}
int main()
{
scanf("%d",&_);
while (_--)
{
scanf("%d",&n);
int i,j;
for (i=;i<=;i++)
{
f[i].tag=;
m[i].tag=;
}
for (i=;i<=n;i++)
{
scanf("%s",ch);
t=ch[]-'a'+;
m[t].f=;
m[t].tag=t;
memset(m[t].rev,,sizeof(m[t].rev));
}
for (i=;i<=n;i++)
{
scanf("%s",ch);
t=ch[]-'A'+;
f[t].tag=t;
f[t].temp=;
f[t].val=;
memset(f[t].wait,,sizeof(f[i].wait));
}
for (i=;i<=n;i++)
{
scanf("%s",ch);
t=ch[]-'a'+;
for (j=;j<=n+;j++)
mf[t][j-]=ch[j]-'A'+;
}
for (i=;i<=n;i++)
{
//cout<<1;
scanf("%s",ch);
t=ch[]-'A'+;
for (j=;j<=n+;j++)
fm[t][j-]=ch[j]-'a'+;
}
//cout<<"hhhhhhh"<<endl;
while (ok())
{
for (i=;i<=;i++)
{
if (m[i].f==&&m[i].tag>)
{
for (j=;j<=n;j++)
{
t=mf[i][j];
if (m[i].rev[t]==)
{
m[i].rev[t]=;
m[i].f=;
k=++f[t].wait[];
f[t].wait[k]=i;
break;
}
}
}
}
for (i=;i<=;i++)
{
if (f[i].tag>)
{
for (j=;j<=f[i].wait[];j++)
{
t=f[i].wait[j];
for (k=;k<=n;k++)
if (fm[i][k]==t) break;
if (f[i].val>k)
{
m[f[i].temp].f=;
f[i].temp=t;
f[i].val=k;
}
else m[t].f=;
}
f[i].wait[]=;
}
}
}
int out[];
memset(out,,sizeof(out));
for (i=;i<=;i++)
if (f[i].tag>)
{
j=f[i].temp;
out[j]=i;
}
for (i=;i<=;i++)
if (out[i]) printf("%c %c\n",i-+'a',out[i]-+'A');
if (_) printf("\n");
}
return ;
}
HDU1914(稳定婚姻)的更多相关文章
- hdu1914 稳定婚姻问题
		稳定婚姻问题就是给你n个男的,n个女的,然后给你每个男生中女生的排名,和女生心目中男生的排名,然后让你匹配成n对,使婚姻稳定,假如a和b匹配,c和d匹配,如果a认为d比b好,同时 ... 
- HDU1914 稳定婚姻匹配
		The Stable Marriage Problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (J ... 
- 【HDU1914 The Stable Marriage Problem】稳定婚姻问题
		题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914 题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序, ... 
- UVA 1175 Ladies' Choice 稳定婚姻问题
		题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ... 
- BZOJ2140: 稳定婚姻
		题解: 题意就是求二分图的必须边. 我们有结论: 在残量网络上跑tarjan,对于一条边(u,v) 如果该边满流||scc[u]==scc[v],那么该边是可行边. 因为如果scc[u]==scc[v ... 
- 【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)
		The Stable Marriage Problem Description The stable marriage problem consists of matching members o ... 
- 【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)
		Ladies' Choice Teenagers from the local high school have asked you to help them with the organizatio ... 
- 【稳定婚姻问题】【HDU1435】【Stable Match】
		2015/7/1 19:48 题意:给一个带权二分图 求稳定匹配 稳定的意义是对于某2个匹配,比如,( a ---- 1) ,(b----2) , 如果 (a,2)<(a,1) 且(2,a)& ... 
- poj 3487 稳定婚姻
		/** 稳定婚姻:男生不停的求婚,女生不停地拒绝 **/ #include <iostream> #include <queue> #include <cstdio> ... 
- 稳定婚姻问题和Gale-Shapley算法(转)
		什么是算法?每当有人问作者这样的问题时,他总会引用这个例子:假如你是一个媒人,有若干个单身男子登门求助,还有同样多的单身女子也前来征婚.如果你已经知道这些女孩儿在每个男孩儿心目中的排名,以及男孩儿们在 ... 
随机推荐
- ASP.NET MVC5 之 客户端实现文件的下载
			MVC 实现下载功能主要借助于 File 属性: //下载文件接口 public ActionResult GetTrackTempIsc(ICSModels icsModels) { bool fl ... 
- G - And Then There Was One (约瑟夫环变形)
			Description Let’s play a stone removing game. Initially, n stones are arranged on a circle and numbe ... 
- java 装饰者类
			装饰者模式:增强一个类的功能还可以让装饰者类之间互相装饰. 装饰者模式和继承的区别: 继承实现的增强类: 优点:代码结构清晰,而且实现简单 缺点:对于每一个的需要增强的类都要创建具体的子类来帮助其增强 ... 
- sql  所有数据表中 插入字段
			declare @tablename varchar(200)declare @sql varchar(2000)declare cur_t cursor forselect name from sy ... 
- Java学习笔记-eclipse配置
			一.配置Java环境变量 JAVA_HOME:D:\Program Files\Java\jdk1.7.0_76 CLASSPATH:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HO ... 
- 网上商城 Incorrect datetime value: '' for column 'ordertime' at row 1
			今天在做商城项目的[提交订单]功能的时候,向数据库插入数据报错:Incorrect datetime value: '' for column 'ordertime' at row 1 public ... 
- responsive-navigator
			html 代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ... 
- esp8266 SOC方案经过半年沉淀之后再度重启
			我发誓这是最后一次玩esp8266,该脱坑了...... 以前看不懂的教程 http://club.gizwits.com/thread-6447-1-1.html 2018教程 http://clu ... 
- <mybatis:scan>与<MapperScannerConfigurer/>
			使用Mybatis作为持久层的框架,对dao层的bean对象的注解扫描有两种方式:<mybatis:san>.<MapperScannerConfigurer> 一:<m ... 
- VS2010编译错误:fatal error C1189: #error : This file requires _WIN32_WINNT to be #defined at least to 0x
			下面是彻底解决方法:在工程的stdafx.h中添加(如有类似语句,需注释掉)#ifndef WINVER // Allow use of features specific to Windows 95 ... 
