Advertisement
anoosykh95

Untitled

Jul 22nd, 2016
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #define min(a,b) ((a)<(b)?(a):(b))
  2.  
  3. struct edge
  4. {
  5.         int from , to , cost , flow;
  6.         edge (int f,int t,int c,int flw) : from(f),to(t),cost(c),flow(flw)
  7.         {}
  8. };
  9.  
  10. const int MAX_NODE = 405;
  11. int flow [MAX_NODE];
  12. int cost [MAX_NODE];
  13. int parent [MAX_NODE];
  14.  
  15. #define INF (1e9)
  16. vector < edge > edgeList;
  17.  
  18. int n ; // 3add el nodes
  19.  
  20. inline void bellman (int src,int sink )
  21. {
  22.         int i,j;
  23.         memset ( flow , 0 , (sizeof (int))*n);
  24.         fill(cost,cost+n,INF);
  25.  
  26.         parent [src] = -1;
  27.         flow [src] = INF;
  28.         cost [src] = 0;
  29.  
  30.         for (  i = n-1 ; i > 0 ; i -- )
  31.         {
  32.                 bool f = false;
  33.                 for (  j = 0 ; j < int(edgeList.size()) ; j ++ )
  34.                 {
  35.                         if ( cost[edgeList[j].from] + edgeList[j].cost < cost [edgeList[j].to] && edgeList[j].flow > 0)
  36.                         {
  37.                                 //relax
  38.                                 cost [edgeList[j].to] = cost[edgeList[j].from] + edgeList[j].cost ;
  39.                                 parent[edgeList[j].to] = j;
  40.                                 flow[edgeList[j].to] = min ( flow[edgeList[j].from] , edgeList[j].flow ) ;
  41.                                 f = true;
  42.                         }
  43.                 }
  44.                 if ( !f )
  45.                         break;
  46.         }
  47. }
  48.  
  49. inline void addEdge (int f,int t,int c,int flw)
  50. {
  51.         edge e1( f,t,c,flw);
  52.         edge e2( t,f,-c,0);
  53.  
  54.         edgeList.push_back(e1);
  55.         edgeList.push_back(e2);
  56. }
  57.  
  58.  
  59. //returns (flow,cost)
  60. inline pair<int,int> minCost_maxFlow (int src,int sink)
  61. {
  62.         int totF = 0 ,totC = 0;
  63.  
  64.         if (src==sink)
  65.                 return make_pair(INF,0);
  66.  
  67.         while(1)
  68.         {
  69.                 bellman(src,sink);
  70.                 if ( flow[sink] <= 0 ) break;
  71.  
  72.                 for (int cur  = sink ; cur != src ; cur = edgeList[parent[cur]].from )
  73.                 {
  74.                         int revInd = (parent[cur] & 1) ? parent [cur] -1 : parent [cur] +1 ;
  75.                         edgeList[parent[cur]].flow -= flow[sink];
  76.                         edgeList[revInd].flow += flow[sink];
  77.                         totC += edgeList[parent[cur]].cost * flow[sink];
  78.                 }
  79.                 totF += flow[sink];
  80.         }
  81.         return make_pair(totF,totC);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement