diff --git a/ch4/maxflow.cpp b/ch4/maxflow.cpp index 621cd33..2050be7 100644 --- a/ch4/maxflow.cpp +++ b/ch4/maxflow.cpp @@ -18,7 +18,7 @@ typedef pair ii; const ll INF = 1e18; // INF = 1e18, not 2^63-1 to avoid overflow class max_flow { -private: +protected: int V; vector EL; vector AL; @@ -76,7 +76,7 @@ class max_flow { // if you are adding a bidirectional edge u<->v with weight w into your // flow graph, set directed = false (default value is directed = true) - void add_edge(int u, int v, int w, bool directed = true) { + void add_edge(int u, int v, ll w, bool directed = true) { if (u == v) return; // safeguard: no self loop EL.emplace_back(u, v, w, 0); // u->v, cap w, flow 0 AL[u].push_back(EL.size()-1); // remember this index @@ -96,7 +96,6 @@ class max_flow { ll dinic(int s, int t) { ll mf = 0; // mf stands for max_flow - int iter = 0; while (BFS(s, t)) { // an O(VE^2) EK algorithm last.assign(V, 0); // important speedup while (ll f = DFS(s, t)) // exhaust blocking flow @@ -106,6 +105,35 @@ class max_flow { } }; +class max_flow_with_VW : public max_flow { +private: + inline int getIn(int v) { return (v << 1); } + inline int getOut(int v) { return (v << 1) + 1; } + +public: + max_flow_with_VW(int _V) : max_flow(2 * _V) { + for (int i = 0; i < _V; i++) + max_flow::add_edge(getIn(i), getOut(i), INF); // All vertices are initialised with infinite capacity + } + + void add_vertex_weight(int v, ll w) { + get<2>(EL[getIn(v)]) = w; + } + + void add_edge(int u, int v, ll w, bool directed = true) { + max_flow::add_edge(getOut(u), getIn(v), w); + if (!directed) max_flow::add_edge(getOut(v), getIn(u), w); + } + + ll edmonds_karp(int s, int t) { + return max_flow::edmonds_karp(getIn(s), getOut(t)); + } + + ll dinic(int s, int t) { + return max_flow::dinic(getIn(s), getOut(t)); + } +}; + int main() { /* // Graph in Figure 4.24 @@ -131,5 +159,35 @@ int main() { // printf("%lld\n", mf.edmonds_karp(s, t)); printf("%lld\n", mf.dinic(s, t)); + printf("=====\n"); + + /* + // Sample graph with vertex capacities (source and sink have infinite capacities) + 5 0 4 + 5 6 7 + 2 1 5 2 3 + 1 3 10 + 1 3 4 + 1 4 10 + 0 + */ + + scanf("%d %d %d", &V, &s, &t); + max_flow_with_VW mfvw(V); + for (int u = 1; u < V - 1; ++u) { + int w; scanf("%d", &w); + mfvw.add_vertex_weight(u, w); + } + for (int u = 0; u < V; ++u) { + int k; scanf("%d", &k); + while (k--) { + int v, w; scanf("%d %d", &v, &w); + mfvw.add_edge(u, v, w); + } + } + + // printf("%lld\n", mfvw.edmonds_karp(s, t)); + printf("%lld\n", mfvw.dinic(s, t)); + return 0; } diff --git a/ch4/maxflow_in.txt b/ch4/maxflow_in.txt index 8388569..6a4652b 100644 --- a/ch4/maxflow_in.txt +++ b/ch4/maxflow_in.txt @@ -3,3 +3,11 @@ 2 2 25 3 70 3 0 70 3 5 1 25 3 0 30 2 5 1 70 + +5 0 4 +5 6 7 +2 1 5 2 3 +1 3 10 +1 3 4 +1 4 10 +0