Matrix_code

flow - Mincost MaxFlow

Apr 25th, 2016 (edited)
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxn = 1001;
  5. typedef long long T;
  6. struct Edge {
  7.     int  u , v ;
  8.     T cap ,flow , cost;
  9. };
  10.  
  11. struct MCMF {
  12.     int n,m,s,t;      // Total Number of Node Including S,T
  13.     vector<int> G[maxn];     //Graph
  14.     vector<Edge> E;         // EdgeList
  15.     T d[maxn];             // Distance Array of BeTmanFord
  16.     bool inq[maxn];           // Is node in queue
  17.     int p[maxn];
  18.     T a[maxn];
  19.     const int oo = 1e9+9;
  20.     void init(int n){
  21.         this->n =n;
  22.         for(int i = 0; i <= n ; i ++ ) G[i].clear();
  23.         E.clear();
  24.     }
  25.    
  26.     void addEdge( int u , int v , T cap , T cost ){
  27.         E.push_back({u, v , cap , 0 , cost});       // Positive Cost
  28.         E.push_back({v,u , 0, 0 , -cost } );        // Negative Cost
  29.         m = (int) E.size();
  30.         G[u].push_back(m - 2);
  31.         G[v].push_back(m - 1);
  32.        
  33.     }
  34.    
  35.     bool BelmanFord(T &flow , T &cost ) {
  36.        
  37.         for(int i = 0; i < n ;i  ++) d[i] = oo ;
  38.         memset(inq, 0, sizeof(inq));
  39.         d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = oo ;
  40.        
  41.         queue<int> Q;
  42.         Q.push(s);
  43.         while(!Q.empty() ) {
  44.             int u = Q.front(); Q.pop();
  45.             inq[u] = 0;
  46.            
  47.             for( int i = 0 ; i < G[u].size() ; i ++ ) {
  48.                 Edge &e = E[G[u][i]];
  49.                 if( e.cap > e.flow && d[e.v] > d[u] + e.cost ) {
  50.                     d[e.v] = d[u] + e.cost;
  51.                     p[e.v] = G[u][i];
  52.                     a[e.v] = min( a[u] , e.cap - e.flow );
  53.                    
  54.                     if( inq[e.v ] == 0 ) {
  55.                         Q.push(e.v);
  56.                         inq[e.v] = 1;
  57.                     }
  58.                 }
  59.             }
  60.         }
  61.        
  62.         if( d[t] == oo ) return false;     // No augmenting Path
  63.         flow = a[t];
  64.         cost = d[t] ;               // Unit cost
  65.         int u = t ;
  66.         while( u  != s ) {
  67.             E[p[u]].flow += a[t];
  68.             E[p[u]^1].flow -= a[t];
  69.             u = E[p[u]].u;
  70.            
  71.         }
  72.        
  73.         return true;
  74.     }
  75.    
  76.     pair<T,T> Mincost(  int s, int  t ){
  77.         this->s=s,this->t=t;
  78.         T Mcost = 0;
  79.         T Flow = 0;
  80.         T f = 0 ;    // For Each CaT , The flow
  81.         T d = 0;      // Shortest Distance / Cost Per Flow
  82.        
  83.         while( BelmanFord( f, d) ) {
  84.             Flow += f;
  85.             Mcost += f *d ;
  86.         }
  87.        
  88.         return make_pair(Flow, Mcost);
  89.        
  90.     }
  91. } maxf;
Add Comment
Please, Sign In to add comment