Advertisement
Josif_tepe

Untitled

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