Problem p03718 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P, int> pip;
typedef vector<pip> vip;
const int inf = 1 << 29;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

class Network {
private:
  int V;
  struct edge {
    int to, cap, rev;
  };
  vector<vector<edge>> List;
  int DFSplus(int v, int t, int f, vi &iter, vi level) {
    if (v == t)
      return f;
    for (int &i = iter[v]; i < List[v].size(); i++) {
      edge &e = List[v][i];
      if (e.cap > 0 && level[v] < level[e.to]) {
        int d = DFSplus(e.to, t, min(f, e.cap), iter, level);
        if (d > 0) {
          e.cap -= d;
          List[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }
  void BFSplus(int s, vi &level) {
    level = vi(V, -1);
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (int i = 0; i < List[v].size(); i++) {
        edge &e = List[v][i];
        if (e.cap > 0 && level[e.to] < 0) {
          level[e.to] = level[v] + 1;
          que.push(e.to);
        }
      }
    }
  }

public:
  Network(int v) {
    V = v;
    List = vector<vector<edge>>(v);
  }
  void add_edge(int s, int t, int c) {
    List[s].push_back(edge{t, c, (int)List[t].size()});
    List[t].push_back(edge{s, 0, (int)List[s].size() - 1});
  }
  int MFplus(int s, int t) {
    int ans = 0;
    while (1) {
      vi iter(V), level;
      BFSplus(s, level);
      if (level[t] < 0)
        return ans;
      int f;
      while ((f = DFSplus(s, t, inf, iter, level)) > 0)
        ans += f;
    }
  }
};

int h, w;
int sx, sy, tx, ty;
vvc a;

int main() {
  cin >> h >> w;
  a = vvc(h, vc(w));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
      }
      if (a[i][j] == 'T') {
        tx = i;
        ty = j;
      }
    }
  if (sx == tx || sy == ty) {
    cout << -1 << endl;
    return 0;
  }
  Network nt(h * w * 2);
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (a[i][j] != '.') {
        if (!(i == sx && j == sy || i == tx && j == ty))
          nt.add_edge(i * h + j, h * w + i * h + j, 1);
        for (int k = 0; k < h; k++)
          if (i != k && a[k][j] != '.') {
            if (!(i == tx && j == ty))
              nt.add_edge(h * w + i * w + j, k * w + j, 500);
            if (!(i == sx && j == sy))
              nt.add_edge(h * w + k * w + j, i * w + j, 500);
          }
        for (int k = 0; k < w; k++)
          if (j != k && a[i][k] != '.') {
            if (!(i == tx && j == ty))
              nt.add_edge(h * w + i * w + j, i * w + k, 500);
            if (!(i == sx && j == sy))
              nt.add_edge(h * w + i * w + k, i * w + j, 500);
          }
      }
  cout << nt.MFplus(h * w + sx * w + sy, tx * w + ty) << endl;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P, int> pip;
typedef vector<pip> vip;
const int inf = 1 << 29;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

class Network {
private:
  int V;
  struct edge {
    int to, cap, rev;
  };
  vector<vector<edge>> List;
  int DFSplus(int v, int t, int f, vi &iter, vi level) {
    if (v == t)
      return f;
    for (int &i = iter[v]; i < List[v].size(); i++) {
      edge &e = List[v][i];
      if (e.cap > 0 && level[v] < level[e.to]) {
        int d = DFSplus(e.to, t, min(f, e.cap), iter, level);
        if (d > 0) {
          e.cap -= d;
          List[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }
  void BFSplus(int s, vi &level) {
    level = vi(V, -1);
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (int i = 0; i < List[v].size(); i++) {
        edge &e = List[v][i];
        if (e.cap > 0 && level[e.to] < 0) {
          level[e.to] = level[v] + 1;
          que.push(e.to);
        }
      }
    }
  }

public:
  Network(int v) {
    V = v;
    List = vector<vector<edge>>(v);
  }
  void add_edge(int s, int t, int c) {
    List[s].push_back(edge{t, c, (int)List[t].size()});
    List[t].push_back(edge{s, 0, (int)List[s].size() - 1});
  }
  int MFplus(int s, int t) {
    int ans = 0;
    while (1) {
      vi iter(V), level;
      BFSplus(s, level);
      if (level[t] < 0)
        return ans;
      int f;
      while ((f = DFSplus(s, t, inf, iter, level)) > 0)
        ans += f;
    }
  }
};

int h, w;
int sx, sy, tx, ty;
vvc a;

int main() {
  cin >> h >> w;
  a = vvc(h, vc(w));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
      }
      if (a[i][j] == 'T') {
        tx = i;
        ty = j;
      }
    }
  if (sx == tx || sy == ty) {
    cout << -1 << endl;
    return 0;
  }
  Network nt(h * w * 2);
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (a[i][j] != '.') {
        if (!(i == sx && j == sy || i == tx && j == ty))
          nt.add_edge(i * w + j, h * w + i * w + j, 1);
        for (int k = 0; k < h; k++)
          if (i != k && a[k][j] != '.') {
            if (!(i == tx && j == ty))
              nt.add_edge(h * w + i * w + j, k * w + j, 500);
            if (!(i == sx && j == sy))
              nt.add_edge(h * w + k * w + j, i * w + j, 500);
          }
        for (int k = 0; k < w; k++)
          if (j != k && a[i][k] != '.') {
            if (!(i == tx && j == ty))
              nt.add_edge(h * w + i * w + j, i * w + k, 500);
            if (!(i == sx && j == sy))
              nt.add_edge(h * w + i * w + k, i * w + j, 500);
          }
      }
  cout << nt.MFplus(h * w + sx * w + sy, tx * w + ty) << endl;
}