Problem p03718 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> P2;
typedef pair<pair<int, int>, int> P3;
typedef pair<pair<int, int>, pair<int, int>> P4;
#define PB(a) push_back(a)
#define MP(a, b) make_pair((a), (b))
#define M3P(a, b, c) make_pair(make_pair((a), (b)), (c))
#define M4P(a, b, c, d) make_pair(make_pair((a), (b)), make_pair((c), (d)))
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define repm(i, a, b) for (int i = (int)(a); i > (int)(b); --i)

const int EK_MAX = 1010;
const int EK_EDGE_MAX = 1010;
const int INF = 1e9;

template <class T> class EK {
  vector<int> V[EK_MAX];
  pair<int, int> edge[EK_EDGE_MAX];
  T f[EK_EDGE_MAX * 2];
  int d[EK_MAX];
  int n;

  int rev(int a) { return a / 2 * 2 + (a + 1) % 2; }

  void bfs(int s) {
    fill(d, d + EK_MAX, -1);
    d[s] = 0;
    queue<int> Q;
    Q.push(s);
    while (!Q.empty()) {
      int x = Q.front();
      Q.pop();
      for (vector<int>::iterator it = V[x].begin(); it != V[x].end(); ++it) {
        if (f[*it] == 0)
          continue;
        int y = (*it) % 2 == 1 ? edge[(*it) / 2].first : edge[(*it) / 2].second;
        if (d[y] >= 0)
          continue;
        d[y] = d[x] + 1;
        Q.push(y);
      }
    }
  }

public:
  EK() {
    n = 0;
    fill(f, f + EK_EDGE_MAX * 2, 0);
  }

  int put(int a, int b, T c) {
    edge[n] = make_pair(a, b);
    V[a].push_back(2 * n);
    V[b].push_back(2 * n + 1);
    f[2 * n] = c;
    f[2 * n + 1] = 0;
    return n++;
  }

  T ans(int s, int t) {
    while (1) {
      bfs(s);
      if (d[t] < 0) {
        T x = 0;
        for (vector<int>::iterator it = V[s].begin(); it != V[s].end(); ++it) {
          x += f[(*it) / 2 * 2 + 1] * ((*it) % 2 == 0 ? 1 : -1);
        }
        return x;
      }
      queue<int> Q;
      T y = INF;
      for (int i = t; i != s;) {
        for (vector<int>::iterator it = V[i].begin(); it != V[i].end(); ++it) {
          if (f[rev(*it)] == 0)
            continue;
          int z =
              (*it) % 2 == 1 ? edge[(*it) / 2].first : edge[(*it) / 2].second;
          if (d[z] == d[i] - 1) {
            i = z;
            y = min(y, f[rev(*it)]);
            Q.push(*it);
            break;
          }
        }
      }
      while (!Q.empty()) {
        int x = Q.front();
        Q.pop();
        f[rev(x)] -= y;
        f[x] += y;
      }
    }
  }
};

const int Z = 100;
int H, W;
EK<int> solve;

int main() {
  scanf("%d%d", &H, &W);
  repp(i, 0, H) {
    repp(j, 0, W) {
      char c;
      scanf(" %c", &c);
      if (c == 'o') {
        solve.put(i, Z + j, 1);
        solve.put(Z + j, i, 1);
      } else if (c == 'S') {
        solve.put(2 * Z, i, INF);
        solve.put(2 * Z, Z + j, INF);
      } else if (c == 'T') {
        solve.put(i, 2 * Z + 1, INF);
        solve.put(Z + j, 2 * Z + 1, INF);
      }
    }
  }
  int d = solve.ans(2 * Z, 2 * Z + 1);
  printf("%d\n", d < INF ? d : -1);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> P2;
typedef pair<pair<int, int>, int> P3;
typedef pair<pair<int, int>, pair<int, int>> P4;
#define PB(a) push_back(a)
#define MP(a, b) make_pair((a), (b))
#define M3P(a, b, c) make_pair(make_pair((a), (b)), (c))
#define M4P(a, b, c, d) make_pair(make_pair((a), (b)), make_pair((c), (d)))
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define repm(i, a, b) for (int i = (int)(a); i > (int)(b); --i)

const int EK_MAX = 1010;
const int EK_EDGE_MAX = 200010;
const int INF = 1e9;

template <class T> class EK {
  vector<int> V[EK_MAX];
  pair<int, int> edge[EK_EDGE_MAX];
  T f[EK_EDGE_MAX * 2];
  int d[EK_MAX];
  int n;

  int rev(int a) { return a / 2 * 2 + (a + 1) % 2; }

  void bfs(int s) {
    fill(d, d + EK_MAX, -1);
    d[s] = 0;
    queue<int> Q;
    Q.push(s);
    while (!Q.empty()) {
      int x = Q.front();
      Q.pop();
      for (vector<int>::iterator it = V[x].begin(); it != V[x].end(); ++it) {
        if (f[*it] == 0)
          continue;
        int y = (*it) % 2 == 1 ? edge[(*it) / 2].first : edge[(*it) / 2].second;
        if (d[y] >= 0)
          continue;
        d[y] = d[x] + 1;
        Q.push(y);
      }
    }
  }

public:
  EK() {
    n = 0;
    fill(f, f + EK_EDGE_MAX * 2, 0);
  }

  int put(int a, int b, T c) {
    edge[n] = make_pair(a, b);
    V[a].push_back(2 * n);
    V[b].push_back(2 * n + 1);
    f[2 * n] = c;
    f[2 * n + 1] = 0;
    return n++;
  }

  T ans(int s, int t) {
    while (1) {
      bfs(s);
      if (d[t] < 0) {
        T x = 0;
        for (vector<int>::iterator it = V[s].begin(); it != V[s].end(); ++it) {
          x += f[(*it) / 2 * 2 + 1] * ((*it) % 2 == 0 ? 1 : -1);
        }
        return x;
      }
      queue<int> Q;
      T y = INF;
      for (int i = t; i != s;) {
        for (vector<int>::iterator it = V[i].begin(); it != V[i].end(); ++it) {
          if (f[rev(*it)] == 0)
            continue;
          int z =
              (*it) % 2 == 1 ? edge[(*it) / 2].first : edge[(*it) / 2].second;
          if (d[z] == d[i] - 1) {
            i = z;
            y = min(y, f[rev(*it)]);
            Q.push(*it);
            break;
          }
        }
      }
      while (!Q.empty()) {
        int x = Q.front();
        Q.pop();
        f[rev(x)] -= y;
        f[x] += y;
      }
    }
  }
};

const int Z = 100;
int H, W;
EK<int> solve;

int main() {
  scanf("%d%d", &H, &W);
  repp(i, 0, H) {
    repp(j, 0, W) {
      char c;
      scanf(" %c", &c);
      if (c == 'o') {
        solve.put(i, Z + j, 1);
        solve.put(Z + j, i, 1);
      } else if (c == 'S') {
        solve.put(2 * Z, i, INF);
        solve.put(2 * Z, Z + j, INF);
      } else if (c == 'T') {
        solve.put(i, 2 * Z + 1, INF);
        solve.put(Z + j, 2 * Z + 1, INF);
      }
    }
  }
  int d = solve.ans(2 * Z, 2 * Z + 1);
  printf("%d\n", d < INF ? d : -1);
  return 0;
}