Advertisement
Ridwanul_Haque

Max_FLOW

Oct 8th, 2020
1,319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef pair<int, int> PAIR;
  5.  
  6. class Graph
  7. {
  8.     int V,N,m;
  9.     list< pair<int, int> > *adj;
  10.  
  11. public:
  12.     Graph(int V,int m)
  13.     {
  14.         this->V = V;
  15.         this->m = m;
  16.         adj = new list<PAIR> [V];
  17.     }
  18.  
  19.     void setnVertices(int n)
  20.     {
  21.         this->V = V;
  22.     }
  23.  
  24.     void addEdge(int u, int v, int w)
  25.     {
  26.         adj[u].push_back(make_pair(v, w));
  27.         //adj[v].push_back(make_pair(u, w));
  28.     }
  29.  
  30.  
  31.     bool isEdge(int u,int v)
  32.     {
  33.         for(auto it = adj[u].begin(); it!=adj[u].end(); it++)
  34.         {
  35.             if((*it).first == v)
  36.             {
  37.                 return true;
  38.             }
  39.         }
  40.         return false;
  41.     }
  42.  
  43.     int getWeight(int u,int v)
  44.     {
  45.         for(auto it = adj[u].begin(); it!=adj[u].end(); it++)
  46.         {
  47.             if((*it).first == v)
  48.             {
  49.                 return (*it).second;
  50.             }
  51.         }
  52.         return 10001;
  53.     }
  54.  
  55.     bool bfs(int rGraph[V][V], int s, int t, int parent[])
  56. {
  57.  
  58.     bool visited[V];
  59.     memset(visited, 0, sizeof(visited));
  60.  
  61.     queue <int> q;
  62.     q.push(s);
  63.     visited[s] = true;
  64.     parent[s] = -1;
  65.  
  66.  
  67.     while (!q.empty())
  68.     {
  69.         int u = q.front();
  70.         q.pop();
  71.  
  72.         for (int v=0; v<V; v++)
  73.         {
  74.             if (visited[v]==false && rGraph[u][v] > 0)
  75.             {
  76.                 q.push(v);
  77.                 parent[v] = u;
  78.                 visited[v] = true;
  79.             }
  80.         }
  81.     }
  82.  
  83.  
  84.     return (visited[t] == true);
  85. }
  86.  
  87.  
  88. int fordFulkerson(int graph[V][V], int s, int t)
  89. {
  90.     int u, v;
  91.     int rGraph[V][V];
  92.     int flow[V][V];
  93.  
  94.     for (u = 0; u < V; u++)
  95.         for (v = 0; v < V; v++)
  96.         {
  97.             rGraph[u][v] = graph[u][v];
  98.             flow[u][v] = 0;
  99.         }
  100.  
  101.  
  102.     int parent[V];
  103.  
  104.     int max_flow = 0;
  105.  
  106.  
  107.     while (bfs(rGraph, s, t, parent))
  108.     {
  109.  
  110.         int path_flow = INT_MAX;
  111.         for (v=t; v!=s; v=parent[v])
  112.         {
  113.             u = parent[v];
  114.             path_flow = min(path_flow, rGraph[u][v]);
  115.  
  116.         }
  117.  
  118.         for (v=t; v != s; v=parent[v])
  119.         {
  120.             u = parent[v];
  121.             rGraph[u][v] -= path_flow;
  122.             rGraph[v][u] += path_flow;
  123.             flow[u][v]   += path_flow;
  124.         }
  125.  
  126.  
  127.         max_flow += path_flow;
  128.     }
  129.  
  130.  
  131.     for(int i = 0; i < V; i++)
  132.     {
  133.         for(int j = 0; j < V; j++)
  134.         {
  135.             if(graph[i][j]!=0)
  136.             {
  137.                 cout<<i<<" "<<j<<" "<<flow[i][j]<<"/"<<graph[i][j]<<endl;
  138.             }
  139.         }
  140.     }
  141.     return max_flow;
  142. }
  143.  
  144. }; /// END OF GRAPH CLASS
  145.  
  146.  
  147.  
  148.  
  149. int main()
  150. {
  151.     int N,M;
  152.     cin>>N>>M;
  153.     int graph[N][N];
  154.     Graph g(N,M);
  155.     g.setnVertices(N);
  156.  
  157.     for(int i = 0; i<M; i++)
  158.     {
  159.         cin>>u>>v>>w;
  160.         g.addEdge(u,v,w);
  161.         graph[u][v] = w;
  162.     }
  163.  
  164.  
  165.     cout << "The maximum possible flow is " <<g.fordFulkerson(graph, 0, 5);
  166.  
  167.     return 0;
  168. }
  169.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement