Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #include<algorithm>
  2. #include<vector>
  3.  
  4. /*
  5. COPY STUFF BELOW THIS COMMENT
  6. */
  7.  
  8. #pragma region
  9.  
  10. class max_flow_graph_copy {
  11. public:
  12. max_flow_graph_copy(int);
  13. ~max_flow_graph_copy();
  14.  
  15. void reset(); //deletes everything
  16. void resize(int); //deletes everything and resizes the max_flow_graph_copy
  17. void add_edge(int, int, int);
  18. void add_double_edge(int, int, int);
  19. int max_flow(int, int);
  20. int size();
  21. protected:
  22. int *head, *gap, *lvl;
  23. int isize;
  24. struct edge {
  25. int v, c, n;
  26. edge();
  27. edge(int, int, int);
  28. };
  29. std::vector<edge> edges;
  30. private:
  31. int sap(int, int, int, int);
  32. };
  33.  
  34.  
  35. max_flow_graph_copy::max_flow_graph_copy(int size) {
  36. isize = size;
  37. head = new int[size];
  38. gap = new int[size];
  39. lvl = new int[size];
  40. std::fill(head, head + size, -1);
  41. }
  42.  
  43. void max_flow_graph_copy::reset() {
  44. std::fill(head, head + isize, -1);
  45. edges.clear();
  46. }
  47.  
  48. int max_flow_graph_copy::size() {
  49. return isize;
  50. }
  51.  
  52. void max_flow_graph_copy::resize(int size) {
  53. delete[] head;
  54. edges.clear();
  55. isize = size;
  56.  
  57. head = new int[size];
  58. gap = new int[size];
  59. lvl = new int[size];
  60. std::fill(head, head + size, -1);
  61. }
  62.  
  63. void max_flow_graph_copy::add_edge(int u, int v, int c) {
  64. if (u >= isize || v >= isize || c < 0) {
  65. throw std::invalid_argument("invalid argument");
  66. return;
  67. }
  68.  
  69. edges.emplace_back(v, c, head[u]);
  70. head[u] = edges.size() - 1;
  71.  
  72. edges.emplace_back(u, 0, head[v]);
  73. head[v] = edges.size() - 1;
  74. }
  75.  
  76. void max_flow_graph_copy::add_double_edge(int u, int v, int c) {
  77. if (u >= isize || v >= isize || c < 0) {
  78. throw std::invalid_argument("invalid argument");
  79. return;
  80. }
  81.  
  82. edges.emplace_back(v, c, head[u]);
  83. head[u] = edges.size() - 1;
  84.  
  85. edges.emplace_back(u, c, head[v]);
  86. head[v] = edges.size() - 1;
  87. }
  88.  
  89. int max_flow_graph_copy::sap(int pos, int flow, int source, int sink) {
  90. if (pos == sink) {
  91. return flow;
  92. }
  93. int flow_left = flow;
  94. for (int a = head[pos]; a != -1; a = edges[a].n) {
  95. if (edges[a].c && lvl[edges[a].v] + 1 == lvl[pos]) {
  96. int cf = sap(edges[a].v, std::min(edges[a].c, flow_left), source, sink);
  97.  
  98. flow_left -= cf;
  99. edges[a].c -= cf;
  100. edges[a ^ 1].c += cf;
  101.  
  102. if (lvl[source] == isize || flow_left == 0) {
  103. return flow - flow_left;
  104. }
  105. }
  106. }
  107.  
  108.  
  109. if (flow == flow_left) {
  110. gap[lvl[pos]]--;
  111. if (gap[lvl[pos]] == 0) {
  112. lvl[source] = isize;
  113. } else {
  114. int min_child = isize;
  115. for (int a = head[pos]; a != -1; a = edges[a].n) {
  116. if (edges[a].c) {
  117. min_child = std::min(min_child, lvl[edges[a].v]);
  118. }
  119. }
  120. lvl[pos] = min_child + 1;
  121. gap[lvl[pos]]++;
  122. }
  123. }
  124. return flow - flow_left;
  125. }
  126.  
  127. int max_flow_graph_copy::max_flow(int source, int sink) {
  128. std::fill(lvl, gap + isize, 0);
  129. std::fill(lvl, lvl + isize, 0);
  130. gap[0] = isize;
  131.  
  132. int ret = 0;
  133. while (lvl[source] < isize) {
  134. ret += sap(source, std::numeric_limits<int>::max(), source, sink); //idk why this even exists
  135. }
  136. return ret;
  137. }
  138.  
  139. max_flow_graph_copy::~max_flow_graph_copy() {
  140. delete[] head;
  141. delete[] lvl;
  142. delete[] gap;
  143. }
  144.  
  145. max_flow_graph_copy::edge::edge() {}
  146. max_flow_graph_copy::edge::edge(int x, int y, int z) {
  147. v = x;
  148. c = y;
  149. n = z;
  150. }
  151.  
  152.  
  153. #pragma endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement