Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P3254 圆桌问题

P3254 圆桌问题

显然是网络流,希望从同一个单位来的代表不在同一个餐桌就餐,即一个单位的代表对于一个餐桌最多选择一个,即流量为 $1$ 。

查看代码
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
#include <iostream>
#include <cstdio>
#include <queue>
#include <climits>
#define INF INT_MAX
using namespace std;
const int N = 430, M = 81850;
int m, n, st, ed, sum, d[N], cur[N];
int idx = -1, hd[N], nxt[M], edg[M], wt[M];
bool bfs()
{
queue <int> q;
for (int i = 0; i <= m + n + 1; i++)
d[i] = -1;
q.push(st);
d[st] = 0;
cur[st] = hd[st];
while (!q.empty())
{
int t = q.front();
q.pop();
for (int i = hd[t]; ~i; i = nxt[i])
{
int to = edg[i];
if (d[to] == -1 && wt[i])
{
cur[to] = hd[to];
d[to] = d[t] + 1;
if (to == ed)
return true;
q.push(to);
}
}
}
return false;
}
int dfs(int x, int limts)
{
if (x == ed)
return limts;
int res = 0;
for (int i = cur[x]; ~i && res < limts; i = nxt[i])
{
cur[x] = i;
int to = edg[i];
if (d[to] == d[x] + 1 && wt[i])
{
int t = dfs(to, min(limts - res, wt[i]));
if (!t)
d[to] = -1;
res += t;
wt[i] -= t;
wt[i ^ 1] += t;
}
}
return res;
}
bool dinic()
{
int res = 0, flow;
while (bfs())
while (flow = dfs(st, INF))
res += flow;
return sum == res;
}
void add(int x, int y, int z)
{
nxt[++idx] = hd[x];
hd[x] = idx;
edg[idx] = y;
wt[idx] = z;
}
int main()
{
cin >> m >> n;
for (int i = 0; i <= m + n; i++)
hd[i] = -1;
st = 0;
ed = m + n + 1;
for (int i = 1, r; i <= m; i++)
{
cin >> r;
sum += r;
add(st, i, r);
add(i, st, 0);
}
for (int i = 1, c; i <= n; i++)
{
cin >> c;
add(i + m, ed, c);
add(ed, i + m, 0);
}
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
{
add(i, j + m, 1);
add(j + m, i, 0);
}
bool flag = dinic();
cout << flag << endl;
if (flag)
{
for (int i = 1; i <= m; i++)
{
for (int j = hd[i]; ~j; j = nxt[j])
if (edg[j] > m && edg[j] <= m + n && !wt[j])
cout << edg[j] - m << ' ';
cout << endl;
}
}
return 0;
}