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

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

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)

mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

template <typename T> inline void chkmax(T &x, T y) {
  if (x < y)
    x = y;
}

template <typename T> inline void chkmin(T &x, T y) {
  if (x > y)
    x = y;
}

inline int read() {
#define nc getchar()
  int x = 0;
  char c = nc;
  while (c < 48)
    c = nc;
  while (c > 47)
    x = x * 10 + (c ^ 48), c = nc;
  return x;
#undef nc
}

typedef long long ll;
typedef pair<int, int> pii;
int N, M;
char A[55][55];

bool pd(int x, int y) { return x <= N && y <= M && A[x][y] != '#'; }

int id(int x, int y) { return (x - 1) * M + y; }

int dis[55][55];
vector<pii> vec;

void dfs(int x, int y, int d) {
  if (!pd(x, y) || dis[x][y] != -1)
    return;
  dis[x][y] = d;
  vec.push_back(pii(x, y));
  dfs(x + 1, y, d + 1), dfs(x, y + 1, d + 1);
}

namespace Flow {
const int maxn = 10010, maxm = 2e6 + 10, inf = 1 << 30;
bool vis[maxn];
int S, T, h[maxn], f[maxn], dis[maxn], pre[maxn];

struct edges {
  int nxt, to, w, c;
  edges(int _n = 0, int _t = 0, int _w = 0, int _c = 0)
      : nxt(_n), to(_t), w(_w), c(_c) {}
} e[maxm];

queue<int> q;

void addline(int u, int v, int w, int c) {
  //  printf("(%d %d) %d %d\\n", u, v, w, c);
  static int cnt = 1;
  e[++cnt] = edges(h[u], v, w, c), h[u] = cnt;
  e[++cnt] = edges(h[v], u, 0, -c), h[v] = cnt;
}

bool bfs() {
  memset(dis, 0x3f, (T + 1) << 2);
  q.push(S), dis[S] = 0, f[S] = inf;
  while (!q.empty()) {
    int u = q.front();
    vis[u] = 0, q.pop();
    for (int i = h[u]; i; i = e[i].nxt) {
      int v = e[i].to;
      if (e[i].w && dis[u] + e[i].c < dis[v]) {
        pre[v] = i;
        f[v] = min(f[u], e[i].w);
        dis[v] = dis[u] + e[i].c;
        if (!vis[v])
          q.push(v), vis[v] = 1;
      }
    }
  }
  return dis[T] < 1e9;
}

int EK() {
  int res = 0;
  //  int tmp = 0;
  while (bfs()) {
    res += f[T] * dis[T];
    //    tmp += f[T];
    for (int u = T; u != S; u = e[pre[u] ^ 1].to) {
      e[pre[u]].w -= f[T], e[pre[u] ^ 1].w += f[T];
    }
  }
  //  printf("#%d\\n", tmp);
  return res;
}
} // namespace Flow

void solve() {
  cin >> N >> M;
  rep(i, 1, N) { scanf("%s", A[i] + 1); }
  Flow::S = N * M * 2 + 1, Flow::T = N * M * 2 + 2;
  rep(i, 1, N) rep(j, 1, M) Flow::addline(N * M + id(i, j), Flow::T, 1, 0);
  rep(i, 1, N) rep(j, 1, M) if (A[i][j] == 'o') {
    Flow::addline(Flow::S, id(i, j), 1, 0);
    vec.clear();
    memset(dis, -1, sizeof dis);
    dfs(i, j, 0);
    for (pii p : vec) {
      Flow::addline(id(i, j), N * M + id(p.first, p.second), 1,
                    -dis[p.first][p.second]);
    }
  }
  cout << -Flow::EK() << endl;
}

signed main() {
  //  int T = read();
  //  while (T--) solve();
  solve();
  return 0;
}

Error: -11

Fixed submission:

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

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)

mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

template <typename T> inline void chkmax(T &x, T y) {
  if (x < y)
    x = y;
}

template <typename T> inline void chkmin(T &x, T y) {
  if (x > y)
    x = y;
}

inline int read() {
#define nc getchar()
  int x = 0;
  char c = nc;
  while (c < 48)
    c = nc;
  while (c > 47)
    x = x * 10 + (c ^ 48), c = nc;
  return x;
#undef nc
}

typedef long long ll;
typedef pair<int, int> pii;
int N, M;
char A[55][55];

bool pd(int x, int y) { return x <= N && y <= M && A[x][y] != '#'; }

int id(int x, int y) { return (x - 1) * M + y; }

int dis[55][55];
vector<pii> vec;

void dfs(int x, int y, int d) {
  if (!pd(x, y))
    return;
  dis[x][y] = d;
  vec.push_back(pii(x, y));
  dfs(x + 1, y, d + 1), dfs(x, y + 1, d + 1);
}

namespace Flow {
const int maxn = 10010, maxm = 1e7 + 10, inf = 1 << 30;
bool vis[maxn];
int S, T, h[maxn], f[maxn], dis[maxn], pre[maxn];

struct edges {
  int nxt, to, w, c;
  edges(int _n = 0, int _t = 0, int _w = 0, int _c = 0)
      : nxt(_n), to(_t), w(_w), c(_c) {}
} e[maxm];

queue<int> q;

void addline(int u, int v, int w, int c) {
  //  printf("(%d %d) %d %d\\n", u, v, w, c);
  static int cnt = 1;
  e[++cnt] = edges(h[u], v, w, c), h[u] = cnt;
  e[++cnt] = edges(h[v], u, 0, -c), h[v] = cnt;
}

bool bfs() {
  memset(dis, 0x3f, (T + 1) << 2);
  q.push(S), dis[S] = 0, f[S] = inf;
  while (!q.empty()) {
    int u = q.front();
    vis[u] = 0, q.pop();
    for (int i = h[u]; i; i = e[i].nxt) {
      int v = e[i].to;
      if (e[i].w && dis[u] + e[i].c < dis[v]) {
        pre[v] = i;
        f[v] = min(f[u], e[i].w);
        dis[v] = dis[u] + e[i].c;
        if (!vis[v])
          q.push(v), vis[v] = 1;
      }
    }
  }
  return dis[T] < 1e9;
}

int EK() {
  int res = 0;
  //  int tmp = 0;
  while (bfs()) {
    res += f[T] * dis[T];
    //    tmp += f[T];
    for (int u = T; u != S; u = e[pre[u] ^ 1].to) {
      e[pre[u]].w -= f[T], e[pre[u] ^ 1].w += f[T];
    }
  }
  //  printf("#%d\\n", tmp);
  return res;
}
} // namespace Flow

void solve() {
  cin >> N >> M;
  rep(i, 1, N) { scanf("%s", A[i] + 1); }
  Flow::S = N * M * 2 + 1, Flow::T = N * M * 2 + 2;
  rep(i, 1, N) rep(j, 1, M) Flow::addline(N * M + id(i, j), Flow::T, 1, 0);
  rep(i, 1, N) rep(j, 1, M) if (A[i][j] == 'o') {
    Flow::addline(Flow::S, id(i, j), 1, 0);
    vec.clear();
    dfs(i, j, 0);
    for (pii p : vec) {
      Flow::addline(id(i, j), N * M + id(p.first, p.second), 1,
                    -dis[p.first][p.second]);
    }
  }
  cout << -Flow::EK() << endl;
}

signed main() {
  //  int T = read();
  //  while (T--) solve();
  solve();
  return 0;
}