PushRelabel.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Adjacency list implementation of FIFO push relabel maximum flow
  2. // with the gap relabeling heuristic. This implementation is
  3. // significantly faster than straight Ford-Fulkerson. It solves
  4. // random problems with 10000 vertices and 1000000 edges in a few
  5. // seconds, though it is possible to construct test cases that
  6. // achieve the worst-case.
  7. //
  8. // Running time:
  9. // O(|V|^3)
  10. //
  11. // INPUT:
  12. // - graph, constructed using AddEdge()
  13. // - source
  14. // - sink
  15. //
  16. // OUTPUT:
  17. // - maximum flow value
  18. // - To obtain the actual flow values, look at all edges with
  19. // capacity > 0 (zero capacity edges are residual edges).
  20. #include <cmath>
  21. #include <vector>
  22. #include <iostream>
  23. #include <queue>
  24. using namespace std;
  25. typedef long long LL;
  26. struct Edge {
  27. int from, to, cap, flow, index;
  28. Edge(int from, int to, int cap, int flow, int index) :
  29. from(from), to(to), cap(cap), flow(flow), index(index) {}
  30. };
  31. struct PushRelabel {
  32. int N;
  33. vector<vector<Edge> > G;
  34. vector<LL> excess;
  35. vector<int> dist, active, count;
  36. queue<int> Q;
  37. PushRelabel(int N) : N(N), G(N), excess(N), dist(N), active(N), count(2*N) {}
  38. void AddEdge(int from, int to, int cap) {
  39. G[from].push_back(Edge(from, to, cap, 0, G[to].size()));
  40. if (from == to) G[from].back().index++;
  41. G[to].push_back(Edge(to, from, 0, 0, G[from].size() - 1));
  42. }
  43. void Enqueue(int v) {
  44. if (!active[v] && excess[v] > 0) { active[v] = true; Q.push(v); }
  45. }
  46. void Push(Edge &e) {
  47. int amt = int(min(excess[e.from], LL(e.cap - e.flow)));
  48. if (dist[e.from] <= dist[e.to] || amt == 0) return;
  49. e.flow += amt;
  50. G[e.to][e.index].flow -= amt;
  51. excess[e.to] += amt;
  52. excess[e.from] -= amt;
  53. Enqueue(e.to);
  54. }
  55. void Gap(int k) {
  56. for (int v = 0; v < N; v++) {
  57. if (dist[v] < k) continue;
  58. count[dist[v]]--;
  59. dist[v] = max(dist[v], N+1);
  60. count[dist[v]]++;
  61. Enqueue(v);
  62. }
  63. }
  64. void Relabel(int v) {
  65. count[dist[v]]--;
  66. dist[v] = 2*N;
  67. for (int i = 0; i < G[v].size(); i++)
  68. if (G[v][i].cap - G[v][i].flow > 0)
  69. dist[v] = min(dist[v], dist[G[v][i].to] + 1);
  70. count[dist[v]]++;
  71. Enqueue(v);
  72. }
  73. void Discharge(int v) {
  74. for (int i = 0; excess[v] > 0 && i < G[v].size(); i++) Push(G[v][i]);
  75. if (excess[v] > 0) {
  76. if (count[dist[v]] == 1)
  77. Gap(dist[v]);
  78. else
  79. Relabel(v);
  80. }
  81. }
  82. LL GetMaxFlow(int s, int t) {
  83. count[0] = N-1;
  84. count[N] = 1;
  85. dist[s] = N;
  86. active[s] = active[t] = true;
  87. for (int i = 0; i < G[s].size(); i++) {
  88. excess[s] += G[s][i].cap;
  89. Push(G[s][i]);
  90. }
  91. while (!Q.empty()) {
  92. int v = Q.front();
  93. Q.pop();
  94. active[v] = false;
  95. Discharge(v);
  96. }
  97. LL totflow = 0;
  98. for (int i = 0; i < G[s].size(); i++) totflow += G[s][i].flow;
  99. return totflow;
  100. }
  101. };