Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <queue>
  4. #include <fstream>
  5.  
  6. const int MAXN = 1024;
  7. const long long INF = 1e9;
  8.  
  9. struct edge {
  10. int from, to, capacity, last;
  11. };
  12.  
  13. int last[MAXN], level[MAXN];
  14. edge edges[10 * MAXN];
  15.  
  16. long long dfs(int s, int t, int currcap, int copylast[])
  17. {
  18. //std::cout << s << ' ' << currcap << '\n';
  19. if(s == t)
  20. {
  21. return currcap;
  22. }
  23. if(copylast[s] == -1)
  24. {
  25. return 0;
  26. }
  27.  
  28. long long ans = 0, ind = copylast[s];
  29. auto edg = edges[copylast[s]];
  30. bool is = false;
  31.  
  32. while(true)
  33. {
  34. if(!currcap)
  35. {
  36. break;
  37. }
  38. if(level[edg.to] == level[edg.from] + 1)
  39. {
  40. int currv = dfs(edg.to, t, std::min(currcap, edg.capacity), copylast);
  41. if(currv != 0)
  42. {
  43. is = true;
  44. ans += currv;
  45. edges[ind].capacity -= currv;
  46. edges[ind ^ 1].capacity += currv;
  47. currcap -= currv;
  48. }
  49. }
  50.  
  51. if(!is)
  52. {
  53. copylast[edg.from] = ind;
  54. }
  55.  
  56. if(edg.last == -1)
  57. {
  58. break;
  59. }
  60. ind = edg.last;
  61. edg = edges[edg.last];
  62. }
  63.  
  64. return ans;
  65. }
  66.  
  67. long long bfs(int s, int t)
  68. {
  69. memset(level, -1, sizeof(level));
  70. std::queue<int> k;
  71. int copylast[MAXN];
  72. for(int i = 0; i < MAXN; ++ i)
  73. {
  74. copylast[i] = last[i];
  75. }
  76. k.push(s);
  77. level[s] = 0;
  78.  
  79. while(!k.empty())
  80. {
  81. int curr = k.front();
  82. k.pop();
  83.  
  84. if(curr == t)
  85. {
  86. break;
  87. }
  88.  
  89. auto edg = edges[last[curr]];
  90.  
  91. while(true)
  92. {
  93. if(edg.capacity != 0)
  94. {
  95. if(level[edg.to] == -1)
  96. {
  97. k.push(edg.to);
  98. level[edg.to] = level[edg.from] + 1;
  99. }
  100. if(level[edg.to] > level[edg.from] + 1)
  101. {
  102. level[edg.to] = level[edg.from] + 1;
  103. }
  104. }
  105. if(edg.last == -1)
  106. {
  107. break;
  108. }
  109. edg = edges[edg.last];
  110. }
  111. }
  112.  
  113. /*for(int i = 1; i <= t; ++ i)
  114. {
  115. std::cout << level[i] << ' ';
  116. }std::cout << '\n';*/
  117.  
  118. return dfs(s, t, INF, copylast);
  119. }
  120.  
  121. int main()
  122. {
  123. #define cout myfileOut
  124. std::ofstream myfileOut;
  125. myfileOut.open ("maxflow.out");
  126.  
  127. #define cin myFileIn
  128. std::ifstream myFileIn;
  129. myFileIn.open ("maxflow.in");
  130.  
  131. memset(last, -1, sizeof(last));
  132. int n, m;
  133. cin >> n >> m;
  134.  
  135. for(int i = 0; i < m; ++ i)
  136. {
  137. int u, v, c;
  138. cin >> u >> v >> c;
  139.  
  140. edges[i * 2] = {u, v, c, last[u]};
  141. last[u] = i * 2;
  142. edges[i * 2 + 1] = {v, u, 0, last[v]};
  143. last[v] = i * 2 + 1;
  144. }
  145.  
  146. /*for(int i = 0; i < 2 * m; ++ i)
  147. {
  148. std::cout << edges[i].from << ' ' << edges[i].to << ' ' << edges[i].capacity << ' ' << edges[i].last << '\n';
  149. }*/
  150.  
  151. long long ans = 0;
  152.  
  153. while(true)
  154. {
  155. long long curr = bfs(1, n);
  156. if(curr)
  157. {
  158. ans += curr;
  159. }else
  160. {
  161. cout << ans << '\n';
  162. return 0;
  163. }
  164. }
  165.  
  166. return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement