TrickmanOff

Диниц

Dec 30th, 2019
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <fstream>
  4. #include <vector>
  5. #include <queue>
  6. #include <functional>
  7. #include <set>
  8. #include <map>
  9. #include <math.h>
  10. #include <cmath>
  11. #include <string>
  12. #include <time.h>
  13. #include <random>
  14. #include <unordered_set>
  15. #include <unordered_map>
  16. #include <bitset>
  17. #include <string.h>
  18. #include <stack>
  19. using namespace std;
  20.  
  21. #define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);
  22. #define cin in
  23. #define cout out
  24. #define pii pair<int,int>
  25. #define ll long long
  26. #define db double
  27. #define ld long double
  28. #define uset unordered_set
  29. #define umap unordered_map
  30. #define vec vector
  31. #define ms multiset
  32. #define pb push_back
  33. #define pll pair<ll,ll>
  34. #define pdd pair<ld, ld>
  35. #define pq priority_queue
  36. #define umap unordered_map
  37. #define uset unordered_set
  38. #define pnn pair<Node*, Node*>
  39. #define uid uniform_int_distribution
  40.  
  41. ifstream in("input.txt");
  42. ofstream out("output.txt");
  43.  
  44. const int MAX_N = 500, INF = 2e9;
  45. int n, m, S, T;
  46. vector<int> g[MAX_N];
  47. int C[MAX_N][MAX_N], F[MAX_N][MAX_N], d[MAX_N], it[MAX_N];
  48.  
  49. bool bfs() {
  50.     memset(d, 255, sizeof(d));
  51.    
  52.     queue<int> q;
  53.     d[S] = 0;
  54.     q.push(S);
  55.    
  56.     while(!q.empty()) {
  57.         int v = q.front();
  58.         q.pop();
  59.        
  60.         for(int to: g[v]) {
  61.             if(F[v][to] < C[v][to] && d[to] == -1) {
  62.                 d[to] = d[v] + 1;
  63.                 q.push(to);
  64.             }
  65.         }
  66.     }
  67.    
  68.     return d[T] != -1;
  69. }
  70.  
  71. //block flow
  72. int dfs(int v, int min_C) {
  73.     if(v == T || min_C == 0)
  74.         return min_C;
  75.    
  76.     for(int to = g[v][it[v]]; it[v] < g[v].size(); to = g[v][it[v]]) {
  77.         if(d[to] == d[v] + 1) {
  78.             int next = dfs(to, min(min_C, C[v][to] - F[v][to]));
  79.             if(next != 0) {
  80.                 F[v][to] += next;
  81.                 F[to][v] -= next;
  82.                 return next;
  83.             }
  84.         }
  85.        
  86.         it[v]++;
  87.     }
  88.    
  89.     return 0;
  90. }
  91.  
  92. ll dinic() {
  93.     ll max_flow = 0;
  94.     while(bfs()) {
  95.         memset(it, 0, sizeof(it));
  96.         int flow = dfs(S, INF);
  97.        
  98.         while(flow != 0) {
  99.             max_flow += flow;
  100.             flow = dfs(S, INF);
  101.         }
  102.     }
  103.     return max_flow;
  104. }
  105.  
  106. int main() {
  107.     cin >> n >> m;
  108.     for(int i = 0; i < m; i++) {
  109.         int a, b, c;
  110.         cin >> a >> b >> c;
  111.         a--;b--;
  112.         C[a][b] = c;
  113.         g[a].push_back(b);
  114.         g[b].push_back(a);
  115.     }
  116.    
  117.     S = 0; T = n-1;
  118.    
  119.     cout << dinic();
  120. }
Add Comment
Please, Sign In to add comment