PushRelabel.cc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // Running time:
  8. // O(|V|^3)
  9. // INPUT:
  10. // - graph, constructed using AddEdge()
  11. // - source
  12. // - sink
  13. // OUTPUT:
  14. // - maximum flow value
  15. // - To obtain the actual flow values, look at all edges with
  16. // capacity > 0 (zero capacity edges are residual edges).
  17. struct Edge {
  18. int from, to, cap, flow, index;
  19. Edge(int from, int to, int cap, int flow, int index) :
  20. from(from), to(to), cap(cap), flow(flow), index(index) {}
  21. };
  22. struct PushRelabel {
  23. int N;
  24. vector<vector<Edge> > G;
  25. vector<LL> excess;
  26. vector<int> dist, active, count;
  27. queue<int> Q;
  28. PushRelabel(int N) : N(N), G(N), excess(N), dist(N), active(N), count(2*N) {}
  29. void AddEdge(int from, int to, int cap) {
  30. G[from].push_back(Edge(from, to, cap, 0, G[to].size()));
  31. if (from == to) G[from].back().index++;
  32. G[to].push_back(Edge(to, from, 0, 0, G[from].size() - 1));
  33. }
  34. void Enqueue(int v) {
  35. if (!active[v] && excess[v] > 0) { active[v] = true; Q.push(v); }
  36. }
  37. void Push(Edge &e) {
  38. int amt = int(min(excess[e.from], LL(e.cap - e.flow)));
  39. if (dist[e.from] <= dist[e.to] || amt == 0) return;
  40. e.flow += amt;
  41. G[e.to][e.index].flow -= amt;
  42. excess[e.to] += amt;
  43. excess[e.from] -= amt;
  44. Enqueue(e.to);
  45. }
  46. void Gap(int k) {
  47. for (int v = 0; v < N; v++) {
  48. if (dist[v] < k) continue;
  49. count[dist[v]]--;
  50. dist[v] = max(dist[v], N+1);
  51. count[dist[v]]++;
  52. Enqueue(v);
  53. }
  54. }
  55. void Relabel(int v) {
  56. count[dist[v]]--;
  57. dist[v] = 2*N;
  58. for (int i = 0; i < G[v].size(); i++)
  59. if (G[v][i].cap - G[v][i].flow > 0)
  60. dist[v] = min(dist[v], dist[G[v][i].to] + 1);
  61. count[dist[v]]++;
  62. Enqueue(v);
  63. }
  64. void Discharge(int v) {
  65. for (int i = 0; excess[v] > 0 && i < G[v].size(); i++) Push(G[v][i]);
  66. if (excess[v] > 0) {
  67. if (count[dist[v]] == 1)
  68. Gap(dist[v]);
  69. else
  70. Relabel(v);
  71. }
  72. }
  73. LL GetMaxFlow(int s, int t) {
  74. count[0] = N-1;
  75. count[N] = 1;
  76. dist[s] = N;
  77. active[s] = active[t] = true;
  78. for (int i = 0; i < G[s].size(); i++) {
  79. excess[s] += G[s][i].cap;
  80. Push(G[s][i]);
  81. }
  82. while (!Q.empty()) {
  83. int v = Q.front();
  84. Q.pop();
  85. active[v] = false;
  86. Discharge(v);
  87. }
  88. LL totflow = 0;
  89. for (int i = 0; i < G[s].size(); i++) totflow += G[s][i].flow;
  90. return totflow;
  91. }
  92. };