Problem p02376 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <fstream>
#include <iostream>
#include <list>
#include <vector>
using namespace std;

void lift(int u, vector<int> &e, vector<list<int>> &nei, vector<vector<int>> &c,
          vector<vector<int>> &f, vector<int> &h, int n) {
  int height = 2 * n;
  for (auto it = nei[u].begin(); it != nei[u].end(); it++)
    if (c[u][*it] > f[u][*it])
      height = min(height, h[*it]);
  h[u] = height + 1;
}

void push(int u, int v, vector<vector<int>> &c, vector<vector<int>> &f,
          vector<int> &e) {
  int d = min(c[u][v] - f[u][v], e[u]);
  f[u][v] += d;
  f[v][u] = -f[u][v];
  e[u] -= d;
  e[v] += d;
}

bool discharge(int u, vector<int> &e, vector<list<int>::iterator> &current,
               vector<list<int>> &nei, vector<vector<int>> &c,
               vector<vector<int>> &f, vector<int> &h, int n) {
  bool lifted = false;

  while (e[u] > 0) {
    if (current[u] == nei[u].end()) {
      lift(u, e, nei, c, f, h, n);
      lifted = true;
      current[u] = nei[u].begin();
      continue;
    }
    int v = *current[u];
    if (c[u][v] > f[u][v] && h[u] == h[v] + 1)
      push(u, v, c, f, e);
    else
      current[u]++;
  }

  return lifted;
}

int ltf(int n, vector<vector<int>> c) {
  int u, v;
  vector<list<int>> nei(n);
  vector<list<int>::iterator> current(n);
  for (u = 0; u < n; ++u) {
    for (v = 0; v < n; ++v)
      if (c[u][v] > 0 || c[v][u] > 0)
        nei[u].push_back(v);
    current[u] = nei[u].begin();
  }

  vector<int> h(n, 0), e(n, 0);
  vector<vector<int>> f(n, vector<int>(n, 0));
  h[0] = n;
  for (u = 0; u < n; ++u)
    if (c[0][u] > 0) {
      e[u] = c[0][u];
      f[0][u] = c[0][u];
      f[u][0] = -f[0][u];
    }

  list<int> theList;
  for (u = 1; u < n - 1; ++u)
    theList.push_back(u);
  auto cur = theList.begin();
  while (cur != theList.end()) {
    u = *cur;
    if (discharge(u, e, current, nei, c, f, h, n)) {
      theList.erase(cur);
      cur = theList.insert(theList.begin(), u);
    }
    cur++;
  }
  return e[n - 1];
}

int main() {
  int n, edgeCount;
  ifstream in("input.txt");
  cin >> n >> edgeCount;

  vector<vector<int>> c(n, vector<int>(n, 0));
  for (int a = 0; a < edgeCount; ++a) {
    int i, j;
    cin >> i >> j;
    cin >> c[i][j];
  }

  int result = ltf(n, c);

  cout << result << endl;

  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <list>
#include <vector>
using namespace std;

void lift(int u, vector<int> &e, vector<list<int>> &nei, vector<vector<int>> &c,
          vector<vector<int>> &f, vector<int> &h, int n) {
  int height = 2 * n;
  for (auto it = nei[u].begin(); it != nei[u].end(); it++)
    if (c[u][*it] > f[u][*it])
      height = min(height, h[*it]);
  h[u] = height + 1;
}

void push(int u, int v, vector<vector<int>> &c, vector<vector<int>> &f,
          vector<int> &e) {
  int d = min(c[u][v] - f[u][v], e[u]);
  f[u][v] += d;
  f[v][u] = -f[u][v];
  e[u] -= d;
  e[v] += d;
}

bool discharge(int u, vector<int> &e, vector<list<int>::iterator> &current,
               vector<list<int>> &nei, vector<vector<int>> &c,
               vector<vector<int>> &f, vector<int> &h, int n) {
  bool lifted = false;

  while (e[u] > 0) {
    if (current[u] == nei[u].end()) {
      lift(u, e, nei, c, f, h, n);
      lifted = true;
      current[u] = nei[u].begin();
      continue;
    }
    int v = *current[u];
    if (c[u][v] > f[u][v] && h[u] == h[v] + 1)
      push(u, v, c, f, e);
    else
      current[u]++;
  }

  return lifted;
}

int ltf(int n, vector<vector<int>> c) {
  int u, v;
  vector<list<int>> nei(n);
  vector<list<int>::iterator> current(n);
  for (u = 0; u < n; ++u) {
    for (v = 0; v < n; ++v)
      if (c[u][v] > 0 || c[v][u] > 0)
        nei[u].push_back(v);
    current[u] = nei[u].begin();
  }

  vector<int> h(n, 0), e(n, 0);
  vector<vector<int>> f(n, vector<int>(n, 0));
  h[0] = n;
  for (u = 0; u < n; ++u)
    if (c[0][u] > 0) {
      e[u] = c[0][u];
      f[0][u] = c[0][u];
      f[u][0] = -f[0][u];
    }

  list<int> theList;
  for (u = 1; u < n - 1; ++u)
    theList.push_back(u);
  auto cur = theList.begin();
  while (cur != theList.end()) {
    u = *cur;
    if (discharge(u, e, current, nei, c, f, h, n)) {
      theList.erase(cur);
      cur = theList.insert(theList.begin(), u);
    }
    cur++;
  }
  return e[n - 1];
}

int main() {
  int n, edgeCount;
  ifstream in("input.txt");
  cin >> n >> edgeCount;

  vector<vector<int>> c(n, vector<int>(n, 0));
  for (int a = 0; a < edgeCount; ++a) {
    int i, j;
    in >> i >> j;
    in >> c[i][j];
  }

  int result = ltf(n, c);

  cout << result;

  return 0;
}