Advertisement
Guest User

Max_Flow V^2*E

a guest
Feb 22nd, 2020
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<queue>
  4. #include<cstring>
  5. #include<fstream>
  6. using namespace std;
  7.  
  8. const int MAX_N = 1024;
  9. const int inf = 989654321;
  10.  
  11. int n, m;
  12. int s, t;
  13. int u, v, c;
  14. int ans;
  15.  
  16. struct edg
  17. {
  18.     int to, c, r;
  19. };
  20.  
  21. vector<edg> sys[MAX_N];
  22.  
  23. int level[MAX_N];
  24. int firstE[MAX_N];
  25.  
  26. int dfs(int curr, int capacity)
  27. {
  28.     if(curr == t){
  29.         return capacity;
  30.     }
  31.  
  32.     int leftcapacity = capacity;
  33.  
  34.     for(int i = firstE[curr]; i < sys[curr].size(); i++){
  35.         if(level[sys[curr][i].to] > level[curr] && sys[curr][i].c > 0){
  36.             int res = dfs(sys[curr][i].to, min(leftcapacity, sys[curr][i].c));
  37.             if(res == 0){
  38.                 firstE[curr] = i+1;
  39.             }else{
  40.                 sys[curr][i].c -= res;
  41.                 sys[sys[curr][i].to][sys[curr][i].r].c += res;
  42.                 leftcapacity -= res;
  43.             }
  44.         }
  45.         if(leftcapacity == 0){break;}
  46.     }
  47.  
  48.     return capacity - leftcapacity;
  49.  
  50. }
  51.  
  52. int bfs()
  53. {
  54.     queue<int> q;
  55.     q.push(s);
  56.     int curr;
  57.     memset(level, -1, sizeof(level));
  58.     level[s] = 0;
  59.  
  60.     while(!q.empty()){
  61.         curr = q.front();
  62.         q.pop();
  63.         if(curr == t){break;}
  64.         for(int i = 0; i < sys[curr].size(); i++){
  65.             if(level[sys[curr][i].to] == -1 && sys[curr][i].c > 0){
  66.                 level[sys[curr][i].to] = level[curr]+1;
  67.                 q.push(sys[curr][i].to);
  68.             }
  69.         }
  70.     }
  71.  
  72.     memset(firstE, 0, sizeof(firstE));
  73.  
  74.     int res = dfs(s, inf);
  75.     return res;
  76.  
  77. }
  78.  
  79. int main(){
  80.  
  81.     ios_base::sync_with_stdio(false);
  82.     cin.tie(nullptr);
  83.  
  84.     /*#define cin myFileIn
  85.     ifstream myFileIn;
  86.     myFileIn.open("maxflow.in");
  87.  
  88.     #define cout myFileOut
  89.     ofstream myFileOut;
  90.     myFileOut.open("maxflow.out");*/
  91.  
  92.     cin >> n >> m;
  93.     s = 0;
  94.     t = n-1;
  95.  
  96.     for(int i = 0; i < m; i++){
  97.         cin >> u >> v >> c;
  98.         u--;
  99.         v--;
  100.         sys[u].push_back({v, c, sys[v].size()});
  101.         sys[v].push_back({u, 0, sys[u].size()-1});
  102.     }
  103.  
  104.     int add = bfs();
  105.     while(add > 0){
  106.         ans += add;
  107.         add = bfs();
  108.     }
  109.  
  110.     cout << ans << endl;
  111.  
  112. return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement