Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::vector<int> parents(g.size());
- std::vector<bool> visited(g.size());
- std::queue<int> que;
- int n_vertices = static_cast<int>(ll);
- int64_t result = 0;
- int source_id = n_vertices * 2;
- int destination_id = n_vertices * 2 + 1;
- for (int epoch = 0; epoch < epoch + 1; ++epoch) {
- std::fill(parents.begin(), parents.end(), 0);
- std::fill(visited.begin(), visited.end(), false);
- while (!que.empty()) {
- que.pop();
- }
- visited[source_id] = true;
- parents[source_id] = source_id;
- que.push(source_id);
- int from;
- bool finish = true;
- while (!que.empty() && finish) {
- from = que.front();
- que.pop();
- for (const auto& [to, data] : g[from]) {
- if (data.first > data.second) {
- if (!visited[to]) {
- visited[to] = true;
- parents[to] = from;
- que.push(to);
- if (to == destination_id) {
- finish = false;
- break;
- }
- }
- }
- }
- }
- if (!visited[destination_id]) {
- break;
- }
- int64_t delta_flow = std::numeric_limits<int64_t>::max();
- int current_vertex = destination_id;
- while (parents[current_vertex] != current_vertex) {
- int parent = parents[current_vertex];
- auto res = g[parent].find(current_vertex);
- delta_flow = std::min(delta_flow, res->second.first - res->second.second);
- current_vertex = parent;
- }
- result += delta_flow;
- current_vertex = destination_id;
- while (parents[current_vertex] != current_vertex) {
- int parent = parents[current_vertex];
- g[parent].find(current_vertex)->second.second += delta_flow;
- g[current_vertex].find(parent)->second.second -= delta_flow;
- current_vertex = parent;
- }
- }
- std::cout << result << std::endl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement