簡介
一般場景是男生和女生配對的問題,現有男生要去配對如果想去配對的女生已經有喜歡的男生了,那就讓想去配對的女生已經喜歡的男生挪挪位置,看看想去配對的女生已經喜歡的男生能不能喜歡其他人,給現有配對的男生讓一個位置。
有點繞。但是就是這樣。
參考鏈接
https://www.bilibili.com/video/BV1Wx411L7Di?from=search&seid=16983503622667189725
http://acm.hdu.edu.cn/showproblem.php?pid=2063
code 對應 hdoj 2063
/*
7
1 1
1 2
2 2
2 3
3 1
3 2
4 3
*/
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 510;
/**************************************************
@brief : used 表示男生的某一次訪問過程中女生有沒有被匹配到
nxt 表示女生喜歡的男生
@author : none
@input :none
@output :none
@time : none
**************************************************/
int line[maxn][maxn], used[maxn], nxt[maxn];
int t, n, m, u, v;
/**************************************************
@brief : 男生配對
@author : none
@input :none
@output :none
@time : none
**************************************************/
bool Find(int x) {
for (int i = 1; i <= m; i++) { // m 個女生
if (line[x][i] && !used[i]) { // x 和 i 是互相喜歡的,並且這個妹子名花無主
used[i] = 1;// 表示這個妹子配對上
if (nxt[i] == 0 || Find(nxt[i])) {
// 如果這個妹子沒有匹配上人 或者 這個男生可以喜歡別人
nxt[i] = x;// i 個女生就和 x 配對上
return true;
}
}
}
return false;
}
/**************************************************
@brief : 匹配算法
@author : none
@input :none
@output :none
@time : none
**************************************************/
int match() {
int sum = 0;
for (int i = 1; i <= n; i++) {// n 男生的個數
memset(used, 0, sizeof(used));
if (Find(i)) sum++; // 尋找匹配的妹子
}
return sum;
}
int main() {
ios::sync_with_stdio(false);
while (cin >> t && t) {
cin >> n >> m;
memset(nxt, 0, sizeof(nxt));
memset(line, 0, sizeof(line));
while (t--) {
cin >> u >> v;
line[u][v] = 1;
}
cout << match() << endl;
}
// system("pause");
}
深入思考
能不能用這個C++算法解決下面的問題,下面數據說明 越接近0 表示小姐姐越想去上班,1表示小姐姐那天有事兒不能去上班。
https://www.cnblogs.com/eat-too-much/p/13409628.html
TIPS
上面的是多解法,有多個答案,讓總體的小姐姐上班開心數值最大也就是總和數值(上班心情值)最小。