Stable Matching Problem HDU1522
来源:互联网 发布:津桥留学 知乎 编辑:程序博客网 时间:2024/06/10 20:43
A simple problem about the match problem.
We know that the KM algorithm solved the match problem with weight,but the stable matching is easier.
first: the man ask the best on his mind and never bend him girl.
second: if the girl is free,she will say yes,and match them.
else the girl will choose the better one from the new man and her husband
if the girl choose the new one,her ex become free,and match the new one.
continue until the queue is empty();
the code.
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<vector>#include<map>#include<queue>using namespace std;class man{public: man() {}; queue<int> w_q; string name; int wife_id;};class woman{public: woman() { }; string name; int hus_id; vector<int> love_man;};int main(){ #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif int n,i,j,tmp_id,cur; string m_tmp,w_tmp; vector<man> m_v; vector<woman> w_v; map<string,int> w_mp; map<string,int> m_mp; queue<int> free_q; while(cin>>n) { m_v.clear(); w_v.clear(); w_v.resize(n+1); m_v.resize(n+1); w_mp.clear(); m_mp.clear(); cin>>m_tmp; m_mp.insert(map<string,int>::value_type(m_tmp,1)); //map the first man m_v[1].name=m_tmp; for(j=1;j<=n;j++){ cin>>w_tmp; w_mp.insert(map<string,int>::value_type(w_tmp,j)); //map the n girls m_v[1].w_q.push(j); } for(i=2; i<=n; i++) { cin>>m_tmp; m_mp.insert(map<string,int>::value_type(m_tmp,i)); //map 2->n men m_v[i].name=m_tmp; for(j=1;j<=n;j++){ cin>>w_tmp; m_v[i].w_q.push(w_mp[w_tmp]); //insert the girl number by sort } } for(i=1; i<=n; i++) { cin>>w_tmp; tmp_id=w_mp[w_tmp] ; w_v[tmp_id ].name=w_tmp; w_v[tmp_id ].hus_id=0; //init hus_id w_v[tmp_id ].love_man.resize(n+1); for(j=1;j<=n;j++){ cin>>m_tmp; w_v[tmp_id].love_man[m_mp[m_tmp]]=j; //set the love_man } } for(i=1; i<=n; i++) { free_q.push(i); //set for the freeman } int wid,hid; //tmp while(!free_q.empty()) { cur=free_q.front(); wid=m_v[cur].w_q.front(); //the best woman id m_v[cur].w_q.pop(); //give up the woman,no matter choose or not. if(w_v[wid].hus_id==0) //free { w_v[wid].hus_id=cur; //set husband id m_v[cur].wife_id=wid; //cur set wife id free_q.pop(); //pop the man from free } else { hid=w_v[wid].hus_id; //get the wid-th woman's husband if(w_v[wid].love_man[cur]<w_v[wid].love_man[hid]) //if cur is better { free_q.pop(); //pop the man from free free_q.push(hid); //push the wid-th ex in free w_v[wid].hus_id=cur; //set the wid-th woman husband id m_v[cur].wife_id=wid;//set wife id } //if failed,go on; } } for(i=1; i<=n; i++) { cout<<m_v[i].name<<" "<<w_v[m_v[i].wife_id].name<<endl; }; } #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0;}
0 0
- Stable Matching Problem HDU1522
- GS problem: stable matching
- 【稳定婚姻问题】hdu1522 Marriage is Stable
- Stable-Matching
- Stable Matching Problem稳定匹配问题-----稳定婚姻算法
- Stable Matching Problem稳定匹配问题-----稳定婚姻算法
- Stable Matching Problem —— Gale Shapley算法
- Stable Matching稳定匹配
- A First Problem : Stable Matching(第一个重要问题:稳定匹配)
- 算法导论(第三版)-复习- Stable Matching Problem
- Stable Matching (Gale Sharpley Algorithm)
- Stable Marriage Problem
- stable marriage problem
- Stable marriage problem笔记
- Stable Matching: Gale and Shapley Algorithm
- 算法设计与分析- Stable Matching-Gale_Shapley
- POJ3487 The Stable Marriage Problem
- poj3487-The Stable Marriage Problem
- 硬盘为什么要分区?
- (三)装饰模式
- char p[] = "hello world" 与 char *p = "Hello world" 的区别
- 三角形类2
- Smart & Simple Testimony 安装之后导致js冲突
- Stable Matching Problem HDU1522
- Android面试题
- 黑马程序员_java基础——多线程
- VirtualBox+RHEL5.5+Oracle 10G RAC安装实验--详细过程(三)
- C++获取当前目录
- iOS多线程之NSOperation
- 【Spring MVC】教程——简单的mvc例子
- ios常用的字符串NSString理解
- Spring笔记