PushRelabel.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. typedef long long LL;
  21. struct Edge {
  22. int from, to, cap, flow, index;
  23. Edge(int from, int to, int cap, int flow, int index) :
  24. from(from), to(to), cap(cap), flow(flow), index(index) {}
  25. };
  26. struct PushRelabel {
  27. int N;
  28. vector<vector<Edge> > G;
  29. vector<LL> excess;
  30. vector<int> dist, active, count;
  31. queue<int> Q;
  32. PushRelabel(int N) : N(N), G(N), excess(N), dist(N), active(N), count(2*N) {}
  33. void AddEdge(int from, int to, int cap) {
  34. G[from].push_back(Edge(from, to, cap, 0, G[to].size()));
  35. if (from == to) G[from].back().index++;
  36. G[to].push_back(Edge(to, from, 0, 0, G[from].size() - 1));
  37. }
  38. void Enqueue(int v) {
  39. if (!active[v] && excess[v] > 0) { active[v] = true; Q.push(v); }
  40. }
  41. void Push(Edge &e) {
  42. int amt = int(min(excess[e.from], LL(e.cap - e.flow)));
  43. if (dist[e.from] <= dist[e.to] || amt == 0) return;
  44. e.flow += amt;
  45. G[e.to][e.index].flow -= amt;
  46. excess[e.to] += amt;
  47. excess[e.from] -= amt;
  48. Enqueue(e.to);
  49. }
  50. void Gap(int k) {
  51. for (int v = 0; v < N; v++) {
  52. if (dist[v] < k) continue;
  53. count[dist[v]]--;
  54. dist[v] = max(dist[v], N+1);
  55. count[dist[v]]++;
  56. Enqueue(v);
  57. }
  58. }
  59. void Relabel(int v) {
  60. count[dist[v]]--;
  61. dist[v] = 2*N;
  62. for (int i = 0; i < G[v].size(); i++)
  63. if (G[v][i].cap - G[v][i].flow > 0)
  64. dist[v] = min(dist[v], dist[G[v][i].to] + 1);
  65. count[dist[v]]++;
  66. Enqueue(v);
  67. }
  68. void Discharge(int v) {
  69. for (int i = 0; excess[v] > 0 && i < G[v].size(); i++) Push(G[v][i]);
  70. if (excess[v] > 0) {
  71. if (count[dist[v]] == 1)
  72. Gap(dist[v]);
  73. else
  74. Relabel(v);
  75. }
  76. }
  77. LL GetMaxFlow(int s, int t) {
  78. count[0] = N-1;
  79. count[N] = 1;
  80. dist[s] = N;
  81. active[s] = active[t] = true;
  82. for (int i = 0; i < G[s].size(); i++) {
  83. excess[s] += G[s][i].cap;
  84. Push(G[s][i]);
  85. }
  86. while (!Q.empty()) {
  87. int v = Q.front();
  88. Q.pop();
  89. active[v] = false;
  90. Discharge(v);
  91. }
  92. LL totflow = 0;
  93. for (int i = 0; i < G[s].size(); i++) totflow += G[s][i].flow;
  94. return totflow;
  95. }
  96. };