Advertisement
jbn6972

Untitled

Dec 2nd, 2021
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define vi vector<int>
  5. #define vll vector<long long int>
  6.  
  7. int n, m;
  8.  
  9. bool bfs(int s, int t,vi parent, vector<vector<int>> &residual_graph)
  10. {
  11. vector<bool> visited(n, false);
  12. vi queue;
  13. queue.push_back(s);
  14. visited[s] = true;
  15. parent[s] = -1;
  16.  
  17. while(!queue.empty()){
  18. int u = queue[queue.size()-1];
  19. queue.pop_back();
  20. for(int v=0;v<n;v++){
  21. if(visited[v] == false && residual_graph[u][v]>0){
  22. if(v==t){
  23. parent[v] = u;
  24. return true;
  25. }
  26. queue.push_back(v);
  27. parent[v] = u;
  28. visited[v] = true;
  29. }
  30. }
  31. }
  32. return false;
  33. }
  34.  
  35. int ffs(int s, int t, vi parent, vector<vector<int>> &residual_graph)
  36. {
  37. int max_flow = 0;
  38. while (bfs(s, t,parent,residual_graph))
  39. {
  40. int path_flow = INT_MAX;
  41. int v = t;
  42. while (v != s)
  43. {
  44. int u = parent[v];
  45. path_flow = std::min(path_flow, residual_graph[u][v]);
  46. v = parent[v];
  47. }
  48. v = t;
  49. while (v != s)
  50. {
  51. ll u = parent[v];
  52. residual_graph[u][v] -= path_flow;
  53. residual_graph[v][u] -= path_flow;
  54. v = parent[v];
  55. }
  56. max_flow += path_flow;
  57. }
  58. return max_flow;
  59. }
  60.  
  61. int main()
  62. {
  63. #ifndef ONLINE_JUDGE
  64. freopen("in.txt", "r", stdin);
  65. freopen("out.txt", "w", stdout);
  66. #endif
  67. std::ios::sync_with_stdio(false);
  68. cin >> n >> m;
  69. vector<vector<int>> graph(n, vector<int>(n, 0));
  70.  
  71. int u, v, c;
  72. vi parent(n, -1);
  73. for (int i = 0; i < m; i++)
  74. {
  75. cin >> u >> v >> c;
  76. graph[u - 1][v - 1] = c;
  77. }
  78. vi pairs;
  79. int t, s, d;
  80. cin >> t;
  81.  
  82. for (int i = 0; i < t; i++)
  83. {
  84. cin >> s >> d;
  85. vector<vector<int>> residual_graph = graph;
  86. cout << ffs(s - 1, d - 1, parent, residual_graph);
  87. }
  88. return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement