这个名字吸引到我了
其实就是一个逐步求解的过程
/*
* Stayable Marriage Problem
* Author: zhangz
* Language: C++
* Compiler: G++ 5.2.1
* OS: Ubuntu 15.10
* Status: Solved
*
*/
#include<bits/stdc++.h>
#define TIPS 1
//控制是否输出额外的提示信息
using namespace std;
inline void read(int&);
inline void read(int& ,int& );
inline void read(int& ,int& ,int& );
void ZeroRam(int* Dist,int size);
void init();
void clean();
void Tips(char*);
void Tips(string);
void NewCouple(int,int);
void ChangeCouple (int ,int);
void output();
queue<int> Pending;
int Count;
//待配对的男孩、女孩数目
map<string,int> BoyEncoder;
map<int,string> BoyDecoder;
map<string,int> GirlEncoder;
map<int,string> GirlDecoder;
int **BoyPref;
int **GirlPref;
int *NextTarget;
int *BoyFriend;
int *GirlFriend;
int main()
{
Tips("Stayable Marriage Problem Solver\nAuthor: zhangz\nLanguage: C++\nWelcome\n");
Tips("Input Counts of Boys/Girls.\nThis Value Should Equal:");
read(Count);
init();
for(int i=0;i<Count;i++)
{
Tips("\nInput Name For Boy ");
char swap[500];
memset(swap,0,sizeof(swap));
sprintf(swap,"%d :",i);
Tips(swap);
string name;
cin>>name;
BoyEncoder.insert(make_pair(name,i));
BoyDecoder.insert(make_pair(i,name));
}
for(int i=0;i<Count;i++)
{
Tips("\nInput Name For Girl ");
char swap[500];
sprintf(swap,"%d :",i);
Tips(swap);
string name;
cin>>name;
GirlEncoder.insert(make_pair(name,i));
GirlDecoder.insert(make_pair(i,name));
}
for(int i=0;i<Count;i++)
{
char swap[500];
sprintf(swap,"\nNow Input %d Girls' Name In Order For Boy %s :",Count,BoyDecoder[i].c_str());
Tips(swap);
for(int j=0;j<Count;j++)
{
string name;
cin>>name;
int s=GirlEncoder[name];
BoyPref[i][j]=s;
}
Pending.push(i);
}
for(int i=0;i<Count;i++)
{
char t[500];
sprintf(t,"\n Now Input %d Boys' Name In Order For Girl %s :",Count,GirlDecoder[i].c_str());
Tips(t);
int swap;
for(int j=0;j<Count;j++)
{
string name;
cin>>name;
int s=BoyEncoder[name];
GirlPref[i][s]=j;
}
}
while(!Pending.empty())
{
int front=Pending.front();
Pending.pop();
int &Boy=front;
int &Girl=BoyPref[front][NextTarget[front]];
NextTarget[front]++;
if(BoyFriend[Girl]==-1)
{
NewCouple (Boy,Girl);
}
else if (GirlPref[Girl][Boy]<GirlPref[Girl][BoyFriend[Girl]])
{
ChangeCouple (Boy,Girl);
}
else
{
Pending.push(Boy);
}
}
output();
clean();
}
void init()
{
BoyPref=new int *[Count];
for(int i=0;i<Count;i++)
{
BoyPref[i]=new int[Count];
}
GirlPref=new int *[Count];
for(int i=0;i<Count;i++)
{
GirlPref[i]=new int [Count];
}
NextTarget=new int[Count];
ZeroRam(NextTarget,Count);
BoyFriend = new int [Count];
memset(BoyFriend,-1,sizeof(int)*Count);
GirlFriend=new int [Count];
memset(GirlFriend,-1,sizeof(int)*Count);
return;
}
void clean()
{
for(int i=0;i<Count;i++)
{
delete[] BoyPref[i];
delete[] GirlPref[i];
}
delete[] BoyPref;
delete[] GirlPref;
delete[] NextTarget;
delete[] BoyFriend;
delete[] GirlFriend;
return;
}
void ZeroRam(int* Dist,int size)
{
memset(Dist,0,sizeof(int)*size);
}
inline void read(int &x)
{
scanf("%d",&x);
return;
}
inline void read(int &x,int &y)
{
scanf("%d%d",&x,&y);
return;
}
inline void read(int &x,int &y,int &z)
{
scanf("%d%d%d",&x,&y,&z);
return;
}
inline void NewCouple(int Boy,int Girl)
{
BoyFriend[Girl]=Boy;
GirlFriend[Boy]=Girl;
return;
}
inline void ChangeCouple(int Boy,int Girl)
{
Pending.push(BoyFriend[Girl]);
GirlFriend[Boy]=Girl;
GirlFriend[BoyFriend[Girl]]=-1;
BoyFriend[Girl]=Boy;
return;
}
void output()
{
Tips("Now Answer Has Been Calculated\nOutput Now\n");
for(int i=0;i<Count;i++)
{
cout<<BoyDecoder[i]<<"<->"<<GirlDecoder[GirlFriend[i]]<<endl;
}
return;
}
void Tips(char* i)
{
#ifdef TIPS
cout<<i;
#endif
}
void Tips(string i)
{
#ifdef TIPS
cout<<i;
#endif
}