Problem p03020 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

typedef long long ll;
const int Maxn = 2004;
const int Maxm = 10000;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x7FFFFFFF;

struct MinCostMaxFlow_Dinic {
  struct Edge {
    int to, cap;
    ll cos;
    Edge *nxt, *bak;
  };
  Edge pool[Maxm + 5];
  Edge *ecnt, *G[Maxn + 5];
  inline void addedge(int u, int v, int cap, ll c) {
    Edge *p = ++ecnt;
    p->to = v, p->cap = cap, p->cos = c;
    p->nxt = G[u], G[u] = p;
    p->bak = p + 1;
    p = ++ecnt;
    p->to = u, p->cap = 0, p->cos = -c;
    p->nxt = G[v], G[v] = p;
    p->bak = p - 1;
  }
  int node, s, t;
  ll d[Maxn + 5];
  Edge *cpy[Maxn + 5];
  ll cost;
  void init(int n) {
    ecnt = &pool[0];
    node = n;
    for (int i = 1; i <= n; i++)
      G[i] = NULL;
  }
  bool vis[Maxn + 5];
  int dfs(int u, int tot) {
    if (u == t)
      return tot;
    vis[u] = true;
    int sum = 0;
    for (; cpy[u] != NULL; cpy[u] = cpy[u]->nxt) {
      int v = cpy[u]->to, w = cpy[u]->cos;
      if (!vis[v] && d[v] == d[u] + w && cpy[u]->cap > 0) {
        int delta = dfs(v, min(tot - sum, cpy[u]->cap));
        cpy[u]->cap -= delta, cpy[u]->bak->cap += delta;
        cost += cpy[u]->cos * delta;
        sum += delta;
        if (sum == tot)
          break;
      }
    }
    vis[u] = false;
    return sum;
  }
  bool spfa(int _s, int _t) {
    static bool inq[Maxn + 5];
    memset(inq, false, sizeof inq);
    for (int i = 0; i <= node; i++)
      d[i] = -INFLL, cpy[i] = G[i];
    queue<int> q;
    q.push(_s), d[_s] = 0, inq[_s] = true;
    while (!q.empty()) {
      int u = q.front();
      q.pop(), inq[u] = false;
      for (Edge *p = G[u]; p != NULL; p = p->nxt) {
        int v = p->to;
        if (p->cap > 0 && d[v] < d[u] + p->cos) {
          d[v] = d[u] + p->cos;
          if (!inq[v]) {
            inq[v] = true;
            q.push(v);
          }
        }
      }
    }
    return d[_t] != -INFLL;
  }
  ll mcmf(int _s, int _t) {
    s = _s, t = _t, cost = 0;
    while (spfa(s, t)) {
      memset(vis, false, sizeof vis);
      dfs(s, INF);
    }
    return cost;
  }
};

int N;
MinCostMaxFlow_Dinic f;

int main() {
#ifdef LOACL
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  scanf("%d", &N);
  int S = 1, T = 2 * N + 6;
  f.init(2 * N + 6);
  for (int i = 1; i <= N; i++) {
    int x, y, val;
    scanf("%d %d %d", &x, &y, &val);
    f.addedge(S, i + 1, val, 0);
    f.addedge(i + 1, N + 2, INF, x + y);
    f.addedge(i + 1, N + 3, INF, -x + y);
    f.addedge(i + 1, N + 4, INF, x - y);
    f.addedge(i + 1, N + 5, INF, -x - y);
  }
  for (int i = 1; i <= N; i++) {
    int x, y, val;
    scanf("%d %d %d", &x, &y, &val);
    f.addedge(i + N + 5, T, val, 0);
    f.addedge(N + 2, i + N + 5, INF, -x - y);
    f.addedge(N + 3, i + N + 5, INF, x - y);
    f.addedge(N + 4, i + N + 5, INF, -x + y);
    f.addedge(N + 5, i + N + 5, INF, x + y);
  }
  printf("%lld\n", f.mcmf(S, T));
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

typedef long long ll;
const int Maxn = 20004;
const int Maxm = 100000;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x7FFFFFFF;

struct MinCostMaxFlow_Dinic {
  struct Edge {
    int to, cap;
    ll cos;
    Edge *nxt, *bak;
  };
  Edge pool[Maxm + 5];
  Edge *ecnt, *G[Maxn + 5];
  inline void addedge(int u, int v, int cap, ll c) {
    Edge *p = ++ecnt;
    p->to = v, p->cap = cap, p->cos = c;
    p->nxt = G[u], G[u] = p;
    p->bak = p + 1;
    p = ++ecnt;
    p->to = u, p->cap = 0, p->cos = -c;
    p->nxt = G[v], G[v] = p;
    p->bak = p - 1;
  }
  int node, s, t;
  ll d[Maxn + 5];
  Edge *cpy[Maxn + 5];
  ll cost;
  void init(int n) {
    ecnt = &pool[0];
    node = n;
    for (int i = 1; i <= n; i++)
      G[i] = NULL;
  }
  bool vis[Maxn + 5];
  int dfs(int u, int tot) {
    if (u == t)
      return tot;
    vis[u] = true;
    int sum = 0;
    for (; cpy[u] != NULL; cpy[u] = cpy[u]->nxt) {
      int v = cpy[u]->to, w = cpy[u]->cos;
      if (!vis[v] && d[v] == d[u] + w && cpy[u]->cap > 0) {
        int delta = dfs(v, min(tot - sum, cpy[u]->cap));
        cpy[u]->cap -= delta, cpy[u]->bak->cap += delta;
        cost += cpy[u]->cos * delta;
        sum += delta;
        if (sum == tot)
          break;
      }
    }
    vis[u] = false;
    return sum;
  }
  bool spfa(int _s, int _t) {
    static bool inq[Maxn + 5];
    memset(inq, false, sizeof inq);
    for (int i = 0; i <= node; i++)
      d[i] = -INFLL, cpy[i] = G[i];
    queue<int> q;
    q.push(_s), d[_s] = 0, inq[_s] = true;
    while (!q.empty()) {
      int u = q.front();
      q.pop(), inq[u] = false;
      for (Edge *p = G[u]; p != NULL; p = p->nxt) {
        int v = p->to;
        if (p->cap > 0 && d[v] < d[u] + p->cos) {
          d[v] = d[u] + p->cos;
          if (!inq[v]) {
            inq[v] = true;
            q.push(v);
          }
        }
      }
    }
    return d[_t] != -INFLL;
  }
  ll mcmf(int _s, int _t) {
    s = _s, t = _t, cost = 0;
    while (spfa(s, t)) {
      memset(vis, false, sizeof vis);
      dfs(s, INF);
    }
    return cost;
  }
};

int N;
MinCostMaxFlow_Dinic f;

int main() {
#ifdef LOACL
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  scanf("%d", &N);
  int S = 1, T = 2 * N + 6;
  f.init(2 * N + 6);
  for (int i = 1; i <= N; i++) {
    int x, y, val;
    scanf("%d %d %d", &x, &y, &val);
    f.addedge(S, i + 1, val, 0);
    f.addedge(i + 1, N + 2, INF, x + y);
    f.addedge(i + 1, N + 3, INF, -x + y);
    f.addedge(i + 1, N + 4, INF, x - y);
    f.addedge(i + 1, N + 5, INF, -x - y);
  }
  for (int i = 1; i <= N; i++) {
    int x, y, val;
    scanf("%d %d %d", &x, &y, &val);
    f.addedge(i + N + 5, T, val, 0);
    f.addedge(N + 2, i + N + 5, INF, -x - y);
    f.addedge(N + 3, i + N + 5, INF, x - y);
    f.addedge(N + 4, i + N + 5, INF, -x + y);
    f.addedge(N + 5, i + N + 5, INF, x + y);
  }
  printf("%lld\n", f.mcmf(S, T));
  return 0;
}