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 130 131 132 133 134 135 136 137 138
| template <typename T> class FlowGraph { public: struct Edge { std::size_t to; T cap; }; std::vector<int> dist; std::vector<std::size_t> cur; std::vector<std::vector<std::size_t>> adj; std::vector<Edge> edges; std::size_t s, t; std::size_t vtot, etot;
FlowGraph() : vtot(0), etot(0) {}
void init(std::size_t s, std::size_t t, std::size_t vtot) { this->s = s, this->t = t, this->vtot = vtot; adj.resize(vtot), dist.resize(vtot), cur.resize(vtot); }
void add(std::size_t u, std::size_t v, T f) { adj.at(u).push_back(edges.size()); edges.emplace_back(v, f); adj.at(v).push_back(edges.size()); edges.emplace_back(u, 0); } };
template <typename T> class PushRelabel : public FlowGraph<T> { public: std::vector<T> height; std::vector<T> excess; std::vector<std::size_t> gap; std::vector<std::vector<std::size_t>> bucket; T level{0}; static constexpr T inf = std::numeric_limits<T>::max();
bool push(std::size_t u) { bool init = u == this->s;
for (auto e : this->adj.at(u)) { auto [v, cap] = this->edges.at(e); if (cap == 0 or this->height.at(v) == inf) continue; if (!init and this->height.at(u) != this->height.at(v) + 1) continue;
T k = init ? cap : std::min(cap, this->excess.at(u)); if (v != this->s and v != this->t and this->excess.at(v) == 0) { this->bucket.at(this->height.at(v)).push_back(v); this->level = std::max(this->level, this->height.at(v)); } this->excess.at(u) -= k; this->excess.at(v) += k; this->edges.at(e).cap -= k; this->edges.at(e ^ 1).cap += k; if (this->excess.at(u) == 0) return false; } return true; }
void relabel(std::size_t u) { this->height.at(u) = inf; for (auto e : this->adj.at(u)) { auto [v, cap] = this->edges.at(e); if (cap > 0) this->height.at(u) = std::min(this->height.at(u), this->height.at(v)); } this->height.at(u)++; if (this->height.at(u) < static_cast<T>(this->vtot)) { this->bucket.at(this->height.at(u)).push_back(u); level = std::max(level, this->height.at(u)); ++this->gap.at(this->height.at(u)); } }
bool bfs_init() { this->height.assign(this->vtot, inf); std::queue<std::size_t> q; q.push(this->t); this->height.at(this->t) = 0; while (!q.empty()) { auto u = q.front(); q.pop(); for (auto e : this->adj.at(u)) { auto [v, cap] = this->edges.at(e); auto [rv, rcap] = this->edges.at(e ^ 1); if (rcap > 0 and this->height.at(v) > this->height.at(u) + 1) { this->height.at(v) = this->height.at(u) + 1; q.push(v); } } } return this->height.at(this->s) != inf; }
std::size_t select() { while (this->level > -1 and this->bucket.at(this->level).size() == 0) this->level--; return this->level == -1 ? 114514 : this->bucket.at(this->level).back(); }
public: void init(std::size_t s, std::size_t t, std::size_t n) { FlowGraph<T>::init(s, t, n); this->height.assign(n, inf); this->excess.assign(n, 0); this->gap.assign(n + 1, 0); this->bucket.assign(n + 1, {}); this->level = 0; } T max_flow() { if (not this->bfs_init()) return 0;
this->gap.assign(this->vtot, 0); for (std::size_t i = 0; i < this->vtot; i++) if (this->height.at(i) != inf) this->gap.at(this->height.at(i))++; this->height.at(this->s) = this->vtot; this->push(this->s);
for (std::size_t u = select(); u != 114514; u = select()) { this->bucket.at(this->level).pop_back(); if (this->push(u)) { if (not --this->gap.at(this->height.at(u))) { for (std::size_t i = 0; i < this->vtot; i++) { if (i == this->s) continue; if (this->height.at(i) >= static_cast<T>(this->vtot + 1)) continue; if (this->height.at(i) <= this->height.at(u)) continue; this->height.at(i) = static_cast<T>(this->vtot + 1); } } this->relabel(u); } } return this->excess.at(this->t); } };
|