Advertisement
LinKin

All pairs max flow

Oct 25th, 2012
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. /*
  2.  * All pairs max flow
  3.  * Algorithm : Gusfield's algorithm
  4.  * Order : N^3 + cost of N max flow
  5. */
  6.  
  7. #include<stdio.h>
  8. #include<string.h>
  9. #include<vector>
  10. #include<algorithm>
  11. using namespace std;
  12.  
  13. #define MAX_V
  14. #define MAX_E
  15. #define INF 7777777
  16.  
  17. struct EDGE{
  18.     long v,c;
  19. };
  20.  
  21. long nV;
  22. long SRC,TNK;
  23. long eI;
  24. EDGE Init[MAX_E+7]; // to keep the initial edge cap
  25. EDGE Edge[MAX_E+7]; // edge list
  26. long Next[MAX_E+7]; // next pointer of vertex v
  27. long Last[MAX_V+7]; // last index of adj edge of vertex v
  28. long Dist[MAX_V+7]; // level from src
  29. long Start[MAX_V+7];// temporary used for last
  30.  
  31. long Par[MAX_V+7];
  32. long Flow[MAX_V+7][MAX_V+7]; // to keep the max flow between i th node and j th node
  33.  
  34. inline void SetEdge( long u,long v,long c )
  35. {
  36.     Edge[eI].v = v;
  37.     Edge[eI].c = c;
  38.     Next[eI] = Last[u];
  39.     Last[u] = eI++;
  40.     Edge[eI].v = u;
  41.     Edge[eI].c = 0;
  42.     Next[eI] = Last[v];
  43.     Last[v] = eI++;
  44. }
  45.  
  46. long Q[MAX_V+7];
  47. long Frnt,End;
  48.  
  49. bool Bfs( void )
  50. {
  51.     Frnt = End = 0;
  52.     Q[End++] = SRC;
  53.     long i;
  54.     for( i=0;i<nV;i++){
  55.         Dist[i] = INF;
  56.     }
  57.     Dist[SRC] = 0;
  58.     long u,v;
  59.     while( Frnt<End ){
  60.         u = Q[Frnt++];
  61.         for( i=Last[u];i!=-1;i=Next[i]){
  62.             v = Edge[i].v;
  63.             if( !Edge[i].c || Dist[v]<INF ) continue;
  64.             Dist[v] = Dist[u] + 1;
  65.             Q[End++] = v;
  66.         }
  67.     }
  68.     return Dist[TNK] < INF;
  69. }
  70.  
  71. long AugmentPath( long u,long f )
  72. {
  73.     if( u==TNK ) return f;
  74.     long Tot = 0;
  75.     for( long &i = Start[u];i!=-1;i=Next[i] ){
  76.         long v = Edge[i].v;
  77.         if( !Edge[i].c ) continue;
  78.         if( Dist[v] != Dist[u]+1 ) continue;
  79.         long Tmp = AugmentPath( v,min( f,Edge[i].c ));
  80.         Edge[i].c -= Tmp;
  81.         Edge[i ^ 1].c += Tmp;
  82.         Tot += Tmp;
  83.         f -= Tmp;
  84.         if( !f ) break;
  85.     }
  86.     return Tot;
  87. }
  88.  
  89. long MaxFlow( void )
  90. {
  91.     long Flw = 0;
  92.     while( Bfs()){
  93.         memcpy( Start,Last,(nV)*sizeof(long));
  94.         Flw += AugmentPath( SRC,2*INF );
  95.     }
  96.     return Flw;
  97. }
  98.  
  99. int main( void )
  100. {
  101.     long i,j;
  102.     eI = 0;
  103.     memset( Last,-1,nV*sizeof(long));
  104.     memcpy( Init,Edge,eI*sizeof(EDGE));
  105.     /* set parent of each vertex to 0 for o based indexin
  106.        if 1th based index is used then parent will be 1 */
  107.     memset( Par,0,sizeof(Par));
  108.     /* set Flow to INF for all vertex pair */
  109.     for( i=0;i<nV;i++ ){
  110.         for( j=0;j<nV;j++ ){
  111.             Flow[i][j] = INF;
  112.         }
  113.     }
  114.  
  115.     for( i=1;i<nV;i++ ){
  116.     /* Compute the minimum cut between i and parent[i].
  117.        Let the i-side of the min cut be S, and the value of the min-cut be F */
  118.         SRC = i;
  119.         TNK = Par[i];
  120.         memcpy( Edge,Init,eI*sizeof(EDGE));
  121.         long F = MaxFlow();
  122.         //Bfs();
  123.         for( j=i+1;j<nV;j++ ){
  124.             /*if ((j is in S) && parent[j]==parent[i])
  125.                 parent[j]=i;
  126.             */
  127.             if( Dist[j]<INF && Par[j]==Par[i] ){
  128.                 Par[j] = i;
  129.             }
  130.         }
  131.         Flow[i][Par[i]] = Flow[Par[i]][i] = F;
  132.         for( j=0;j<i;j++ ){
  133.             Flow[i][j] = Flow [j][i] = min( F,Flow[Par[i]][j] );
  134.         }
  135.     }
  136.     /*
  137.      Flow[i][j] with contain all pairs of flow
  138.     */
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement