我正在尝试为在线法官解决这个问题:
有n
男女之分n
。每个男人用从1
到的数字来评估女性n
,给她们不同的等级,每个女人的估计都与从1
到的男性相似n
。这一切都导致根据对双方最大吸引力的原则和幸福系数的计算形成对。幸福系数是男人的评价和女人的评价之和。
您必须最大化对的幸福系数的总和并输出这些对。
输入:
第一行包含一个自然的n
地方1 ≤ n ≤ 1000
——同性的人数。
下一n
行包含男性对每个女性的评价。
n
女性的最后几行类似地输入。
输出:
第一行应该包含对幸福系数的最大总和。
第二行应该依次包含男性的合作伙伴(首先是第一个人的合作伙伴,然后是第二个人的合作伙伴,依此类推)。
例子:
输入
3
1 2 3
2 3 1
1 2 3
1 2 3
2 3 1
3 1 2
输出
16
3 2 1
我有以下代码:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
void stable_marriage(vector<vector<int>> &matrix) {
int n = matrix[0].size();
vector<int> status(n * 2, -1);/*status[i] contains husband/wife of i, initially -1*/
queue<int> q;
for (int i = 0; i < n; i++)/* Push all single men */
q.push(i);
/* While there is a single men */
while (!q.empty()) {
int i = q.front();
q.pop();
/* iterate through his preference list */
for (int j = 0; j < n; j++) {
/* if girl is single marry her to this man*/
if (status[matrix[i][j]] == -1) {
status[i] = matrix[i][j];/* set this girl as wife of i */
status[matrix[i][j]] = i;/*make i as husband of this girl*/
break;
}
else {
int rank_1, rank_2;/* for holding priority of current husband and most preferable husband*/
for (int k = 0; k < n; k++) {
if (matrix[matrix[i][j]][k] == status[matrix[i][j]])
rank_1 = k;
if (matrix[matrix[i][j]][k] == i)
rank_2 = k;
}
/* if current husband is less attractive
than divorce him and marry a new one making the old one
single */
if (rank_2 < rank_1) {/* if this girl j prefers current man i
more than her present husband */
status[i] = matrix[i][j];/* her wife of i */
int old = status[matrix[i][j]];
status[old] = -1;/*divorce current husband*/
q.push(old);/*add him to list of singles */
status[matrix[i][j]] = i;/* set new husband for this girl*/
break;
}
}
}
}
for (int i = n - 1; i >= 0; i--) {
cout << status[i] << ' ';
}
}
int main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n;
cin >> n;
vector<vector<int>> matrix(n * 2, vector<int>(n));
for (int i = 0; i < n * 2; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}
stable_marriage(matrix);
return 0;
}
现在,我不明白幸福系数是如何测量的?没看懂,能帮忙吗?