最多能同时开几台
五个人、四台机器。每个人只会开其中几台,一台机器同时只能一个人用,一个人也只能占一台。
运行下面这段程序:
#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// 五个人、四台机器;LIKE[p] 是第 p 个人会开的机器(MACH 里的下标)
const vector<string> PEOPLE = {"阿岚", "小满", "阿泰", "南风", "北辰"};
const vector<string> MACH = {"甲", "乙", "丙", "丁"};
const vector<vector<int>> LIKE = {{0, 1}, {0}, {2, 3}, {2}, {1, 3}};
const vector<int> ORDER = {0, 1, 2, 3, 4}; // 名单顺序:阿岚、小满、阿泰、南风、北辰
// 增广:给 p 找一台机器;机器被占了,就看占着它的人能不能挪到别处去
bool aug(int p, vector<int>& owner, vector<bool>& seen) {
for (int t : LIKE[p]) {
if (seen[t]) continue;
seen[t] = true;
if (owner[t] == -1 || aug(owner[t], owner, seen)) {
owner[t] = p;
return true;
}
}
return false;
}
int hungarian(const vector<int>& order) {
vector<int> owner(MACH.size(), -1); // 机器 -> 占着它的人
int n = 0;
for (int p : order) {
vector<bool> seen(MACH.size(), false);
if (aug(p, owner, seen)) n++;
}
return n;
}
int main() {
cout << hungarian(ORDER) << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)
全部评论