Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1.  
  2.  
  3.     std::vector<int> parents(g.size());
  4.     std::vector<bool> visited(g.size());
  5.     std::queue<int> que;
  6.  
  7.     int n_vertices = static_cast<int>(ll);
  8.  
  9.     int64_t result = 0;
  10.  
  11.     int source_id = n_vertices * 2;
  12.     int destination_id = n_vertices * 2 + 1;
  13.    
  14.     for (int epoch = 0; epoch < epoch + 1; ++epoch) {
  15.         std::fill(parents.begin(), parents.end(), 0);
  16.         std::fill(visited.begin(), visited.end(), false);
  17.         while (!que.empty()) {
  18.             que.pop();
  19.         }
  20.  
  21.         visited[source_id] = true;
  22.         parents[source_id] = source_id;
  23.         que.push(source_id);
  24.        
  25.         int from;
  26.         bool finish = true;
  27.         while (!que.empty() && finish) {
  28.             from = que.front();
  29.             que.pop();
  30.  
  31.             for (const auto& [to, data] : g[from]) {
  32.                 if (data.first > data.second) {
  33.                     if (!visited[to]) {
  34.                         visited[to] = true;
  35.                         parents[to] = from;
  36.                         que.push(to);
  37.  
  38.                         if (to == destination_id) {
  39.                             finish = false;
  40.                             break;
  41.                         }
  42.                     }
  43.                 }
  44.             }
  45.         }
  46.  
  47.         if (!visited[destination_id]) {
  48.             break;
  49.         }
  50.  
  51.         int64_t delta_flow = std::numeric_limits<int64_t>::max();
  52.         int current_vertex = destination_id;
  53.         while (parents[current_vertex] != current_vertex) {
  54.             int parent = parents[current_vertex];
  55.             auto res = g[parent].find(current_vertex);
  56.             delta_flow = std::min(delta_flow, res->second.first - res->second.second);
  57.             current_vertex = parent;
  58.         }
  59.  
  60.         result += delta_flow;
  61.  
  62.         current_vertex = destination_id;
  63.         while (parents[current_vertex] != current_vertex) {
  64.             int parent = parents[current_vertex];
  65.             g[parent].find(current_vertex)->second.second += delta_flow;
  66.             g[current_vertex].find(parent)->second.second -= delta_flow;
  67.             current_vertex = parent;
  68.         }
  69.     }
  70.  
  71.     std::cout << result << std::endl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement