NP-Nidzo

Edmond-Karp Algorithm

Aug 10th, 2020
1,048
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1.  
  2.  
  3. /**------------------------
  4.     Author : NikolaP
  5.     Compiler : C++
  6. -------------------------*/
  7.  
  8. //{         Setup
  9.  
  10.     #include <bits/stdc++.h>
  11.     using namespace std;
  12.  
  13.     typedef long long int ll;
  14.     typedef long double ld;
  15.     typedef vector<int> vi;
  16.  
  17.     const int inf = INT_MAX;
  18.     const bool debug = 0;
  19.  
  20. //}
  21.  
  22. struct EdmondKarp{
  23.     int n;
  24.     vector<vi> capacity;
  25.     vector<vi> flowpassed;
  26.     vi parents;
  27.     vi currentpathcapacity;
  28.     vector<vi> graph;
  29.  
  30.     EdmondKarp(int _n){
  31.         n = _n;
  32.         capacity.assign(n,vi(n,0));
  33.         flowpassed.assign(n,vi(n,0));
  34.         parents.assign(n,-1);
  35.         currentpathcapacity.assign(n,0);
  36.         graph.resize(n);
  37.     }
  38.  
  39.     void InputGraph(int edges){
  40.         int from, to, c;
  41.         for(int i = 0; i < edges; ++i){
  42.             cin >> from >> to >> c;
  43.             capacity[from][to] = c;
  44.             graph[from].push_back(to);
  45.             graph[to].push_back(from);
  46.         }
  47.     }
  48.  
  49.     int bfs(int start, int end){
  50.         parents.assign(n,-1);
  51.         currentpathcapacity.assign(n,0);
  52.  
  53.         queue<int> q;
  54.         q.push(start);
  55.  
  56.         parents[start] = -2;
  57.         currentpathcapacity[start] = inf;
  58.  
  59.         while(!q.empty()){
  60.             int current = q.front();
  61.             q.pop();
  62.  
  63.             for(int i = 0; i < graph[current].size(); ++i){
  64.                 int to = graph[current][i];
  65.  
  66.                 if(parents[to] == -1){
  67.                     if(capacity[current][to] - flowpassed[current][to] > 0){
  68.                         parents[to] = current;
  69.                         currentpathcapacity[to] = min(currentpathcapacity[current] , capacity[current][to] - flowpassed[current][to]);
  70.  
  71.                         if(to == end) return currentpathcapacity[end];
  72.  
  73.                         q.push(to);
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.  
  79.         return 0;
  80.     }
  81.  
  82.     int Solution(int start, int end){
  83.         int maxflow = 0;
  84.  
  85.         for(;;){
  86.             int flow = bfs(start,end);
  87.             if(flow == 0) break;
  88.  
  89.             maxflow += flow;
  90.  
  91.             int current = end;
  92.  
  93.             while(current != start){
  94.                 int previous = parents[current];
  95.                 flowpassed[previous][current] += flow;
  96.                 flowpassed[current][previous] -= flow;
  97.                 current = previous;
  98.             }
  99.         }
  100.  
  101.         return maxflow;
  102.     }
  103.  
  104. };
  105. int main()
  106. {
  107.     ios::sync_with_stdio(false);
  108.     cin.tie(0);
  109.     cout.precision(8);
  110.     //cout << fixed;
  111.  
  112.     int n,m;
  113.     cin >> n >> m;
  114.  
  115.     EdmondKarp ek = EdmondKarp(n);
  116.  
  117.     int start,end;
  118.     cin >> start >> end;
  119.  
  120.     ek.InputGraph(m);
  121.  
  122.     cout << ek.Solution(start,end);
  123.     return 0;
  124. }
  125.  
  126.  
  127.  
  128.  
Advertisement
Add Comment
Please, Sign In to add comment