Problem p02542 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize(200000)
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#define re register
using namespace std;
struct edge {
  int next, w, to, ww, fr;
} e[3000005];
const int Mxdt = 100000;
char gc() {
  static char buf[Mxdt], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, Mxdt, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
#define gc getchar
int read() {
  int res = 0;
  char ch = gc();
  while (ch < '0' || ch > '9')
    ch = gc();
  while (ch >= '0' && ch <= '9')
    res = (res << 3) + (res << 1) + (ch ^ 48), ch = gc();
  return res;
}
int head[3002], b, c, d, n, m, cnt = 1, S, T, dis[3002], pre[3002], p[3002],
                               ans1, ans2, vv[3002];
void bu(register int u, register int v, register int w, re int ww) {
  if (w == 0)
    return;
  e[++cnt].to = v;
  e[cnt].ww = ww;
  e[cnt].w = w;
  e[cnt].fr = u;
  e[cnt].next = head[u];
  head[u] = cnt;
  e[++cnt].to = u;
  e[cnt].fr = v;
  e[cnt].ww = -ww;
  e[cnt].next = head[v];
  head[v] = cnt;
}
deque<int> q;
int spfa(register int s, register int t) {

  memset(p, 0, sizeof(p));
  memset(dis, 127, sizeof(dis));
  memset(vv, 0, sizeof(vv));
  bool zzw = 0;
  q.push_front(s);
  dis[s] = 0;
  vv[s] = 1;
  while (!q.empty()) {
    int r = q.front();
    q.pop_front();
    vv[r] = 0;
    if (r == t)
      zzw = 1;
    for (register int i = head[r]; i; i = e[i].next) {
      if (e[i].w && dis[e[i].to] > dis[r] + e[i].ww) {
        dis[e[i].to] = dis[r] + e[i].ww;
        p[e[i].to] = i;
        if (!vv[e[i].to]) {

          vv[e[i].to] = 1;
          if (!q.empty() && dis[e[i].to] < dis[q.front()])
            q.push_front(e[i].to);
          else
            q.push_back(e[i].to);
        }
      }
    }
  }
  if (zzw)
    return 1;
  return 0;
}
#define min mn
int min(re int x, re int y) { return x < y ? x : y; }
void dfs() {
  int tmp = 0x7fffffff, cnt = 0, pp;
  for (re int i = T; i != S; i = e[pp ^ 1].to) {
    pp = p[i];
    cnt += e[pp].ww;
    tmp = min(tmp, e[pp].w);
  }
  for (re int i = T; i != S; i = e[pp ^ 1].to) {
    pp = p[i];
    e[pp].w -= tmp, e[pp ^ 1].w += tmp;
  }

  ans1 += tmp, ans2 += tmp * cnt;
}
char s[502][502];
int vis[52][52];
inline void bfs(re int x, re int y, re int z) {
  if (x > n || y > m || s[x][y] == '#')
    return;
  vis[x][y] = z;
  bfs(x + 1, y, z + 1);
  bfs(x, y + 1, z + 1);
}
signed main() {
  //	freopen("a.out","w",stdout);
  n = read();
  m = read();
  S = n * m + 1, T = S + 1;
  for (re int i = 1; i <= n; ++i)
    scanf("%s", s[i] + 1);
  for (re int i = 1; i <= n; ++i) {
    for (re int j = 1; j <= m; ++j) {
      if (s[i][j] == '#')
        continue;
      if (s[i][j] == 'o')
        bu(S, (i - 1) * m + j, 1, 0);
      if (s[i][j + 1] != '#' && j < m)
        bu((i - 1) * m + j, (i - 1) * m + j + 1, 1 << 29, -1);
      if (s[i + 1][j] != '#' && i < n)
        bu((i - 1) * m + j, i * m + j, 1 << 29, -1);
      bu((i - 1) * m + j, T, 1, 0);
    }
  }
  while (spfa(S, T))
    dfs();
  printf("%d", -ans2);
}

Error: TLE

Fixed submission:

#pragma GCC optimize(200000)
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#define re register
using namespace std;
struct edge {
  int next, w, to, ww, fr;
} e[3000005];
const int Mxdt = 100000;
char gc() {
  static char buf[Mxdt], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, Mxdt, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
#define gc getchar
int read() {
  int res = 0;
  char ch = gc();
  while (ch < '0' || ch > '9')
    ch = gc();
  while (ch >= '0' && ch <= '9')
    res = (res << 3) + (res << 1) + (ch ^ 48), ch = gc();
  return res;
}
int head[3002], b, c, d, n, m, cnt = 1, S, T, dis[3002], pre[3002], p[3002],
                               ans1, ans2, vv[3002];
void bu(register int u, register int v, register int w, re int ww) {
  if (w == 0)
    return;
  e[++cnt].to = v;
  e[cnt].ww = ww;
  e[cnt].w = w;
  e[cnt].fr = u;
  e[cnt].next = head[u];
  head[u] = cnt;
  e[++cnt].to = u;
  e[cnt].fr = v;
  e[cnt].ww = -ww;
  e[cnt].next = head[v];
  head[v] = cnt;
}
deque<int> q;
int spfa(register int s, register int t) {

  memset(p, 0, sizeof(p));
  memset(dis, 127, sizeof(dis));
  memset(vv, 0, sizeof(vv));
  bool zzw = 0;
  q.push_front(s);
  dis[s] = 0;
  vv[s] = 1;
  while (!q.empty()) {
    int r = q.front();
    q.pop_front();
    vv[r] = 0;
    if (r == t)
      zzw = 1;
    for (register int i = head[r]; i; i = e[i].next) {
      if (e[i].w && dis[e[i].to] > dis[r] + e[i].ww) {
        dis[e[i].to] = dis[r] + e[i].ww;
        p[e[i].to] = i;
        if (!vv[e[i].to]) {

          vv[e[i].to] = 1;
          if (!q.empty() && dis[e[i].to] < dis[q.front()])
            q.push_front(e[i].to);
          else
            q.push_back(e[i].to);
        }
      }
    }
  }
  if (zzw)
    return 1;
  return 0;
}
#define min mn
int min(re int x, re int y) { return x < y ? x : y; }
void dfs() {
  int tmp = 0x7fffffff, cnt = 0, pp;
  for (re int i = T; i != S; i = e[pp ^ 1].to) {
    pp = p[i];
    cnt += e[pp].ww;
    tmp = min(tmp, e[pp].w);
  }
  for (re int i = T; i != S; i = e[pp ^ 1].to) {
    pp = p[i];
    e[pp].w -= tmp, e[pp ^ 1].w += tmp;
  }

  ans1 += tmp, ans2 += tmp * cnt;
}
char s[502][502];
int vis[52][52];
inline void bfs(re int x, re int y, re int z) {
  if (x > n || y > m || s[x][y] == '#')
    return;
  vis[x][y] = z;
  bfs(x + 1, y, z + 1);
  bfs(x, y + 1, z + 1);
}
signed main() {
  //	freopen("a.out","w",stdout);
  n = read();
  m = read();
  S = n * m + 1, T = S + 1;
  for (re int i = 1; i <= n; ++i)
    scanf("%s", s[i] + 1);
  for (re int i = 1; i <= n; ++i) {
    for (re int j = 1; j <= m; ++j) {
      if (s[i][j] == '#')
        continue;
      if (s[i][j] == 'o') {
        bu(S, (i - 1) * m + j, 1, 0);
        memset(vis, 0, sizeof(vis));
        bfs(i, j, 0);
        for (re int ii = 1; ii <= n; ++ii) {
          for (re int jj = 1; jj <= m; ++jj) {
            if (vis[ii][jj])
              bu((i - 1) * m + j, (ii - 1) * m + jj, 1, -vis[ii][jj]);
          }
        }
      }
      bu((i - 1) * m + j, T, 1, 0);
    }
  }
  while (spfa(S, T))
    dfs();
  printf("%d", -ans2);
}