LOJ 6004「网络流 24 题」圆桌聚餐

建模:

1.从源点向每个单位$x_i$连边,容量是该单位的人数

2.从每张餐桌$y_i$向汇点连边,容量是该餐桌能容纳的人数

3.从每个单位$x_i$向每张餐桌$y_j$连边,容量为$1$

如果最大流量等于所有单位人数之和,则有解,否则无解。

方案:

对于每个单位$x_i$,该单位向$y$集合连出的所有满流量边即为该单位人员的安排情况(证明显然

Code:

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
126
127
128
129
#include <bits/stdc++.h>

#define R register
#define ll long long
#define cmax(a, b) ((a < b) ? b : a)
#define cmin(a, b) ((a < b) ? a : b)
#define sum(a, b, mod) ((a + b) % mod)

const int MaxN = 2e4 + 10;
const int MaxM = 5e5 + 10;
const int inf = (1 << 30);

struct edge
{
int to, next, cap;
};

edge e[MaxM];
int n, m, s = 20000, t = 20001, cnt = 1, ans;
int head[MaxN], dep[MaxN], cur[MaxN], a[MaxN];

inline void add(int u, int v, int c)
{
++cnt;
e[cnt].to = v;
e[cnt].next = head[u];
e[cnt].cap = c;
head[u] = cnt;
}

inline void add_edge(int u, int v, int c) { add(u, v, c), add(v, u, 0); }

inline int read()
{
int x = 0;
char ch = getchar();
while (ch > '9' || ch < '0')
ch = getchar();
while (ch <= '9' && ch >= '0')
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return x;
}

inline int bfs()
{
memset(dep, 0, sizeof(dep));
memcpy(cur, head, sizeof(head));
std::queue<int> q;
dep[s] = 1;
q.push(s);
while (!q.empty())
{
int u = q.front();
q.pop();
for (int i = head[u]; i; i = e[i].next)
{
int v = e[i].to, c = e[i].cap;
if (dep[v] || !c)
continue;
dep[v] = dep[u] + 1;
q.push(v);
}
}
return dep[t];
}

inline int dinic(int u, int flow)
{
if (u == t)
return flow;
int rest = flow;
for (int i = cur[u]; i && (flow - rest < flow); i = e[i].next)
{
int v = e[i].to, c = e[i].cap;
if (dep[v] != dep[u] + 1 || !c)
continue;
int k = dinic(v, cmin(rest, c));
if (!k)
dep[v] = dep[u] + 1;
else
{
e[i].cap -= k;
e[i ^ 1].cap += k;
rest -= k;
}
}
if (flow - rest < flow)
dep[u] = -1;
return flow - rest;
}

inline void solve()
{
int now = 0;
while (bfs())
while ((now = dinic(s, inf)))
ans += now;
}

int main()
{
int x, tmp = 0;
m = read(), n = read();
for (int i = 1; i <= m; i++)
x = read(), add_edge(s, i, x), tmp += x;
for (int i = 1; i <= n; i++)
x = read(), add_edge(i + m, t, x);
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
add_edge(i, j + m, 1);
}
solve();
if (ans != tmp)
return 0 * printf("0");
printf("1\n");
for (int i = 1; i <= m; i++)
{
int h = head[i];
while (h)
{
if (!e[h].cap)
printf("%d ", e[h].to - m);
h = e[h].next;
}
puts("");
}
return 0;
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×