Advertisement
dipBRUR

Dinic - Max Flow

Oct 4th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.18 KB | None | 0 0
  1. #define FAST      ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
  2. #define ll        long long
  3. #define inf       12345678
  4.  
  5. static const int MAXN = 100 + 5;
  6.  
  7. vector <int> graph[MAXN];
  8. int tNode, tEdge;
  9. ll cap[MAXN][MAXN];   // capacity of edge
  10. ll flow[MAXN][MAXN];  // total sending flow
  11. int level[MAXN];      // level of i'th node
  12.  
  13. int in(int x)
  14. {
  15.     return (x-1)*2;
  16. }
  17.  
  18. int out(int x)
  19. {
  20.     return in(x) + 1;
  21. }
  22.  
  23.  
  24.  
  25. bool bfs(int source, int sink)
  26. {
  27.     fill(begin(level), end(level), -1);
  28.     queue <int> PQ;
  29.     PQ.push(source);
  30.     level[source] = 0;
  31.     while (!PQ.empty())
  32.     {
  33.         int u = PQ.front(); PQ.pop();
  34.         for (int &v : graph[u])
  35.         {
  36.             if (level[v] != -1 || flow[u][v] >= cap[u][v])
  37.                 continue;
  38.             level[v] = level[u] + 1;
  39.             PQ.push(v);
  40.         }
  41.     }
  42.     return level[sink] != -1;
  43. }
  44. ll dfs(int u, ll minCap, int sink)
  45. {
  46.     if (u == sink)
  47.         return minCap;
  48.     for (int &v : graph[u])
  49.     {
  50.         if (level[v] == level[u] + 1 && flow[u][v] < cap[u][v])
  51.         {
  52.             ll minFlow = dfs(v, min(minCap, cap[u][v] - flow[u][v]), sink);
  53.             if (minFlow > 0)
  54.             {
  55.                 flow[u][v] += minFlow;
  56.                 flow[v][u] -= minFlow;
  57.                 return minFlow;
  58.             }
  59.         }
  60.     }
  61.     return 0;
  62. }
  63. ll DinicMaxFlow(int source, int sink)
  64. {
  65.     int maxFlow = 0;
  66.     while (bfs(source, sink))
  67.     {
  68.         while (ll bottleneck = dfs(source, inf, sink))
  69.         {
  70.             maxFlow += bottleneck;
  71.         }
  72.     }
  73.     return maxFlow;
  74. }
  75. void set_capacity(int u, int v, ll cost)
  76. {
  77.     cap[u][v] = cost;
  78.     cap[v][u] = cost;
  79. }
  80. void add_edge(int u, int v)
  81. {
  82.     graph[u].push_back(v);
  83.     graph[v].push_back(u);
  84. }
  85. // for directed graph   -> cap[u][v] = c, flow[u][v] = 0
  86. //                         cap[v][u] = 0, flow[v][u] = 0
  87. // for undirected graph -> cap[u][v] = c, flow[u][v] = 0
  88. //                         cap[v][u] = c, flow[v][u] = 0
  89. int main()
  90. {
  91.     FAST;
  92.     while (cin >> tNode >> tEdge)
  93.     {
  94.         if (tNode+tEdge == 0)
  95.             break;
  96.         int u = 1;
  97.         int v = tNode;
  98.        
  99.         add_edge(in(u), out(u));
  100.         set_capacity(in(u), out(u), inf);
  101.        
  102.         add_edge(in(v), out(v));
  103.         set_capacity(in(v), out(v), inf);
  104.        
  105.         for (int e = 0; e < tNode-2; e++)
  106.         {
  107.             int i;
  108.             ll c;
  109.             cin >> i >> c; // node and its capacity
  110.             add_edge(in(i), out(i));
  111.             set_capacity(in(i), out(i), c);
  112.         }
  113.         for (int e = 0; e < tEdge; e++)
  114.         {
  115.             int j, k;
  116.             ll d;
  117.             cin >> j >> k >> d;  // edge and its capacity
  118.             add_edge(out(j), in(k));
  119.             cap[out(j)][in(k)] = d;
  120.             add_edge(out(k), in(j));
  121.             cap[out(k)][in(j)] = d;
  122.         }
  123.         int source = out(1);
  124.         int sink = in(tNode);
  125.         ll ans = DinicMaxFlow(source, sink);
  126.         cout << ans << endl;
  127.         memset(cap, 0, sizeof cap);
  128.         memset(flow, 0, sizeof flow);
  129.         for (int i = 0; i < MAXN; i++)
  130.             graph[i].clear();
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement