Advertisement
Josif_tepe

Untitled

Sep 17th, 2024
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4. using namespace std;
  5. const int INF = 1e9;
  6. class Graph {
  7. private:
  8.     vector<vector<int>> adj_list;
  9. public:
  10.     Graph() {}
  11.     Graph(int n) {
  12.         adj_list.resize(n);
  13.     }
  14.     void add_edge(int a, int b) {
  15.         adj_list[a].push_back(b);
  16.     }
  17.     vector<int> get_neighbours(int x) {
  18.         return adj_list[x];
  19.     }
  20.    
  21. };
  22. class EdmnodKarpAlgorithm {
  23. private:
  24.     int n;
  25.     Graph adj;
  26.     vector<vector<int>> capacity;
  27.    
  28. public:
  29.     EdmnodKarpAlgorithm(int n, vector<vector<int>> capacity, Graph adj) {
  30.         this->n = n;
  31.         this->capacity = capacity;
  32.         this->adj = adj;
  33.     }
  34.    
  35.     int bfs(int S, int E, vector<int> & parent) {
  36.         fill(parent.begin(), parent.end(), -1);
  37.         parent[S] = -2;
  38.        
  39.         queue<int> q;
  40.         q.push(S);
  41.         q.push(INF);
  42.        
  43.         while(!q.empty()) {
  44.             int c = q.front();
  45.             q.pop();
  46.             int flow = q.front();
  47.             q.pop();
  48.            
  49.             for(int neigh : adj.get_neighbours(c)) {
  50.                 if(parent[neigh] == -1 and capacity[c][neigh]) {
  51.                     parent[neigh] = c;
  52.                     int new_flow = min(flow, capacity[c][neigh]);
  53.                     if(neigh == E) {
  54.                         return new_flow;
  55.                     }
  56.                     q.push(neigh);
  57.                     q.push(new_flow);
  58.                 }
  59.                
  60.             }
  61.            
  62.         }
  63.         return 0;
  64.     }
  65.    
  66.     int max_flow(int S, int E) {
  67.         int flow = 0;
  68.         vector<int> parent(this->n);
  69.        
  70.         int tmp;
  71.         while(tmp = bfs(S, E, parent)) {
  72.             flow += tmp;
  73.             int b = E;
  74.             while(b != S) {
  75.                 int prev = parent[b];
  76.                 capacity[prev][b] -= tmp;
  77.                 capacity[b][prev] += tmp;
  78.                 b = prev;
  79.             }
  80.         }
  81.         return flow;
  82.     }
  83.    
  84. };
  85.  
  86. int main() {
  87.     int V = 6;
  88.     int graph[6][6] = { {0, 16, 13, 0, 0, 0},
  89.                            {0, 0, 10, 12, 0, 0},
  90.                            {0, 4, 0, 0, 14, 0},
  91.                            {0, 0, 9, 0, 0, 20},
  92.                            {0, 0, 0, 7, 0, 4},
  93.                            {0, 0, 0, 0, 0, 0}
  94.                          };
  95.     Graph adj(6);
  96.     vector<vector<int>> capacity(6, vector<int>(6, 0));
  97.     for(int i = 0; i < 6; i++) {
  98.         for(int j = 0; j < 6; j++) {
  99.             capacity[i][j] = graph[i][j];
  100.             if(graph[i][j] != 0) {
  101.                 adj.add_edge(i, j);
  102.             }
  103.         }
  104.     }
  105.     EdmnodKarpAlgorithm edka(6, capacity, adj);
  106.     cout << edka.max_flow(0, 5) << endl;
  107.     return 0;
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement