Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. //Mr.Fiskerton//
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. //*
  6. ifstream  in("maxflow.in");
  7. ofstream out("maxflow.out");
  8. //*/
  9. /*
  10. ifstream  in("input.txt");
  11. ofstream out("output.txt");
  12. ofstream deb("debug.txt");
  13. //*/
  14.  
  15. struct edge {
  16.     int u, v;
  17.     long long cost, flow;
  18. };
  19.  
  20. const int MAX_N = 105;
  21. const long long INF = int(1e5) + 10;
  22. int n, m,
  23.     s, t,
  24.     mQueue[MAX_N], qHead, qTail,
  25.     deep[MAX_N], lastAccess[MAX_N];
  26. std::vector <int> G[MAX_N];
  27. std::vector <edge> E;
  28.  
  29. void input() {
  30.     in>>n>>m;
  31.  
  32.     edge temp = {0, 0, 0, 0};
  33.     for (int i = 0; i < m; i++) {
  34.         in>>temp.u>>temp.v>>temp.cost;
  35.         temp.u--; temp.v--;
  36.  
  37.         G[temp.u].push_back ((int) E.size()); //ptr on edge
  38.         E.push_back(temp);
  39.         temp.cost = 0;
  40.         G[temp.v].push_back ((int) E.size()); //ptr on edge
  41.         E.push_back(temp);
  42.     }
  43.     s = 1 - 1; t = n - 1;
  44. }
  45.  
  46. bool bfs() {
  47.     qHead = 0; qTail = 0;
  48.     mQueue[qTail++] = s;
  49.  
  50.     memset (deep, -1, n * sizeof deep[0]);
  51.     deep[s] = 0;
  52.  
  53.     int from, to, idEdge;
  54.     while (qHead < qTail && deep[t] == -1) {
  55.         from = mQueue[qHead++];
  56.         for (int i = 0; i < G[from].size(); ++i) {
  57.             idEdge = G[from][i];
  58.             to = E[idEdge].v;
  59.             if (deep[to] == -1 && E[idEdge].flow < E[idEdge].cost) {
  60.                 mQueue[qTail++] = to;
  61.                 deep[to] = deep[from] + 1;
  62.             }
  63.         }
  64.     }
  65.     return deep[t] != -1;
  66. }
  67.  
  68. long long dfs (int from, long long flow) {
  69.     if (!flow || from == t) { return flow;}
  70.  
  71.     int idEdge, to;
  72.     long long pushed;
  73.     for (; lastAccess[from] < (int)G[from].size(); ++lastAccess[from]) {
  74.         idEdge = G[from][ lastAccess[from] ];
  75.         to = E[idEdge].v;
  76.  
  77.         if (deep[to] != deep[from] + 1) { continue; }
  78.  
  79.         pushed = dfs (to, min (flow, E[idEdge].cost - E[idEdge].flow));
  80.         if (pushed) {
  81.             E[idEdge].flow += pushed;
  82.             E[idEdge^1].flow -= pushed;
  83.             return pushed;
  84.         }
  85.     }
  86.     return 0;
  87. }
  88.  
  89. long long solve() {
  90.     long long maxflow = 0, pushed;
  91.  
  92.     while (bfs()) {
  93.         memset (lastAccess, 0, n * sizeof lastAccess[0]);
  94.         while (pushed = dfs (s, INF)) { maxflow += pushed; }
  95.     }
  96.  
  97.     return maxflow;
  98. }
  99.  
  100. int main() {
  101.     input();
  102.     out<<solve();
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement