problemscpp
A collection of my answers to algorithm problems in c++.
载入中...
搜索中...
未找到
comp526::stable_matching::Solution类 参考

#include <comp526.h>

Public 成员函数

vector< int > match (vector< vector< int > >, vector< vector< int > >)
 

Private 属性

vector< Company * > companies
 
vector< Student * > students
 

详细描述

在文件 comp526.h39 行定义.

成员函数说明

◆ match()

vector< int > comp526::stable_matching::Solution::match ( vector< vector< int > > s2c,
vector< vector< int > > c2s )

在文件 comp526.cpp17 行定义.

17 {
18 int N = s2c.size();
19 students = vector<Student *>();
20 companies = vector<Company *>();
21 for(int i = 0; i < N; i++) {
22 Student *stu = new Student(i);
23 stu->c_priority = unordered_map<Company *, int>();
24 stu->priority_c = unordered_map<int, Company *>();
25 students.emplace_back(stu);
26 Company *com = new Company(i);
27 com->priority = unordered_map<Student *, int>();
28 companies.emplace_back(com);
29 }
30 for(int i = 0; i < N; i++) {
31 for(int j = 0; j < N; j++) {
32 students[i]->c_priority[companies[j]] = s2c[i][j];
33 students[i]->priority_c[s2c[i][j]] = companies[j];
34 companies[i]->priority[students[j]] = c2s[i][j];
35 }
36 }
37 bool flag = true;
38 while(flag) {
39 flag = false;
40 for(int i = 0; i < N; i++) {
41 auto stu = students[i];
42 if(stu->matched_company == nullptr) {
43 flag = true;
44 stu->priority_c[stu->current_priority++]->proposed_students.insert(stu);
45 }
46 }
47
48 for(int i = 0; i < N; i++) {
49 auto com = companies[i];
50 Student *current_matched_student = com->matched_student;
51 if(com->proposed_students.size() > 0) {
52 int min_priority = com->matched_student ? com->priority[com->matched_student] : INT_MAX;
53 Student *max_p_student = com->matched_student;
54 for(auto stu: com->proposed_students) {
55 if(com->priority[stu] < min_priority) {
56 min_priority = com->priority[stu];
57 max_p_student = stu;
58 flag = true;
59 }
60 }
61 if(flag) {
62 if(current_matched_student != nullptr) {
63 current_matched_student->matched_company = nullptr;
64 }
65 com->matched_student = max_p_student;
66 max_p_student->matched_company = com;
67 }
68 com->proposed_students.clear();
69 }
70 }
71 }
72 vector<int> ans = vector<int>(N, -1);
73 for(int i = 0; i < N; i++) {
74 ans[i] = students[i]->matched_company->id;
75 }
76 return ans;
77 }
vector< vector< int > > ans

引用了 comp526::stable_matching::Student::c_priority, companies, comp526::stable_matching::Student::matched_company, comp526::stable_matching::Company::priority, comp526::stable_matching::Student::priority_c , 以及 students.

被这些函数引用 comp526::stable_matching::TEST(), comp526::stable_matching::TEST(), comp526::stable_matching::TEST() , 以及 comp526::stable_matching::TEST().

类成员变量说明

◆ companies

vector<Company *> comp526::stable_matching::Solution::companies
private

在文件 comp526.h42 行定义.

被这些函数引用 match().

◆ students

vector<Student *> comp526::stable_matching::Solution::students
private

在文件 comp526.h41 行定义.

被这些函数引用 match().


该类的文档由以下文件生成: