SGU 264 Travel(稳定婚姻匹配)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=264
题意:稳定婚姻匹配。
思路:对于每个男的依次从自己最喜欢的女的开始求婚若女的还没有被匹配或者女的之前匹配的不如这个好,女的会放弃之前的而选择这个。循环这个过程直到全部匹配完。
#include <iostream> #include <cstdio> #include <string.h> #include <algorithm> #include <cmath> #include <vector> #include <queue> #include <set> #include <stack> #include <string> #include <map>#define max(x,y) ((x)>(y)?(x):(y)) #define min(x,y) ((x)<(y)?(x):(y)) #define abs(x) ((x)>=0?(x):-(x)) #define i64 long long #define u32 unsigned int #define u64 unsigned long long #define clr(x,y) memset(x,y,sizeof(x)) #define CLR(x) x.clear() #define ph(x) push(x) #define pb(x) push_back(x) #define Len(x) x.length() #define SZ(x) x.size() #define PI acos(-1.0) #define sqr(x) ((x)*(x))#define FOR0(i,x) for(i=0;i<x;i++) #define FOR1(i,x) for(i=1;i<=x;i++) #define FOR(i,a,b) for(i=a;i<=b;i++) #define DOW0(i,x) for(i=x;i>=0;i--) #define DOW1(i,x) for(i=x;i>=1;i--) #define DOW(i,a,b) for(i=a;i>=b;i--) using namespace std;void RD(int &x){scanf("%d",&x);} void RD(i64 &x){scanf("%I64d",&x);} void RD(u32 &x){scanf("%u",&x);} void RD(double &x){scanf("%lf",&x);} void RD(int &x,int &y){scanf("%d%d",&x,&y);} void RD(i64 &x,i64 &y){scanf("%I64d%I64d",&x,&y);} void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);} void RD(double &x,double &y){scanf("%lf%lf",&x,&y);} void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);} void RD(i64 &x,i64 &y,i64 &z){scanf("%I64d%I64d%I64d",&x,&y,&z);} void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);} void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);} void RD(char &x){x=getchar();} void RD(char *s){scanf("%s",s);} void RD(string &s){cin>>s;}void PR(int x) {printf("%d\n",x);} void PR(i64 x) {printf("%I64d\n",x);} void PR(u32 x) {printf("%u\n",x);} void PR(u64 x) {printf("%llu\n",x);} void PR(double x) {printf("%.4lf\n",x);} void PR(char x) {printf("%c\n",x);} void PR(char *x) {printf("%s\n",x);} void PR(string x) {cout<<x<<endl;}int n,fNum,mNum; map<string,int> F,M; map<string,int>::iterator it; map<int,string> F1,M1; char s[15]; string str; int a[805][805],b[805][805]; int p[805];void input() {RD(n);fNum=mNum=0;int i,j,x,y;for(i=1;i<=n;i++){scanf("%s",s);str=s;x=M[str]=++mNum;M1[mNum]=str;for(j=1;j<=n;j++){scanf("%s",s);str=s;it=F.find(str);if(it==F.end()){y=F[str]=++fNum;F1[fNum]=str;}else y=(*it).second;b[x][j]=y;}}for(i=1;i<=n;i++){scanf("%s",s);str=s;it=F.find(str);x=(*it).second;for(j=1;j<=n;j++){scanf("%s",s);str=s;it=M.find(str);y=(*it).second;a[x][y]=j;}} }void deal() {queue<int> Q;int i,x,y;for(i=1;i<=n;i++) Q.push(i);while(!Q.empty()){x=Q.front();Q.pop();for(i=1;i<=n;i++){y=b[x][i];if(!p[y]){p[y]=x;break;}else if(a[y][x]<a[y][p[y]]){Q.push(p[y]);p[y]=x;break;}}if(i>n) Q.push(x);} }void print() {puts("YES");int i;for(i=1;i<=n;i++){cout<<M1[p[i]]<<' '<<F1[i]<<endl;} }int main() {input();deal();print();return 0; }
SGU 264 Travel(稳定婚姻匹配)相关推荐
- 稳定婚姻匹配(Stable Match, HDOJ 1435, HDOJ 1914, 详解)
稳定婚姻匹配 HDOJ 1435 Stable Match(此例基于发射点优先的匹配,但题目并未明确表明) Time Limit: 2000/1000 MS (Java/Others) Memory ...
- Hdu1914稳定婚姻匹配
题意: 给出 n 个男生,n 个女生,给出他们对异性的喜欢顺序,求一个稳定的婚姻组合. 思路: 稳定婚姻匹配裸题 #include <cstdio> #include <cstrin ...
- HDU 1435 Stable Match (稳定婚姻匹配)
题意:建立一个匹配表,使得一个发射点对应一个接收点,对于某一个发射点来说,它的接收点离它越近那么就会更稳定,同样对于接收点也是一样的情况. 匹配的目标是使得整个网络变得稳定. 对于某2个匹配,比如,( ...
- HDU 1522 Marriage is Stable (稳定婚姻匹配)
题意:稳定婚姻匹配问题,板子题. 题解:稳定婚姻匹配 还好做了这题,之前的板子有点问题,1435数据太弱了. #define _CRT_SECURE_NO_WARNINGS #include<i ...
- HDU 1914 The Stable Marriage Problem (稳定婚姻匹配)
题意:稳定婚姻匹配问题,板子题. 题解:稳定婚姻匹配 注意按字典序输出,还有输出格式,最后个样例不输出空行. #define _CRT_SECURE_NO_WARNINGS #include<i ...
- Marriage is Stable 【HDU - 1522】【稳定婚姻匹配问题】
题目链接 什么是婚姻匹配问题呢?我做了这道题后就有了这样的体会 -- 比谁更渣??? 我们知道,有N个男生还有N个女生,要把每个男生和女生匹配起来,让他们结婚,但是呢,有这样的情况,就是假如我们现在B ...
- ALG1:GS算法,稳定婚姻匹配问题
ALG1:GS算法,稳定婚姻匹配问题 问题描述如下: (详见NKU计算机上机课程辅助评测) 稳定婚姻匹配 实现代码: (此代码逻辑可通过全部测试样例) 由于代码有一段时间了,一下的展示代码是没有自己的 ...
- 图论 —— 稳定婚姻问题与延迟认可算法
[稳定婚姻问题] 1.集合 M 表示 n 个男性 2.集合 F 表示 n 个女性 3.对于每个人我们都按异性的中意程度给出一份名单(从最中意的到最不中意的) 如果没有 ,f 对 m 比对她的配偶中意的 ...
- hdu - 1435 Stable Match 稳定婚姻问题、Gale-Shapley算法模板
稳定婚姻的描述: 假如你是一个媒人,有若干个单身男子登门求助,还有同样多的单身女子也前来征婚.如果你已经知道这些女孩儿在每个男孩儿心目中的排名,以及男孩儿们在每个女孩儿心中的排名,你应该怎样为他们牵线 ...
最新文章
- Windows系统安全管理
- 小白学python买什么书-小白如何高效率学习python?真心建议(附教程)
- ThreadLocal小记
- .Net Micro Framework研究—绘图
- nginx配置长连接
- java 读取properties文件
- linux/windows上STM32固件烧写工具的开发demo
- 转 Grand Central Dispatch 基础教程:Part 1/2 -swift
- 80-450-020-原理-索引-索引失效与优化
- 再也不学AJAX了!(三)跨域获取资源 ② - JSONP CORS
- matlab求解拉格朗日第一方程,matlab求解拉格朗日方程肿么编程
- 小程序数据分析工具:TalkingData
- 键盘切换不出中文输入法的解决方法
- codewars练习(javascript)-2021/3/23
- 10.27 正睿提高9
- 【CV系列】图像算法之一:Randon变换
- 移动硬盘某个分区打不开,显示“文件或目录损坏且无法读取”的解决方法
- 奶爸英语学习课程要点(4-6课)
- 不同试验设计遗传力的计算方法
- 电动汽车充电站的最优选址定容MATLAB程序