ZOJ 3933 Team Formation(KM)

题意:

$N\le 500个人,分为X组和Y组,每个人可能是男或者女$
$X和Y要匹配,现要每个人都有厌恶的list,不和list上的人匹配$
$求最大匹配数,以及满足条件下的女生总和最多的方案,输出任意方案$

分析:

$赤果果的最大权匹配模型,500个点费用流显然是跑不过去的(反正我的不行)$
$对于这种求妹子数的也是很经典的模型了$
$然后能连的边权是1,如果有妹子就边权增加$ 妹子数*D$,D是个比最大匹配数大的数$
$跑出来妹子数就是最大权/D,打印方案即可$
$连边的时候X部还是Y部要注意,打印方案的时候也要注意$
$时间复杂度O(n^3)$

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// Created by TaoSama on 2016-04-10
// Copyright (c) 2016 TaoSama. All rights reserved.
//
#pragma comment(linker, "/STACK:102400000,102400000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 500 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int g[N][N], slack[N];
int lx[N], ly[N];
int match[N], n;
bool vx[N], vy[N];
bool dfs(int u) {
vx[u] = 1;
for(int i = 1; i <= n; i++) {
if(!vy[i]) {
int t = lx[u] + ly[i] - g[u][i];
if(t == 0) {
vy[i] = 1;
if(match[i] == -1 || dfs(match[i])) {
match[i] = u;
return 1;
}
} else slack[i] = min(slack[i], t);
}
}
return 0;
}
void KM() {
memset(match, -1, sizeof(match));
memset(ly, 0, sizeof(ly));
for(int i = 1; i <= n; i++) {
lx[i] = -INF;
for(int j = 1; j <= n; j++)
lx[i] = max(lx[i], g[i][j]);
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) slack[j] = INF;
while(1) {
memset(vx, 0, sizeof(vx));
memset(vy, 0, sizeof(vy));
if(dfs(i)) break;
int d = INF;
for(int j = 1; j <= n; j++)
if(!vy[j]) d = min(d, slack[j]);
for(int j = 1; j <= n; j++)
if(vx[j]) lx[j] -= d;
for(int j = 1; j <= n; j++)
if(vy[j]) ly[j] += d;
}
}
}
const int D = 1e6;
bool lft[N], girl[N];
int main() {
#ifdef LOCAL
freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
int t; scanf("%d", &t);
while(t--) {
scanf("%d", &n);
char buf[N];
scanf("%s", buf + 1);
for(int i = 1; i <= n; ++i)
lft[i] = buf[i] == '0';
scanf("%s", buf + 1);
for(int i = 1; i <= n; ++i)
girl[i] = buf[i] == '0';
memset(g, 0, sizeof g);
for(int i = 1; i <= n; ++i) {
if(!lft[i]) continue;
for(int j = 1; j <= n; ++j) {
if(lft[j]) continue;
g[i][j] = (girl[i] + girl[j]) * D + 1;
}
}
for(int i = 1; i <= n; ++i) {
int cnt; scanf("%d", &cnt);
while(cnt--) {
int x; scanf("%d", &x);
if(lft[i] && !lft[x]) g[i][x] = 0;
}
}
KM();
int matches = 0, ans = 0;
vector<pair<int, int> > path;
for(int i = 1; i <= n; ++i) {
if(match[i] == -1 || !g[match[i]][i]) continue;
++matches;
ans += g[match[i]][i];
path.push_back(make_pair(match[i], i));
}
printf("%d %d\n", matches, ans / D);
for(int i = 0; i < path.size(); ++i)
printf("%d %d\n", path[i].first, path[i].second);
}
return 0;
}


1. 除非注明,本博文即为原创,转载请注明链接地址
2. 本博文只代表博主当时的观点或结论,请不要恶意攻击
3. 如果本文帮到了您,不妨点一下 下面分享到 按钮,让更多的人看到