Advertisement
a53

Apa

a53
Mar 21st, 2022
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <queue>
  4.  
  5. std::ifstream fin("apa.in");
  6. std::ofstream fout("apa.out");
  7.  
  8. typedef std::vector<std::vector<int>> mat;
  9.  
  10. int n, m, flow;
  11. mat V, C;
  12. std::vector<int> root;
  13.  
  14. inline void read()
  15. {
  16. int i, j, c;
  17.  
  18. fin >> n;
  19.  
  20. V.resize(1LL * n + 1);
  21. root.resize(1LL * n + 1);
  22. C.resize(1LL * n + 1);
  23.  
  24. for (int i = 1; i <= n; ++i)
  25. C[i].resize(1LL * n + 1);
  26.  
  27. while (fin >> i >> j >> c)
  28. {
  29. V[i].push_back(j);
  30. V[j].push_back(i);
  31. C[i][j] = c;
  32. }
  33. }
  34.  
  35. inline bool bfs(int k)
  36. {
  37. std::queue<int> Q;
  38.  
  39. Q.push(k);
  40. fill(root.begin(), root.end(), 0);
  41.  
  42. while (!Q.empty())
  43. {
  44. k = Q.front(), Q.pop();
  45.  
  46. for (auto const& i : V[k])
  47. if (!root[i] && C[k][i] > 0)
  48. {
  49. root[i] = k;
  50. Q.push(i);
  51.  
  52. if (i == n)
  53. return true;
  54. }
  55. }
  56.  
  57. return false;
  58. }
  59.  
  60. inline void search(int const& start)
  61. {
  62. int cmin, i;
  63.  
  64. while (bfs(start))
  65. {
  66. cmin = 2e9, i = n;
  67.  
  68. while (i != start)
  69. {
  70. if (C[root[i]][i] < cmin)
  71. cmin = C[root[i]][i];
  72. i = root[i];
  73. }
  74.  
  75. flow += cmin;
  76.  
  77. i = n;
  78.  
  79. while (i != start)
  80. {
  81. C[root[i]][i] -= cmin;
  82. C[i][root[i]] += cmin;
  83. i = root[i];
  84. }
  85. }
  86. }
  87.  
  88. inline void display()
  89. {
  90. if (flow) fout << flow << '\n';
  91. else fout << "Apa nu ajunge!";
  92. }
  93.  
  94. int main()
  95. {
  96. read();
  97. search(1);
  98. display();
  99. return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement