keymasterviriya1150

Untitled

Apr 5th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <algorithm>
  5. #include <climits>
  6. #include <cstdlib>
  7. #include <cstring>
  8.  
  9. using namespace std;
  10.  
  11. typedef pair<int, int> ii; // weight and Node
  12. typedef vector<ii> vii; //agjlist
  13.  
  14. int maxFlow(vector<vii> AdjList, int s, int t);
  15.  
  16. int main()
  17. {
  18.     vector<vii> AdjList;
  19.    
  20.     int V, s, t;
  21.     cout<<"vertex=";
  22.     cin >> V ;
  23.     cout<<"Sort Node";
  24.     cin >> s ;
  25.     cout<<"Sing Node";
  26.     cin >> t;
  27.    
  28.     AdjList.assign(V, vii());
  29.        
  30.    
  31.     for (int u = 0; u < V; u++)
  32.     {
  33.        
  34.         int K;
  35.         cout<<"K(Line)=";
  36.         cin >> K ;
  37.        
  38.         for (int k = 0; k < K; k++)
  39.         {
  40.             int v, w;
  41.             cout<<"vertex="
  42.             cin >> v
  43.             cout>>"Weight";
  44.             cin >> w;
  45.            
  46.             AdjList[u].push_back(make_pair(w, v));
  47.         }
  48.     }
  49.     // input Node S T
  50.    
  51.     cout << "Max flow is " << maxFlow(AdjList, s, t) << "\n";
  52.    
  53.     return 0;
  54. }
  55.  
  56. int maxFlow(vector<vii> AdjList, int s, int t) //graph S T
  57. {
  58.     int maxflow = 0;
  59.     int V = AdjList.size(); //Node number
  60.     int residual[V][V]; //maxtirx v*v
  61.    
  62.     // Create residual matrix
  63.     memset(residual, 0, sizeof residual); // all element  at 0
  64.     for (int u = 0; u < V; u++)
  65.     {
  66.         //
  67.         for (size_t k = 0; k < AdjList[u].size(); k++) //size_t = int
  68.         {
  69.             int v = AdjList[u][k].second;
  70.             int w = AdjList[u][k].first;
  71.            
  72.             residual[u][v] = w; // U -> V=W
  73.         }
  74.     }
  75.    
  76.     // Edmonds-Karp
  77.     while(1)
  78.     {
  79.         //BFS
  80.         vector<bool> visited; // Give Node go
  81.         vector<int> parent; // Go Node streen
  82.         vector<int> bottleneck; // result of Go Node streen
  83.        
  84.         visited.assign(V, false);// defaut dont go  false
  85.         parent.assign(V, -1); //No go dont parent
  86.         bottleneck.assign(V, INT_MAX); // INF -> min
  87.         //----------------------------------------------------------------//
  88.         // BFS + save path + propagate bottleneck
  89.         queue<int> q; // open give Node
  90.         q.push(s);
  91.         visited[s] = true;
  92.        
  93.         while(!q.empty())//cheak  true emty
  94.         {
  95.             int u = q.front(); // Node u pop u
  96.             q.pop();
  97.            
  98.             if (u == t) break; // found the sink node // T node Stop
  99.            
  100.             for (size_t k = 0; k < AdjList[u].size(); k++)//Node shit u
  101.             {
  102.                 int v = AdjList[u][k].second;
  103.                 int availableFlow = residual[u][v];
  104.                
  105.                 if ((availableFlow > 0) && (!visited[v]))  
  106.                 {
  107.                     visited[v] = true;
  108.                     parent[v] = u;
  109.                     bottleneck[v] = min(bottleneck[u], availableFlow);
  110.                     //
  111.                     q.push(v);
  112.                 }
  113.             }
  114.         }
  115.        
  116.         // No more way // T
  117.         if (!visited[t] || bottleneck[t] == 0)
  118.             break;
  119.        
  120.         // propagate bottleneck
  121.         int k = t;
  122.         while (parent[k] != -1)
  123.         {
  124.             cout << k << " <- ";
  125.             residual[parent[k]][k] -= bottleneck[t];
  126.             residual[k][parent[k]] += bottleneck[t];
  127.            
  128.             k = parent[k];
  129.         }
  130.         cout << " Bottleneck = " << bottleneck[t] << "\n";
  131.        
  132.         // accumulate flow
  133.         maxflow += bottleneck[t];
  134.     }
  135.    
  136.     return maxflow;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment