Problem p02542 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define x first
#define y second
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) { x < y ? x = y : T(); }
template <typename T> void chkmin(T &x, T y) { x > y ? x = y : T(); }
template <typename T> void readint(T &x) {
  x = 0;
  int f = 1;
  char c;
  for (c = getchar(); !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  x *= f;
}
const int MOD = 998244353;
inline int dmy(int x) { return x >= MOD ? x - MOD : x; }
inline void inc(int &x, int y) { x = dmy(x + y); }
int qmi(int x, int y) {
  int ans = 1;
  for (; y; y >>= 1, x = 1ll * x * x % MOD)
    if (y & 1)
      ans = 1ll * ans * x % MOD;
  return ans;
}
const int MAXN = 25005, MAXM = 200005, INF = 0x3f3f3f3f;

int n, m;
char a[55][55];

struct Edge {
  int v, f, c;
  Edge *next, *rev;
} pool[MAXM], *h[MAXN], *pe[MAXN];
int etot = 0;
inline void addEdge(int u, int v, int f, int c) {
  Edge *p = &pool[++etot], *q = &pool[++etot];
  p->v = v, p->f = f, p->c = c, p->next = h[u], h[u] = p, p->rev = q;
  q->v = u, q->f = 0, q->c = -c, q->next = h[v], h[v] = q, q->rev = p;
}
int src, sink;
int q[MAXM], dis[MAXN], pv[MAXN];
bool inq[MAXN];
bool spfa() {
  int front = 1, rear = 0;
  memset(dis, 0x3f, sizeof(dis));
  dis[src] = 0;
  q[++rear] = src;
  inq[src] = 1;
  while (front <= rear) {
    int u = q[front++];
    inq[u] = 0;
    for (Edge *p = h[u]; p; p = p->next)
      if (p->f && dis[p->v] > dis[u] + p->c) {
        dis[p->v] = dis[u] + p->c;
        pv[p->v] = u, pe[p->v] = p;
        if (!inq[p->v])
          q[++rear] = p->v;
      }
  }
  return dis[sink] < INF;
}
int augment() {
  int t = INF;
  for (int u = sink; u != src; u = pv[u])
    chkmin(t, pe[u]->f);
  for (int u = sink; u != src; u = pv[u])
    pe[u]->f -= t, pe[u]->rev->f += t;
  return t;
}

int main() {
#ifdef LOCAL
  freopen("code.in", "r", stdin);
  //	freopen("code.out","w",stdout);
#endif
  readint(n), readint(m);
  for (int i = 0; i < n; ++i)
    scanf("%s", a[i]);
  src = n * m, sink = n * m + 1;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      if (a[i][j] == '#')
        continue;
      if (a[i][j] == 'o')
        addEdge(src, i * m + j, 1, 0);
      if (i <= n - 2)
        addEdge(i * m + j, (i + 1) * m + j, INF, -1);
      if (j <= m - 2)
        addEdge(i * m + j, i * m + j + 1, INF, -1);
      addEdge(i * m + j, sink, 1, 0);
    }
  int cost = 0, t;
  while (spfa())
    t = augment(), cost += t * dis[sink];
  printf("%d\\n", -cost);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define x first
#define y second
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) { x < y ? x = y : T(); }
template <typename T> void chkmin(T &x, T y) { x > y ? x = y : T(); }
template <typename T> void readint(T &x) {
  x = 0;
  int f = 1;
  char c;
  for (c = getchar(); !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  x *= f;
}
const int MOD = 998244353;
inline int dmy(int x) { return x >= MOD ? x - MOD : x; }
inline void inc(int &x, int y) { x = dmy(x + y); }
int qmi(int x, int y) {
  int ans = 1;
  for (; y; y >>= 1, x = 1ll * x * x % MOD)
    if (y & 1)
      ans = 1ll * ans * x % MOD;
  return ans;
}
const int MAXN = 2505, MAXM = 20005, INF = 0x3f3f3f3f;

int n, m;
char a[55][55];

struct Edge {
  int v, f, c;
  Edge *next, *rev;
} pool[MAXM], *h[MAXN], *pe[MAXN];
int etot = 0;
inline void addEdge(int u, int v, int f, int c) {
  Edge *p = &pool[++etot], *q = &pool[++etot];
  p->v = v, p->f = f, p->c = c, p->next = h[u], h[u] = p, p->rev = q;
  q->v = u, q->f = 0, q->c = -c, q->next = h[v], h[v] = q, q->rev = p;
}
int src, sink;
int q[MAXM], dis[MAXN], pv[MAXN];
bool inq[MAXN];
bool spfa() {
  int front = 1, rear = 0;
  memset(dis, 0x3f, sizeof(dis));
  dis[src] = 0;
  q[++rear] = src;
  inq[src] = 1;
  while (front <= rear) {
    int u = q[front++];
    inq[u] = 0;
    for (Edge *p = h[u]; p; p = p->next)
      if (p->f && dis[p->v] > dis[u] + p->c) {
        dis[p->v] = dis[u] + p->c;
        pv[p->v] = u, pe[p->v] = p;
        if (!inq[p->v])
          q[++rear] = p->v;
      }
  }
  return dis[sink] < INF;
}
int augment() {
  int t = INF;
  for (int u = sink; u != src; u = pv[u])
    chkmin(t, pe[u]->f);
  for (int u = sink; u != src; u = pv[u])
    pe[u]->f -= t, pe[u]->rev->f += t;
  return t;
}

int main() {
#ifdef LOCAL
  freopen("code.in", "r", stdin);
  //	freopen("code.out","w",stdout);
#endif
  readint(n), readint(m);
  for (int i = 0; i < n; ++i)
    scanf("%s", a[i]);
  src = n * m, sink = n * m + 1;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      if (a[i][j] == '#')
        continue;
      if (a[i][j] == 'o')
        addEdge(src, i * m + j, 1, 0);
      if (i <= n - 2)
        addEdge(i * m + j, (i + 1) * m + j, INF, -1);
      if (j <= n - 2)
        addEdge(i * m + j, i * m + j + 1, INF, -1);
      addEdge(i * m + j, sink, 1, 0);
    }
  int cost = 0, t;
  while (spfa())
    t = augment(), cost += t * dis[sink];
  printf("%d\\n", -cost);
  return 0;
}