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

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

// Start
typedef long long ll;
typedef double db;
#define mp(a, b) make_pair((a), (b))
#define x first
#define y second
#define be(a) (a).begin()
#define en(a) (a).end()
#define sz(a) int((a).size())
#define pb(a) push_back(a)
#define R(i, a, b) for (int i = (a), I = (b); i < I; i++)
#define L(i, a, b) for (int i = (b)-1, I = (a)-1; i > I; i--)
const int iinf = 0x3f3f3f3f;
const ll linf = 0x3f3f3f3f3f3f3f3f;

// NetWorkFlow
const int tN = 10002;
int tn, s, t;
vector<int> e[tN], to, fw, co;
void adde(int u, int v, int w, int c) {
  e[u].pb(sz(to)), to.pb(v), fw.pb(w), co.pb(+c);
  e[v].pb(sz(to)), to.pb(u), fw.pb(0), co.pb(-c);
}
bool vis[tN];
int pre[tN], dep[tN];
bool spfa() {
  fill(vis, vis + tn, false);
  fill(pre, pre + tn, -1);
  fill(dep, dep + tn, -iinf);
  queue<int> q;
  q.push(s), dep[s] = 0, vis[s] = true;
  while (sz(q)) {
    int u = q.front();
    q.pop(), vis[u] = false;
    for (int v : e[u])
      if (fw[v] && dep[to[v]] < dep[u] + co[v]) {
        dep[to[v]] = dep[u] + co[v], pre[to[v]] = v;
        if (!vis[to[v]])
          vis[to[v]] = true, q.push(to[v]);
      }
  }
  return dep[t] != -iinf;
}
pair<int, int> mcmf() {
  pair<int, int> res(0, 0);
  while (spfa()) {
    //		cout<<"lycfucking.\\n";
    int f = iinf;
    for (int i = t; i != s; i = to[pre[i] ^ 1])
      f = min(f, fw[pre[i]]);
    for (int i = t; i != s; i = to[pre[i] ^ 1])
      fw[pre[i]] -= f, fw[pre[i] ^ 1] += f;
    res.x += f, res.y += dep[t] * f;
    //		cout<<"fengyangyang\\n";
  }
  return res;
}

// Data
const int N = 100;
int n, m;
char Map[N][N];
#define p(x, y) ((x) * (m) + (y))

// Main
int main() {
  //	ios::sync_with_stdio(0);
  //	cin.tie(0),cout.tie(0);
  cin >> n >> m;
  tn = (t = (s = n * m) + 1) + 1;
  R(i, 0, n) cin >> Map[i];
  R(i, 0, n) R(j, 0, m) if (Map[i][j] != '#') {
    if (Map[i][j] == 'o')
      adde(s, p(i, j), 1, 0);
    if (i + 1 < n && Map[i + 1][j] != '#')
      adde(p(i, j), p(i + 1, j), iinf, 1);
    if (j + 1 < m && Map[i][j + 1] != '#')
      adde(p(i, j), p(i, j + 1), iinf, 1);
    adde(p(i, j), t, 1, 0);
  }
  //	cout<<"?\\n";
  pair<int, int> ans = mcmf();
  //	cout<<"!\\n";
  cout << ans.y << '\\n';
  return 0;
}

Error: 0

Fixed submission:

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

// Start
typedef long long ll;
typedef double db;
#define mp(a, b) make_pair((a), (b))
#define x first
#define y second
#define be(a) (a).begin()
#define en(a) (a).end()
#define sz(a) int((a).size())
#define pb(a) push_back(a)
#define R(i, a, b) for (int i = (a), I = (b); i < I; i++)
#define L(i, a, b) for (int i = (b)-1, I = (a)-1; i > I; i--)
const int iinf = 0x3f3f3f3f;
const ll linf = 0x3f3f3f3f3f3f3f3f;

// NetWorkFlow
const int tN = 252;
int tn, s, t;
vector<int> e[tN], to, fw, co;
void adde(int u, int v, int w, int c) {
  e[u].pb(sz(to)), to.pb(v), fw.pb(w), co.pb(+c);
  e[v].pb(sz(to)), to.pb(u), fw.pb(0), co.pb(-c);
}
bool vis[tN];
int pre[tN], dep[tN];
bool spfa() {
  fill(vis, vis + tn, false);
  fill(pre, pre + tn, -1);
  fill(dep, dep + tn, -iinf);
  queue<int> q;
  q.push(s), dep[s] = 0, vis[s] = true;
  while (sz(q)) {
    int u = q.front();
    q.pop(), vis[u] = false;
    for (int v : e[u])
      if (fw[v] && dep[to[v]] < dep[u] + co[v]) {
        dep[to[v]] = dep[u] + co[v], pre[to[v]] = v;
        if (!vis[to[v]])
          vis[to[v]] = true, q.push(to[v]);
      }
  }
  return dep[t] != -iinf;
}
pair<int, int> mcmf() {
  pair<int, int> res(0, 0);
  while (spfa()) {
    //		cout<<"lycfucking.\\n";
    int f = iinf;
    for (int i = t; i != s; i = to[pre[i] ^ 1])
      f = min(f, fw[pre[i]]);
    for (int i = t; i != s; i = to[pre[i] ^ 1])
      fw[pre[i]] -= f, fw[pre[i] ^ 1] += f;
    res.x += f, res.y += dep[t] * f;
    //		cout<<"fengyangyang\\n";
  }
  return res;
}

// Data
const int N = 50;
int n, m;
char Map[N][N];
#define p(x, y) ((x) * (m) + (y))

// Main
int main() {
  //	ios::sync_with_stdio(0);
  //	cin.tie(0),cout.tie(0);
  cin >> n >> m;
  tn = (t = (s = n * m) + 1) + 1;
  R(i, 0, n) cin >> Map[i];
  R(i, 0, n) R(j, 0, m) if (Map[i][j] != '#') {
    if (Map[i][j] == 'o')
      adde(s, p(i, j), 1, 0);
    if (i + 1 < n && Map[i + 1][j] != '#')
      adde(p(i, j), p(i + 1, j), iinf, 1);
    if (j + 1 < m && Map[i][j + 1] != '#')
      adde(p(i, j), p(i, j + 1), iinf, 1);
    adde(p(i, j), t, 1, 0);
  }
  //	cout<<"?\\n";
  pair<int, int> ans = mcmf();
  //	cout<<"!\\n";
  cout << ans.y << '\\n';
  return 0;
}