Advertisement
Josif_tepe

Untitled

Sep 17th, 2024
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 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.  
  23. class FordFulkerson {
  24. private:
  25.     int n;
  26.     vector<vector<int>> capacity;
  27.     Graph adj;
  28. public:
  29.     FordFulkerson(int n, vector<vector<int>> capacity, Graph adj) {
  30.         this->n = n;
  31.         this->capacity = capacity;
  32.         this->adj = adj;
  33.     }
  34.     bool dfs(int S, int E, vector<bool> & visited, vector<int> & path) {
  35.         visited[S] = true;
  36.         path.push_back(S);
  37.        
  38.         if(S == E) {
  39.             return true;
  40.         }
  41.         for(int neighbour : adj.get_neighbours(S)) {
  42.             if(!visited[neighbour] and capacity[S][neighbour] > 0) {
  43.                 if(dfs(neighbour, E, visited, path)) {
  44.                     return true;
  45.                 }
  46.             }
  47.         }
  48.         path.pop_back();
  49.         return false;
  50.     }
  51.     int ford_fulkerson(int S, int E) {
  52.         int flow = 0;
  53.         vector<bool> visited(n, false);
  54.         vector<int> path;
  55.        
  56.         while(true) {
  57.             int path_flow = INF;
  58.             fill(visited.begin(), visited.end(), false);
  59.             path.clear();
  60.             if(!dfs(S, E, visited, path)) {
  61.                 break;
  62.             }
  63.            
  64.            
  65.             for(int i = 0; i < path.size() - 1; i++) {
  66.                 int a = path[i];
  67.                 int b = path[i + 1];
  68.                 path_flow = min(path_flow, capacity[a][b]);
  69.             }
  70.             for(int i = 0; i < path.size() - 1; i++) {
  71.                 int a = path[i];
  72.                 int b = path[i + 1];
  73.                 capacity[a][b] -= path_flow;
  74.                 capacity[b][a] += path_flow;
  75.                
  76.             }
  77.             flow += path_flow;
  78.         }
  79.        
  80.         return flow;
  81.     }
  82. };
  83.  
  84. int main() {
  85.     int V = 6;
  86.     int graph[6][6] = { {0, 16, 13, 0, 0, 0},
  87.                            {0, 0, 10, 12, 0, 0},
  88.                            {0, 4, 0, 0, 14, 0},
  89.                            {0, 0, 9, 0, 0, 20},
  90.                            {0, 0, 0, 7, 0, 4},
  91.                            {0, 0, 0, 0, 0, 0}
  92.                          };
  93.     Graph adj(6);
  94.     vector<vector<int>> capacity(6, vector<int>(6, 0));
  95.     for(int i = 0; i < 6; i++) {
  96.         for(int j = 0; j < 6; j++) {
  97.             capacity[i][j] = graph[i][j];
  98.             if(graph[i][j] != 0) {
  99.                 adj.add_edge(i, j);
  100.             }
  101.         }
  102.     }
  103.     FordFulkerson ff(6, capacity, adj);
  104.     cout << ff.ford_fulkerson(0, 5) << endl;
  105.     return 0;
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement