LA 3989 - Ladies' Choice 稳定婚姻问题
题目大意:
在盛大的校园舞会上有n位男生和n位女生,每人都对每个异性有一个排序,代表对他们的喜欢程度。你的任务是将男生和女生一一配对,使得男生U和女生V不存在一下情况1.男生u和女生v不是舞伴2,他们喜欢对方的程度都大于各自当前舞伴的程度。如果出现了2中的情况,他们可能擅自抛下自己的舞伴,另外组成一对。
你的任务是对于每个女生,在所有可能和她跳舞的男生中,找出她最喜欢的那一个。
思路:
看图。。。。
刘汝佳书上的原题。。划线的部分我只是想表明这个算法的。。。。丧失。。。。
稳定婚姻问题,求婚拒绝算法的实现。。。我觉得我也丧心病狂了。。。。。。情人节就是要做这种。。。。。
- #include<cstdio>
- #include<queue>
- #include<cstring>
- using namespace std;
- const int MAXN=1000+10;
- int future_wife[MAXN],future_husband[MAXN];
- int order_man[MAXN][MAXN],order_woman[MAXN][MAXN],next_persue[MAXN];
- queue<int> q; //为订婚男士排队中。。。
- void engage(int man,int woman) //订婚
- {
- int getout=future_husband[woman]; //这个可怜的男人被抛弃了。当然可能为0,就是没有这个人
- if(getout!=0)
- {
- q.push(getout); //兄弟,你得重新找别人了。。
- future_wife[getout]=0; //抛弃现任未婚夫
- }
- future_husband[woman]=man;
- future_wife[man]=woman;
- }
- int main()
- {
- int T;
- scanf("%d",&T);
- while(T--)
- {
- while(!q.empty())
- q.pop();
- int n;
- scanf("%d",&n);
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=n;j++)
- scanf("%d",&order_man[i][j]); //编号为i的男士第j个喜欢的人。
- future_wife[i]=0; //接下来应该向排名为1的女士求婚。
- next_persue[i]=1; //没有未婚妻。
- q.push(i);
- }
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=n;j++)
- {
- int temp;
- scanf("%d",&temp);
- order_woman[i][temp]=j; //在编号为i的女士心目中,编号为temp的男士排名
- }
- future_husband[i]=0; //没有未婚夫
- }
- while(!q.empty())
- {
- int man=q.front(); //该这个男的求婚了!
- q.pop();
- int woman=order_man[man][next_persue[man]++]; //下一个求婚对象
- if(future_husband[woman]==0) //如果女士没有对象,直接订婚
- engage(man,woman);
- else if(order_woman[woman][man]<order_woman[woman][ future_husband[woman] ])
- engage(man,woman); //由于好感度高于现在女士的未婚夫,所以抢亲成功。
- else
- q.push(man); //直接被拒绝,下次再来。
- }
- for(int i=1;i<=n;i++) printf("%d\n",future_wife[i]);
- if(T) printf("\n");
- }
- return 0;
- }
LA 3989 - Ladies' Choice 稳定婚姻问题的更多相关文章
- UVA 1175 Ladies' Choice 稳定婚姻问题
题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...
- UVALive3989 Ladies' Choice —— 稳定婚姻问题 Gale - Shapely算法
题目链接:https://vjudge.net/problem/UVALive-3989 题解: 题意:有n个男生和n个女生.每个女生对男神都有个好感度排行,同时每个男生对每个女生也有一个好感度排行. ...
- UVALive 3989 Ladies' Choice
Ladies' Choice Time Limit: 6000ms Memory Limit: 131072KB This problem will be judged on UVALive. Ori ...
- UVALive 3989 Ladies' Choice(稳定婚姻问题:稳定匹配、合作博弈)
题意:男女各n人,进行婚配,对于每个人来说,所有异性都存在优先次序,即最喜欢某人,其次喜欢某人...输出一个稳定婚配方案.所谓稳定,就是指未结婚的一对异性,彼此喜欢对方的程度都胜过自己的另一半,那么这 ...
- UVALive 3989Ladies' Choice(稳定婚姻问题)
题目链接 题意:n个男生和女生,先是n行n个数,表示每一个女生对男生的好感值排序,然后是n行n列式每一个男生的好感值排序,输出N行,即每个女生在最好情况下的男生的编号 分析:如果是求女生的最好情况下, ...
- UVALive 3989 Ladies' Choice
经典的稳定婚姻匹配问题 UVALive - 3989 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: ...
- Ladies' Choice UVALive - 3989 稳定婚姻问题 gale_shapley算法
/** 题目: Ladies' Choice UVALive - 3989 链接:https://vjudge.net/problem/UVALive-3989 题意:稳定婚姻问题 思路: gale_ ...
- 【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)
Ladies' Choice Teenagers from the local high school have asked you to help them with the organizatio ...
- UVALive-3989 Ladies' Choice (稳定婚姻问题)
题目大意:稳定婚姻问题.... 题目分析:模板题. 代码如下: # include<iostream> # include<cstdio> # include<queue ...
随机推荐
- sed的一些tricks
1.sed -f xx.sed input_file 可以将一系列操作放在一个xx.sed脚本里执行 ``` #!/bin/sed -f ``` 2.在匹配字符串后面或行尾添加内容 在text后面添加 ...
- openssl之BIO系列之25---结束语
(作者:DragonKing, Mail: wzhah@263.net ,公布于:http://openssl.126.com之ope nssl专业论坛) 经过半个月左右,最终将BIO的结构和各个分支 ...
- css中linear-gradient的使用
注明:此篇文章来自http://www.cnblogs.com/lhb25/archive/2013/01/30/css3-text-shadow.html.
- 洛谷P2891 [USACO07OPEN]吃饭Dining
题目描述 Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she w ...
- Java(标识符,关键字,注释,常量,变量)
标识符 在java程序中有些名字是可以自定义的,那么这些自定义的名字我们就称作为自定义的标识符. 标识符要注意的细节: 标识符的组成元素是由字母(a-zA-Z).数字(0-9).下划线(_).美元符号 ...
- spring三大框架整合
Spring概述 Spring介绍 Spring它是一个一站式的分层轻量级框架. Spring体系结构 1. core container a) beans与core ...
- 【习题 8-10 UVA - 1614】Hell on the Markets
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 证明:前i个数一定能凑够1..sum[i]中的所有数字 i=1时显然成立. 现在假设i>=2时结论成立 即前i个数字能凑出1. ...
- [Angular & Unit Testing] Testing a RouterOutlet component
The way to test router componet needs a little bit setup, first we need to create a "router-stu ...
- 论Node在构建超媒体API中的作用
论Node在构建超媒体API中的作用 作者:chszs,转载需注明. 博客主页:http://blog.csdn.net/chszs 超媒体即Hypermedia,是一种採用非线性网状结构对块状多媒体 ...
- LinearLayout-控件不显示
今天Mms遇到了一个问题,布局如下 <RelativeLayout android:layout_width="match_par ...