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

#include <bits/stdc++.h>

#define rep(i, x, y) for (int i = x, i##end = y; i <= i##end; ++i)
#define _rep(i, x, y) for (int i = x, i##end = y; i >= i##end; --i)
#define ll long long
#define pk push_back
#define N 100005
typedef std::pair<int, int> pii;
int s, t;
int cnt = 1, head[N], to[N], flow[N], cost[N], nxt[N];
inline void add(int u, int v, int w, int c) {
  to[++cnt] = v, flow[cnt] = w, cost[cnt] = c, nxt[cnt] = head[u],
  head[u] = cnt;
  to[++cnt] = u, flow[cnt] = 0, cost[cnt] = -c, nxt[cnt] = head[v],
  head[v] = cnt;
}
bool vis[N];
int pre[N], dep[N];
inline bool spfa(int s, int t) {
  memset(vis, 0, sizeof(vis));
  memset(pre, -1, sizeof(pre));
  memset(dep, 128, sizeof(dep));
  std::queue<int> q;
  q.push(s), dep[s] = 0, vis[s] = 1;
  int inf = dep[0];
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = 0;
    for (int i = head[u]; i; i = nxt[i]) {
      if (flow[i] && dep[to[i]] < dep[u] + cost[i]) {
        dep[to[i]] = dep[u] + cost[i], pre[to[i]] = i;
        if (!vis[to[i]])
          vis[to[i]] = 1, q.push(to[i]);
      }
    }
  }
  return dep[t] != inf;
}
inline std::pair<int, int> mcmf(int s, int t) {
  std::pair<int, int> ret(0, 0);
  while (spfa(s, t)) {
    int mi = 2e9;
    for (int i = t; i != s; i = to[pre[i] ^ 1])
      mi = std::min(mi, flow[pre[i]]);
    for (int i = t; i != s; i = to[pre[i] ^ 1])
      flow[pre[i]] -= mi, flow[pre[i] ^ 1] += mi;
    ret.first += mi, ret.second += dep[t] * mi;
  }
  return ret;
}
int n, m;
char S[105][105];
inline int id(int x, int y) { return (x - 1) * m + y; }
int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0);
  std::cin >> n >> m, s = n * m + 1, t = s + 1;
  rep(i, 1, n) std::cin >> S[i] + 1;
  rep(i, 1, n) rep(j, 1, m) if (S[i][j] != '#') {
    if (S[i][j] == 'o')
      add(s, id(i, j), 1, 0);
    if (i < n && S[i + 1][j] != '#')
      add(id(i, j), id(i + 1, j), 2e9, 1);
    if (j < m && S[i][j + 1] != '#')
      add(id(i, j), id(i, j + 1), 2e9, 1);
    add(id(i, j), t, 1, 0);
  }
  std::cout << mcmf(s, t).second << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, x, y) for (int i = x, i##end = y; i <= i##end; ++i)
#define _rep(i, x, y) for (int i = x, i##end = y; i >= i##end; --i)
#define ll long long
#define pk push_back
#define N 100005
typedef std::pair<int, int> pii;
int s, t;
int cnt = 1, head[N], to[N], flow[N], cost[N], nxt[N];
inline void add(int u, int v, int w, int c) {
  to[++cnt] = v, flow[cnt] = w, cost[cnt] = c, nxt[cnt] = head[u],
  head[u] = cnt;
  to[++cnt] = u, flow[cnt] = 0, cost[cnt] = -c, nxt[cnt] = head[v],
  head[v] = cnt;
}
bool vis[N];
int pre[N], dep[N];
inline bool spfa(int s, int t) {
  memset(vis, 0, sizeof(vis));
  memset(pre, -1, sizeof(pre));
  memset(dep, 128, sizeof(dep));
  std::queue<int> q;
  q.push(s), dep[s] = 0, vis[s] = 1;
  int inf = dep[0];
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = 0;
    for (int i = head[u]; i; i = nxt[i]) {
      if (flow[i] && dep[to[i]] < dep[u] + cost[i]) {
        dep[to[i]] = dep[u] + cost[i], pre[to[i]] = i;
        if (!vis[to[i]])
          vis[to[i]] = 1, q.push(to[i]);
      }
    }
  }
  return dep[t] != inf;
}
inline std::pair<int, int> mcmf(int s, int t) {
  std::pair<int, int> ret(0, 0);
  while (spfa(s, t)) {
    int mi = 2e9;
    for (int i = t; i != s; i = to[pre[i] ^ 1])
      mi = std::min(mi, flow[pre[i]]);
    for (int i = t; i != s; i = to[pre[i] ^ 1])
      flow[pre[i]] -= mi, flow[pre[i] ^ 1] += mi;
    ret.first += mi, ret.second += dep[t] * mi;
  }
  return ret;
}
int n, m;
char S[105][105];
inline int id(int x, int y) { return (x - 1) * m + y; }
int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0);
  std::cin >> n >> m, s = n * m, t = s + 1;
  rep(i, 1, n) std::cin >> S[i] + 1;
  rep(i, 1, n) rep(j, 1, m) if (S[i][j] != '#') {
    if (S[i][j] == 'o')
      add(s, id(i, j), 1, 0);
    if (i < n && S[i + 1][j] != '#')
      add(id(i, j), id(i + 1, j), 2e9, 1);
    if (j < m && S[i][j + 1] != '#')
      add(id(i, j), id(i, j + 1), 2e9, 1);
    add(id(i, j), t, 1, 0);
  }
  std::cout << mcmf(s, t).second << '\\n';
  return 0;
}