Problem p03718 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
#define dame                                                                   \
  {                                                                            \
    puts("-1");                                                                \
    return 0;                                                                  \
  }
#define yn                                                                     \
  { puts("Yes"); }                                                             \
  else {                                                                       \
    puts("No");                                                                \
  }
#define MAX_N 200005

const int MAX_V = 1100;
const int INF = 1001001001;
const ll LINF = 23456789123456789;
struct edge {
  ll to, cap, rev;
};

vector<edge> G[MAX_V];
ll level[MAX_V]; // sからの距離
int iter[MAX_V]; // どこまで調べ終わったか

// fromからtoへ向かう容量capの辺をグラフに追加する
void add_edge(int from, int to, ll cap) {
  G[from].push_back((edge){to, cap, (ll)G[to].size()});
  G[to].push_back((edge){from, 0, (ll)G[from].size() - 1});
}

// sからの最短距離をBFSで計算する
void bfs(int s) {
  memset(level, -1, sizeof(level));
  queue<int> que;
  level[s] = 0;
  que.push(s);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    rep(i, G[v].size()) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[e.to] < 0) {
        level[e.to] = level[v] + 1;
        que.push(e.to);
      }
    }
  }
}

// 増加パスをDFSで探す
ll dfs(int v, int t, ll f) {
  if (v == t)
    return f;
  for (int &i = iter[v]; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (e.cap > 0 && level[v] < level[e.to]) {
      ll d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

// sからtへの最大流を求める
ll max_flow(int s, int t) {
  ll flow = 0;
  for (;;) {
    bfs(s);
    if (level[t] < 0)
      return flow;
    memset(iter, 0, sizeof(iter));
    int f;
    while ((f = dfs(s, t, INF)) > 0) {
      flow += f;
    }
  }
}

int main() {

  int h, w;
  cin >> h >> w;

  string s[h];
  rep(i, h) cin >> s[i];

  int S, T;

  rep(i, h) {
    rep(j, w) {
      if (s[i][j] != '.') {
        int x = i * 100 + j;
        add_edge(x, x + 10000, 1);
        rep(k, h) {
          if (i == k)
            continue;
          if (s[k][j] != '.') {
            int y = k * 100 + j;
            add_edge(y + 10000, x, 1);
            add_edge(x + 10000, y, 1);
          }
        }
        rep(k, w) {
          if (j == k)
            continue;
          if (s[i][k] != '.') {
            int y = i * 100 + k;
            add_edge(y + 10000, x, 1);
            add_edge(x + 10000, y, 1);
          }
        }
      }
      if (s[i][j] == 'S')
        S = i * 100 + j;
      if (s[i][j] == 'T')
        T = i * 100 + j;
    }
  }

  if (S % 100 == T % 100 || S / 100 == T / 100) {
    cout << -1 << endl;
    return 0;
  }

  // 3. 出力.
  cout << max_flow(S + 10000, T) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
#define dame                                                                   \
  {                                                                            \
    puts("-1");                                                                \
    return 0;                                                                  \
  }
#define yn                                                                     \
  { puts("Yes"); }                                                             \
  else {                                                                       \
    puts("No");                                                                \
  }
#define MAX_N 200005

const int MAX_V = 21100;
const int INF = 1001001001;
const ll LINF = 23456789123456789;
struct edge {
  ll to, cap, rev;
};

vector<edge> G[MAX_V];
ll level[MAX_V]; // sからの距離
int iter[MAX_V]; // どこまで調べ終わったか

// fromからtoへ向かう容量capの辺をグラフに追加する
void add_edge(int from, int to, ll cap) {
  G[from].push_back((edge){to, cap, (ll)G[to].size()});
  G[to].push_back((edge){from, 0, (ll)G[from].size() - 1});
}

// sからの最短距離をBFSで計算する
void bfs(int s) {
  memset(level, -1, sizeof(level));
  queue<int> que;
  level[s] = 0;
  que.push(s);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    rep(i, G[v].size()) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[e.to] < 0) {
        level[e.to] = level[v] + 1;
        que.push(e.to);
      }
    }
  }
}

// 増加パスをDFSで探す
ll dfs(int v, int t, ll f) {
  if (v == t)
    return f;
  for (int &i = iter[v]; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (e.cap > 0 && level[v] < level[e.to]) {
      ll d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

// sからtへの最大流を求める
ll max_flow(int s, int t) {
  ll flow = 0;
  for (;;) {
    bfs(s);
    if (level[t] < 0)
      return flow;
    memset(iter, 0, sizeof(iter));
    int f;
    while ((f = dfs(s, t, INF)) > 0) {
      flow += f;
    }
  }
}

int main() {

  int h, w;
  cin >> h >> w;

  string s[h];
  rep(i, h) cin >> s[i];

  int S, T;

  rep(i, h) {
    rep(j, w) {
      if (s[i][j] != '.') {
        int x = i * 100 + j;
        add_edge(x, x + 10000, 1);
        rep(k, h) {
          if (i == k)
            continue;
          if (s[k][j] != '.') {
            int y = k * 100 + j;
            add_edge(y + 10000, x, 1);
            add_edge(x + 10000, y, 1);
          }
        }
        rep(k, w) {
          if (j == k)
            continue;
          if (s[i][k] != '.') {
            int y = i * 100 + k;
            add_edge(y + 10000, x, 1);
            add_edge(x + 10000, y, 1);
          }
        }
      }
      if (s[i][j] == 'S')
        S = i * 100 + j;
      if (s[i][j] == 'T')
        T = i * 100 + j;
    }
  }

  if (S % 100 == T % 100 || S / 100 == T / 100) {
    cout << -1 << endl;
    return 0;
  }

  // 3. 出力.
  cout << max_flow(S + 10000, T) << endl;
  return 0;
}