Dinic.cc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Adjacency list implementation of Dinic's blocking flow algorithm.
  2. // This is very fast in practice, and only loses to push-relabel flow.
  3. //
  4. // Running time:
  5. // O(|V|^2 |E|)
  6. //
  7. // INPUT:
  8. // - oriented graph, constructed using AddEdge()
  9. // - source
  10. // - sink
  11. //
  12. // OUTPUT:
  13. // - maximum flow value
  14. // - To obtain the actual flow values, look at all edges with
  15. // capacity > 0 (zero capacity edges are residual edges).
  16. struct Edge {
  17. int from, to, cap, flow, index;
  18. Edge(int from, int to, int cap, int flow, int index) :
  19. from(from), to(to), cap(cap), flow(flow), index(index) {}
  20. };
  21. struct Dinic {
  22. int N;
  23. vector<vector<Edge> > G;
  24. vector<Edge *> dad;
  25. vector<int> Q;
  26. Dinic(int N) : N(N), G(N), dad(N), Q(N) {}
  27. void AddEdge(int from, int to, int cap) {
  28. G[from].push_back(Edge(from, to, cap, 0, G[to].size()));
  29. if (from == to) G[from].back().index++;
  30. G[to].push_back(Edge(to, from, 0, 0, G[from].size() - 1));
  31. }
  32. long long BlockingFlow(int s, int t) {
  33. fill(dad.begin(), dad.end(), (Edge *) NULL);
  34. dad[s] = &G[0][0] - 1;
  35. int head = 0, tail = 0;
  36. Q[tail++] = s;
  37. while (head < tail) {
  38. int x = Q[head++];
  39. for (int i = 0; i < G[x].size(); i++) {
  40. Edge &e = G[x][i];
  41. if (!dad[e.to] && e.cap - e.flow > 0) {
  42. dad[e.to] = &G[x][i];
  43. Q[tail++] = e.to;
  44. }
  45. }
  46. }
  47. if (!dad[t]) return 0;
  48. long long totflow = 0;
  49. for (int i = 0; i < G[t].size(); i++) {
  50. Edge *start = &G[G[t][i].to][G[t][i].index];
  51. int amt = INF;
  52. for (Edge *e = start; amt && e != dad[s]; e = dad[e->from]) {
  53. if (!e) { amt = 0; break; }
  54. amt = min(amt, e->cap - e->flow);
  55. }
  56. if (amt == 0) continue;
  57. for (Edge *e = start; amt && e != dad[s]; e = dad[e->from]) {
  58. e->flow += amt;
  59. G[e->to][e->index].flow -= amt;
  60. }
  61. totflow += amt;
  62. }
  63. return totflow;
  64. }
  65. long long GetMaxFlow(int s, int t) {
  66. long long totflow = 0;
  67. while (long long flow = BlockingFlow(s, t))
  68. totflow += flow;
  69. return totflow;
  70. }
  71. };