稳定婚配问题:n个男生n个女生,其中每个人都有自己心仪的列表。问如何达成稳定的匹配(比如, b想B求婚,但是B已有的对象的优先级高于b,此时b的魅力不足以拆散B所处的那一对,即达到稳定状态。)
(Gale_Shapley algorithm)总体策略:男士负责求婚,女士负责接受或者拒绝。
题目原型: HDUOJ 1914 The Stable Marriage Problem
以下为数据生成函数,生成boys_rankings.txt 和 girls_rankings.txt
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <ctime>
#include <fstream>
using namespace std;
vector<string> gen_boys(int number)
{
vector<string> result;
for(int i = 0; i < number; i++){
string boy = "B";
string s;
stringstream ss;
ss << i;
ss >> s;
boy.append(s);
result.push_back(boy);
}
return result;
}
vector<string> gen_girls(int number)
{
vector<string> result;
for(int i = 0; i < number; i++){
string boy = "G";
string s;
stringstream ss;
ss << i;
ss >> s;
boy.append(s);
result.push_back(boy);
}
return result;
}
vector<string> gen_ranking(vector<string> candidate)
{
random_shuffle(candidate.begin(), candidate.end());
return candidate;
}
void write_rankings(vector<string> people, vector<vector<string> > rankings, string out_file)
{
ofstream ofs(out_file.c_str());
if(ofs.fail()){
cerr << "Error: " << out_file << endl;
return;
}
for(int i = 0; i < people.size(); i++){
ofs << people[i] << ":";
for(int j = 0; j < rankings[i].size(); j++){