Problem p03020 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<LL, LL>
#define x first
#define y second
#define N 131072
#define pi acos(-1)
#define sqr(x) ((x) * (x))
#define pdd pair<double, double>
#define MEMS(x) memset(x, -1, sizeof(x))
#define MEM(x) memset(x, 0, sizeof(x))
#define MXN 2005
#define MXN 2005
struct CostFlow {
  struct Edge {
    int v, r;
    long long f, c;
    Edge(int a, int b, int _c, int d) : v(a), r(b), f(_c), c(d) {}
  };
  int n, s, t, prv[MXN], prvL[MXN], inq[MXN];
  LL dis[MXN], fl, cost;
  vector<Edge> E[MXN];
  void init(int _n, int _s, int _t) {
    n = _n;
    s = _s;
    t = _t;
    for (int i = 0; i < n; i++)
      E[i].clear();
    fl = cost = 0;
  }
  void add_edge(int u, int v, long long f, long long c) {
    E[u].pb(Edge(v, E[v].size(), f, c));
    E[v].pb(Edge(u, E[u].size() - 1, 0, -c));
  }
  void SPFA() {
    fill(dis, dis + n + 1, -1e18);
    fill(inq, inq + n + 1, 0);
    dis[s] = 0;
    queue<int> q;
    q.push(s);
    while (!q.empty()) {
      int x = q.front();
      q.pop();
      inq[x] = 0;
      int cnt = 0;
      for (auto it : E[x]) {
        if (it.f > 0 && dis[it.v] < dis[x] + it.c) {
          prv[it.v] = x;
          prvL[it.v] = cnt;
          dis[it.v] = dis[x] + it.c;
          if (!inq[it.v]) {
            inq[it.v] = 1;
            q.push(it.v);
          }
        }
        cnt++;
      }
    }
  }
  pll flow() {
    while (true) {
      SPFA();
      if (dis[t] == -1e18)
        break;
      long long tf = 1e18;
      for (int v = t, u, l; v != s; v = u) {
        u = prv[v];
        l = prvL[v];
        tf = min(tf, E[u][l].f);
      }
      for (int v = t, u, l; v != s; v = u) {
        u = prv[v];
        l = prvL[v];
        E[u][l].f -= tf;
        E[v][E[u][l].r].f += tf;
      }
      cost += tf * dis[t];
      fl += tf;
    }
    return {fl, cost};
  }
} flow;

int main() {
  int n;
  scanf("%d", &n);
  flow.init(2 * n + 10, 0, 2 * n + 5);
  pair<pii, int> r[1005], b[1005];
  for (int i = 1; i <= n; i++) {
    int x, y, c;
    scanf("%d %d %d", &x, &y, &c);
    r[i] = mp(mp(x, y), c);
    flow.add_edge(0, i, c, 0);
    flow.add_edge(i, n + 1, c, x + y);
    flow.add_edge(i, n + 2, c, x - y);
    flow.add_edge(i, n + 3, c, y - x);
    flow.add_edge(i, n + 4, c, -y - x);
  }
  for (int i = 1; i <= n; i++) {
    int x, y, c;
    scanf("%d %d %d", &x, &y, &c);
    b[i] = mp(mp(x, y), c);
    flow.add_edge(n + i + 4, 2 * n + 5, c, 0);
    flow.add_edge(n + 1, n + i + 4, c, -x - y);
    flow.add_edge(n + 2, n + i + 4, c, y - x);
    flow.add_edge(n + 3, n + i + 4, c, x - y);
    flow.add_edge(n + 4, n + i + 4, c, y + x);
  }
  printf("%lld\n", flow.flow().y);
}
/*
230
0.5 0.5
0.5 0.25
0.25 0.125
0.25
*/

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<LL, LL>
#define x first
#define y second
#define N 131072
#define pi acos(-1)
#define sqr(x) ((x) * (x))
#define pdd pair<double, double>
#define MEMS(x) memset(x, -1, sizeof(x))
#define MEM(x) memset(x, 0, sizeof(x))
#define MXN 3005
struct CostFlow {
  struct Edge {
    int v, r;
    long long f, c;
    Edge(int a, int b, int _c, int d) : v(a), r(b), f(_c), c(d) {}
  };
  int n, s, t, prv[MXN], prvL[MXN], inq[MXN];
  LL dis[MXN], fl, cost;
  vector<Edge> E[MXN];
  void init(int _n, int _s, int _t) {
    n = _n;
    s = _s;
    t = _t;
    for (int i = 0; i < n; i++)
      E[i].clear();
    fl = cost = 0;
  }
  void add_edge(int u, int v, long long f, long long c) {
    E[u].pb(Edge(v, E[v].size(), f, c));
    E[v].pb(Edge(u, E[u].size() - 1, 0, -c));
  }
  void SPFA() {
    fill(dis, dis + n + 1, -1e18);
    fill(inq, inq + n + 1, 0);
    dis[s] = 0;
    queue<int> q;
    q.push(s);
    while (!q.empty()) {
      int x = q.front();
      q.pop();
      inq[x] = 0;
      int cnt = 0;
      for (auto it : E[x]) {
        if (it.f > 0 && dis[it.v] < dis[x] + it.c) {
          prv[it.v] = x;
          prvL[it.v] = cnt;
          dis[it.v] = dis[x] + it.c;
          if (!inq[it.v]) {
            inq[it.v] = 1;
            q.push(it.v);
          }
        }
        cnt++;
      }
    }
  }
  pll flow() {
    while (true) {
      SPFA();
      if (dis[t] == -1e18)
        break;
      long long tf = 1e18;
      for (int v = t, u, l; v != s; v = u) {
        u = prv[v];
        l = prvL[v];
        tf = min(tf, E[u][l].f);
      }
      for (int v = t, u, l; v != s; v = u) {
        u = prv[v];
        l = prvL[v];
        E[u][l].f -= tf;
        E[v][E[u][l].r].f += tf;
      }
      cost += tf * dis[t];
      fl += tf;
    }
    return {fl, cost};
  }
} flow;

int main() {
  int n;
  scanf("%d", &n);
  flow.init(2 * n + 10, 0, 2 * n + 5);
  pair<pii, int> r[1005], b[1005];
  for (int i = 1; i <= n; i++) {
    int x, y, c;
    scanf("%d %d %d", &x, &y, &c);
    r[i] = mp(mp(x, y), c);
    flow.add_edge(0, i, c, 0);
    flow.add_edge(i, n + 1, c, x + y);
    flow.add_edge(i, n + 2, c, x - y);
    flow.add_edge(i, n + 3, c, y - x);
    flow.add_edge(i, n + 4, c, -y - x);
  }
  for (int i = 1; i <= n; i++) {
    int x, y, c;
    scanf("%d %d %d", &x, &y, &c);
    b[i] = mp(mp(x, y), c);
    flow.add_edge(n + i + 4, 2 * n + 5, c, 0);
    flow.add_edge(n + 1, n + i + 4, c, -x - y);
    flow.add_edge(n + 2, n + i + 4, c, y - x);
    flow.add_edge(n + 3, n + i + 4, c, x - y);
    flow.add_edge(n + 4, n + i + 4, c, y + x);
  }
  printf("%lld\n", flow.flow().y);
}
/*
230
0.5 0.5
0.5 0.25
0.25 0.125
0.25
*/