【稳定匹配】:poj3487,The Stable Marriage Problem


http://poj.org/problem?id=3487

Gale-Shapley算法求稳定婚姻

# include<iostream>
# include<map>
# include<vector>
using namespace std;

# define N 30

int n;
char m[N],w[N];
vector<vector<char> > mL,wL,MproposeW;
map<char, char> MmatchW;
map<char, char> WmatchM;

void FindFreeMan(int &a, int &b)
{
	for(int j=0;j<n;j++) //for each man
	{
		if(MmatchW.count(m[j])==0) //a free man
		{
			for(int k=0;k<MproposeW[j].size();k++)
			{
				if(MproposeW[j][k]!='#') //also hasn't propose to all woman(woman k)
				{
					a=j;
					b=k;
					return;
				}
			}
		}
	}
	a=-1;
	b=-1;
}

//判断(in the j-th man's list)the k-th woman是否喜欢the j-th man超过当前的约会男友WmatchM[mL[j][k]](mi)
bool LoveMore(int k, int j)
{
	char mj=m[j];
	char mi=WmatchM[mL[j][k]];
	int t;
	for(t=0;t<n;t++) //find the index of (in the j-th man's list)the k-th woman
	{
		if(w[t]==mL[j][k])
		{
			break;
		}
	}
	for(int i=0;i<wL[t].size();i++) //not wL[k].size()
	{
		if(wL[t][i]==mj) //mj rank front, not wL[k][i]==mj
		{
			return true;
		}
		if(wL[t][i]==mi) //mi rank front, not wL[k][i]==mi
		{
			return false;
		}
	}
}
int main()
{
	int c;
	int i,j,k,f;	
	char t,tt;
	vector<char> temp;

	cin>>c;
	for(i=0;i<c;i++)
	{
		MmatchW.clear();
		WmatchM.clear();
		mL.clear();
		wL.clear();
		MproposeW.clear();

		cin>>n;

		for(j=0;j<n;j++)
		{
			cin>>m[j];
		}
		for(j=0;j<n;j++)
		{
			cin>>w[j];
		}

		for(j=0;j<n;j++)
		{
			cin>>t>>tt;
			temp.clear();
			for(k=0;k<n;k++)
			{
				cin>>tt;
				temp.push_back(tt);
			}
			mL.push_back(temp);
			MproposeW.push_back(temp);
		}
		for(j=0;j<n;j++)
		{
			cin>>t>>tt;
			temp.clear();
			for(k=0;k<n;k++)
			{
				cin>>tt;
				temp.push_back(tt);
			}
			wL.push_back(temp);
		}
		


		while(true)
		{
			//find a free man(the j-th one), he also hasn't propose to all woman(注意这里的j是下标)
			//(in the j-th man's list)the k-th woman is the most front one hasn't been proposed by the j-th man(注意这里的k是下标)
			FindFreeMan(j, k);
			//注意,从下面开始,k表示的是man j list中的k-th woman,而不是全局的k-th,所以一定要注意下标使用什么!!!
			if(j==-1 && k==-1)
			{
				if(i!=0)
				{
					cout<<endl;
				}
				for(map<char, char>::iterator iter=MmatchW.begin();iter!=MmatchW.end();iter++)
				{
					cout<<iter->first<<" "<<iter->second<<endl;
				}
				break;
			}
			MproposeW[j][k]='#'; //the j-th man proposes to the k-th woman
			if(WmatchM.count(mL[j][k])==0) //(in the j-th man's list)the k-th woman is free
			{
				MmatchW[m[j]]=mL[j][k];
				WmatchM[mL[j][k]]=m[j];
			}
			else //(in the j-th man's list)the k-th woman is in a date state
			{
				if(LoveMore(k,j)==true) //判断(in the j-th man's list)the k-th woman是否喜欢the j-th man超过当前的约会男友WmatchM[mL[j][k]]
				{
					//release old date state
					MmatchW.erase(WmatchM[mL[j][k]]); //not WmatchM[w[k]]
					WmatchM.erase(mL[j][k]);

					//build new date state
					MmatchW[m[j]]=mL[j][k];
					WmatchM[mL[j][k]]=m[j];
				}
				else
				{
					//do nothing
				}
			}
		}
	}
	return 0;
}



你可能感兴趣的:(poj,Marriage,稳定匹配,the,stable,poj3487)