sgu-264. Travel
来源:互联网 发布:js如何验证手机号码 编辑:程序博客网 时间:2024/05/21 13:56
题目大意:
没什么好说的,稳定婚姻问题。
解题思路:
首先将所有的男的压入一次加入队列中,对于每一个男的
AC代码:
#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#include <iostream>#include <map>#include <string>#include <queue>using namespace std;map <string,int> man;map <string,int> woman;string wname[810];string mname[810];int N;int num=0;int Gm[810][810]={{0}};int Gw[810][810]={{0}};int Linkm[810]={0};int Linkw[810]={0};int main(){ ios::sync_with_stdio(0); cin>>N; for(int i=1;i<=N;i++) { cin>>mname[i]; man[mname[i]]=i; for(int j=1;j<=N;j++) { string ch; cin>>ch; if(!woman[ch]) { woman[ch]=++num; wname[num]=ch; } Gm[i][j]=woman[ch]; } } for(int i=1;i<=N;i++) { string ch; cin>>ch; int gg=woman[ch]; wname[gg]=ch; for(int j=1;j<=N;j++) { cin>>ch; Gw[gg][man[ch]]=j; } } queue <int> Que; for(int i=1;i<=N;i++) Que.push(i); for(;!Que.empty();) { int cnt=Que.front(); Que.pop(); int i; for(i=1;i<=N;i++) { int gg=Gm[cnt][i]; if(Linkw[gg]==0) { Linkw[gg]=cnt; Linkm[cnt]=gg; break; } else if(Gw[gg][cnt]<Gw[gg][Linkw[gg]]) { Que.push(Linkw[gg]); Linkw[gg]=cnt; Linkm[cnt]=gg; break; } } if(i>N) Que.push(cnt); } cout<<"YES"<<endl; for(int i=1;i<=N;i++) cout<<mname[i]<<' '<<wname[Linkm[i]]<<endl; return 0;}
0 0
- sgu-264. Travel
- sgu-264. Travel 稳定婚姻问题
- SGU 4444 Travel(最短路+bfs)
- SGU 264 Travel 稳定婚姻匹配 Gale-Shapley算法
- Travel
- Travel
- Travel
- SGU
- SGU
- SGU
- SGU
- SGU
- SGU
- (SGU
- SGU
- SGU
- SGU
- SGU
- Quartz的cron表达式
- 【动态规划】【多重背包】[HDU 1291]悼念512汶川大地震遇难同胞――珍惜现在,感恩生活
- [一起学Hive]之七-向Hive表中加载数据
- 对于return new A();
- UICollectionViewController简单操作
- sgu-264. Travel
- Color the ball(树状数组之插点问线问题)
- 算法准备
- c语言中通过指针将数值赋值到制定内存地址
- delphi 控件 combobox常识
- 深入理解HTTP Session
- C常用的对数组操作的几个方法
- Git使用技巧(整理)三 实战
- Keil4 断点按钮为灰色解决方法