Alex_tz307

Dinic

Oct 15th, 2020 (edited)
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. ifstream fin("maxflow.in");
  7. ofstream fout("maxflow.out");
  8.  
  9. class FlowEdge {
  10.     public:
  11.         int u, v, capacity;
  12.  
  13.         FlowEdge(int u, int v, int capacity) {
  14.             this -> u = u;
  15.             this -> v = v;
  16.             this -> capacity = capacity;
  17.         }
  18. };
  19.  
  20. class Dinic {
  21.     public:
  22.         const int INF = 0x3f3f3f3f;
  23.         vector < FlowEdge > edges;
  24.         vector < vector < int > > adj;
  25.         int N, M = 0, source, sink;
  26.         vector < int > level, ptr;
  27.         queue < int > Q;
  28.  
  29.         Dinic(int N, int source, int sink) {
  30.             this -> N = N;
  31.             this -> source = source;
  32.             this -> sink = sink;
  33.             adj.resize(N + 1);
  34.             level.resize(N + 1);
  35.             ptr.resize(N + 1);
  36.         }
  37.  
  38.         void add_edge(int u, int v, int capacity) {
  39.             edges.emplace_back(u, v, capacity);
  40.             edges.emplace_back(v, u, 0);
  41.             adj[u].emplace_back(M++);
  42.             adj[v].emplace_back(M++);
  43.         }
  44.  
  45.         bool BFS() {
  46.             while(!Q.empty()) {
  47.                 int curr = Q.front();
  48.                 Q.pop();
  49.                 for(int id : adj[curr])
  50.                     if(level[edges[id].v] == -1 && edges[id].capacity) {
  51.                         level[edges[id].v] = level[curr] + 1;
  52.                         Q.emplace(edges[id].v);
  53.                     }
  54.             }
  55.             return level[sink] != -1;
  56.         }
  57.  
  58.         int DFS(int u, int curr_flow) {
  59.             if(curr_flow == 0)
  60.                 return 0;
  61.             if(u == sink)
  62.                 return curr_flow;
  63.             for(int& p = ptr[u]; p < (int)adj[u].size(); ++p) {
  64.                 int id = adj[u][p],
  65.                     v = edges[id].v;
  66.                 if(level[u] + 1 != level[v] || edges[id].capacity <= 0)
  67.                     continue;
  68.                 int new_flow = DFS(v, min(curr_flow, edges[id].capacity));
  69.                 if(new_flow == 0)
  70.                     continue;
  71.                 edges[id].capacity -= new_flow;
  72.                 edges[id ^ 1].capacity += new_flow;
  73.                 return new_flow;
  74.             }
  75.             return 0;
  76.         }
  77.  
  78.         int max_flow() {
  79.             int flow_max = 0;
  80.             while(true) {
  81.                 fill(level.begin(), level.end(), -1);
  82.                 level[source] = 0;
  83.                 Q.emplace(source);
  84.                 if(!BFS())
  85.                     break;
  86.                 fill(ptr.begin(), ptr.end(), 0);
  87.                 while(int curr_flow = DFS(source, INF))
  88.                     flow_max += curr_flow;
  89.             }
  90.             return flow_max;
  91.         }
  92. };
  93.  
  94. int main() {
  95.     fin.sync_with_stdio(false);
  96.     fout.sync_with_stdio(false);
  97.     fin.tie(nullptr);
  98.     fout.tie(nullptr);
  99.     int N, M;
  100.     fin >> N >> M;
  101.     Dinic Graph(N, 1, N);
  102.     while(M--) {
  103.         int u, v, capacity;
  104.         fin >> u >> v >> capacity;
  105.         Graph.add_edge(u, v, capacity);
  106.     }
  107.     fout << Graph.max_flow();
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment