mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-12-24 03:31:59 +00:00
381. 有线电视网络
https://www.acwing.com/problem/content/submission/code_detail/12424269/
This commit is contained in:
parent
224c7ce90e
commit
c457bcadba
112
AcWing/381/381.cpp
Normal file
112
AcWing/381/381.cpp
Normal file
@ -0,0 +1,112 @@
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <queue>
|
||||
|
||||
const int N = 55 << 1,
|
||||
M = 50005,
|
||||
INF = 1e8;
|
||||
|
||||
int n, m, s, t, flow, max_flow, ans;
|
||||
|
||||
// Graph
|
||||
int idx, head[N], edge[M << 1], ver[M << 1], next[M << 1];
|
||||
|
||||
void add(int u, int v, int w) {
|
||||
next[idx] = head[u];
|
||||
ver[idx] = v;
|
||||
edge[idx] = w;
|
||||
head[u] = idx++;
|
||||
}
|
||||
|
||||
// Dinic
|
||||
int d[N], cur[N];
|
||||
|
||||
bool bfs() {
|
||||
memset(d, 0x00, sizeof(d));
|
||||
|
||||
std::queue<int> q;
|
||||
q.push(s);
|
||||
d[s] = 1;
|
||||
cur[s] = head[s];
|
||||
|
||||
while (!q.empty()) {
|
||||
int u = q.front();
|
||||
q.pop();
|
||||
|
||||
for (int i = head[u]; ~i; i = next[i]) {
|
||||
int v = ver[i],
|
||||
w = edge[i];
|
||||
|
||||
if (!d[v] && w) {
|
||||
d[v] = d[u] + 1;
|
||||
cur[v] = head[v];
|
||||
if (v == t) return true;
|
||||
q.push(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int dinic(int u, int limit) {
|
||||
if (u == t) return limit;
|
||||
|
||||
int flow = 0;
|
||||
for (int &i = cur[u]; ~i && flow < limit; i = next[i]) {
|
||||
int v = ver[i],
|
||||
w = edge[i];
|
||||
if (d[v] == d[u] + 1 && w) {
|
||||
int k = dinic(v, std::min(w, limit - flow));
|
||||
if (!k) d[v] = 0;
|
||||
edge[i] -= k;
|
||||
edge[i ^ 1] += k;
|
||||
flow += k;
|
||||
}
|
||||
}
|
||||
|
||||
return flow;
|
||||
}
|
||||
|
||||
int main() {
|
||||
while (scanf("%d%d", &n, &m) != EOF) {
|
||||
idx = 0;
|
||||
ans = n;
|
||||
memset(head, 0xff, sizeof(head));
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
add(i, n + i, 1);
|
||||
add(n + i, i, 0);
|
||||
}
|
||||
for (int i = 0; i < m; i++) {
|
||||
int u, v;
|
||||
scanf(" (%d,%d)", &u, &v);
|
||||
add(u + n, v, INF);
|
||||
add(v, u + n, 0);
|
||||
add(v + n, u, INF);
|
||||
add(u, v + n, 0);
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < i; j++) {
|
||||
s = n + i, t = j;
|
||||
|
||||
for (int k = 0; k < idx; k += 2) {
|
||||
edge[k] += edge[k ^ 1];
|
||||
edge[k ^ 1] = 0;
|
||||
}
|
||||
|
||||
max_flow = 0;
|
||||
while (bfs()) {
|
||||
while (flow = dinic(s, INF)) max_flow += flow;
|
||||
}
|
||||
|
||||
ans = std::min(ans, max_flow);
|
||||
}
|
||||
}
|
||||
|
||||
printf("%d\n", ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user