Dinic.cc 2.0 KB

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