Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <vector>
- #include <queue>
- std::ifstream fin("apa.in");
- std::ofstream fout("apa.out");
- typedef std::vector<std::vector<int>> mat;
- int n, m, flow;
- mat V, C;
- std::vector<int> root;
- inline void read()
- {
- int i, j, c;
- fin >> n;
- V.resize(1LL * n + 1);
- root.resize(1LL * n + 1);
- C.resize(1LL * n + 1);
- for (int i = 1; i <= n; ++i)
- C[i].resize(1LL * n + 1);
- while (fin >> i >> j >> c)
- {
- V[i].push_back(j);
- V[j].push_back(i);
- C[i][j] = c;
- }
- }
- inline bool bfs(int k)
- {
- std::queue<int> Q;
- Q.push(k);
- fill(root.begin(), root.end(), 0);
- while (!Q.empty())
- {
- k = Q.front(), Q.pop();
- for (auto const& i : V[k])
- if (!root[i] && C[k][i] > 0)
- {
- root[i] = k;
- Q.push(i);
- if (i == n)
- return true;
- }
- }
- return false;
- }
- inline void search(int const& start)
- {
- int cmin, i;
- while (bfs(start))
- {
- cmin = 2e9, i = n;
- while (i != start)
- {
- if (C[root[i]][i] < cmin)
- cmin = C[root[i]][i];
- i = root[i];
- }
- flow += cmin;
- i = n;
- while (i != start)
- {
- C[root[i]][i] -= cmin;
- C[i][root[i]] += cmin;
- i = root[i];
- }
- }
- }
- inline void display()
- {
- if (flow) fout << flow << '\n';
- else fout << "Apa nu ajunge!";
- }
- int main()
- {
- read();
- search(1);
- display();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement