Problem p03718 (C++)

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

const int oo = 0x3f3f3f3f;
const int maxn = 40000 + 10;
const int maxm = 8000000 + 10;

int S, T;
namespace Dinic {

int ecnt = 1;
int dis[maxn], cur[maxn];
int st[maxn], to[maxn], nxt[maxn], cap[maxn];

void addedge(int u, int v, int c) {
  to[++ecnt] = v;
  nxt[ecnt] = st[u];
  st[u] = ecnt;
  cap[ecnt] = c;
  to[++ecnt] = u;
  nxt[ecnt] = st[v];
  st[v] = ecnt;
  cap[ecnt] = 0;
}

bool bfs() {
  static int q[maxn];
  int head = 0, tail = 0;
  memset(dis, 0xff, sizeof dis);

  dis[q[tail++] = S] = 0;
  while (head < tail) {
    int x = q[head++];
    for (int i = st[x]; i; i = nxt[i])
      if (cap[i] && !~dis[to[i]]) {
        dis[to[i]] = dis[x] + 1;
        q[tail++] = to[i];
      }
  }
  return ~dis[T];
}

int dfs(int u, int flow) {
  if (u == T || !flow)
    return flow;

  int res = 0;
  for (int &i = cur[u]; i; i = nxt[i]) {
    int aug, v = to[i];
    if (dis[v] == dis[u] + 1 && (aug = dfs(v, min(flow, cap[i])))) {
      res += aug;
      flow -= aug;
      cap[i] -= aug;
      cap[i ^ 1] += aug;
      if (!flow)
        return res;
    }
  }
  return res;
}
int maxflow() {
  int ans = 0;
  while (bfs()) {
    memcpy(cur, st, sizeof st);
    ans += dfs(S, oo);
  }
  return ans;
}
} // namespace Dinic

int main() {
  char grid[110][110];
  int n, m, si, sj, ti, tj;
  // freopen("data.txt", "r", stdin);

  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++)
    scanf("%s", grid[i]);

  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      int sx, tx;
      sx = i * m + j;
      tx = i * m + j + n * m;

      if (grid[i][j] == 'S') {
        S = i * m + j;

        si = i, sj = j;
        for (int k = 0; k < n; k++)
          if (k ^ i)
            if (grid[k][j] != '.')
              Dinic ::addedge(S, k * m + j, 1);
        for (int k = 0; k < m; k++)
          if (k ^ j)
            if (grid[i][k] != '.')
              Dinic ::addedge(S, i * m + k, 1);
      } else if (grid[i][j] == 'T') {
        T = i * m + j;

        ti = i, tj = j;
        for (int k = 0; k < n; k++)
          if (k ^ i)
            if (grid[k][j] != '.')
              Dinic ::addedge(k * m + j + n * m, T, 1);
        for (int k = 0; k < m; k++)
          if (k ^ j)
            if (grid[i][k] != '.')
              Dinic ::addedge(i * m + k + n * m, T, 1);
      } else {
        Dinic ::addedge(sx, tx, 1);
        for (int k = 0; k < n; k++)
          if (k ^ i)
            if (grid[k][j] != '.')
              Dinic ::addedge(tx, k * m + j, 1);
        for (int k = 0; k < m; k++)
          if (k ^ j)
            if (grid[i][k] != '.')
              Dinic ::addedge(tx, i * m + k, 1);
      }
    }

  if (si == ti || sj == tj)
    return printf("%d\n", -1), 0;

  printf("%d\n", Dinic ::maxflow());

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int oo = 0x3f3f3f3f;
const int maxn = 40000 + 10;
const int maxm = 8000000 + 10;

int S, T;
namespace Dinic {

int ecnt = 1;
int dis[maxn], cur[maxn];
int st[maxn], to[maxm], nxt[maxm], cap[maxm];

void addedge(int u, int v, int c) {
  to[++ecnt] = v;
  nxt[ecnt] = st[u];
  st[u] = ecnt;
  cap[ecnt] = c;
  to[++ecnt] = u;
  nxt[ecnt] = st[v];
  st[v] = ecnt;
  cap[ecnt] = 0;
}

bool bfs() {
  static int q[maxn];
  int head = 0, tail = 0;
  memset(dis, 0xff, sizeof dis);

  dis[q[tail++] = S] = 0;
  while (head < tail) {
    int x = q[head++];
    for (int i = st[x]; i; i = nxt[i])
      if (cap[i] && !~dis[to[i]]) {
        dis[to[i]] = dis[x] + 1;
        q[tail++] = to[i];
      }
  }
  return ~dis[T];
}

int dfs(int u, int flow) {
  if (u == T || !flow)
    return flow;

  int res = 0;
  for (int &i = cur[u]; i; i = nxt[i]) {
    int aug, v = to[i];
    if (dis[v] == dis[u] + 1 && (aug = dfs(v, min(flow, cap[i])))) {
      res += aug;
      flow -= aug;
      cap[i] -= aug;
      cap[i ^ 1] += aug;
      if (!flow)
        return res;
    }
  }
  return res;
}
int maxflow() {
  int ans = 0;
  while (bfs()) {
    memcpy(cur, st, sizeof st);
    ans += dfs(S, oo);
  }
  return ans;
}
} // namespace Dinic

int main() {
  char grid[110][110];
  int n, m, si, sj, ti, tj;
  // freopen("data.txt", "r", stdin);

  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++)
    scanf("%s", grid[i]);

  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      int sx, tx;
      sx = i * m + j;
      tx = i * m + j + n * m;

      if (grid[i][j] == 'S') {
        S = i * m + j;

        si = i, sj = j;
        for (int k = 0; k < n; k++)
          if (k ^ i)
            if (grid[k][j] != '.')
              Dinic ::addedge(S, k * m + j, 1);
        for (int k = 0; k < m; k++)
          if (k ^ j)
            if (grid[i][k] != '.')
              Dinic ::addedge(S, i * m + k, 1);
      } else if (grid[i][j] == 'T') {
        T = i * m + j;

        ti = i, tj = j;
        for (int k = 0; k < n; k++)
          if (k ^ i)
            if (grid[k][j] != '.')
              Dinic ::addedge(k * m + j + n * m, T, 1);
        for (int k = 0; k < m; k++)
          if (k ^ j)
            if (grid[i][k] != '.')
              Dinic ::addedge(i * m + k + n * m, T, 1);
      } else {
        Dinic ::addedge(sx, tx, 1);
        for (int k = 0; k < n; k++)
          if (k ^ i)
            if (grid[k][j] != '.')
              Dinic ::addedge(tx, k * m + j, 1);
        for (int k = 0; k < m; k++)
          if (k ^ j)
            if (grid[i][k] != '.')
              Dinic ::addedge(tx, i * m + k, 1);
      }
    }

  if (si == ti || sj == tj)
    return printf("%d\n", -1), 0;

  printf("%d\n", Dinic ::maxflow());

  return 0;
}