Problem p03020 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
typedef long long LL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL LMAX = 100;

LL ec = 1, to[2000005], cap[2000005], w[2000005], nxt[2000005],
   hed[1000005] = {0};
void add_edge(LL f, LL t, LL c, LL d) {
  ++ec;
  to[ec] = t;
  cap[ec] = c;
  w[ec] = d;
  nxt[ec] = hed[f];
  hed[f] = ec;
  ++ec;
  to[ec] = f;
  cap[ec] = 0;
  w[ec] = -d;
  nxt[ec] = hed[t];
  hed[t] = ec;
}

LL dis[1000005], inq[1000005], flw[1000005];
LL pre[1000005], prv[1000005];

queue<LL> q;
LL spfa(LL s, LL t) {
  memset(dis, INF, sizeof(dis));
  memset(inq, 0, sizeof(inq));
  memset(flw, 0, sizeof(flw));
  prv[t] = -1;
  q.push(s);
  inq[s] = 1;
  dis[s] = 0;
  flw[s] = INF;
  while (!q.empty()) {
    LL u = q.front();
    q.pop();
    inq[u] = 0;
    for (LL i = hed[u]; i; i = nxt[i]) {
      LL v = to[i];
      if (dis[v] > dis[u] + w[i] && cap[i]) {
        dis[v] = dis[u] + w[i];
        prv[v] = u;
        pre[v] = i;
        flw[v] = min(flw[u], cap[i]);
        if (!inq[v]) {
          inq[v] = 1;
          q.push(v);
        }
      }
    }
  }
  return prv[t] != -1;
}

LL mxf = 0, mcf = 0;
void mcmf(LL s, LL t) {
  while (spfa(s, t)) {
    mxf += flw[t];
    mcf += flw[t] * dis[t];
    LL now = t;
    while (now != s) {
      cap[pre[now]] -= flw[t];
      cap[pre[now] ^ 1] += flw[t];
      now = prv[now];
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  LL n, x, y, w, S, T;
  cin >> n;
  S = n + n + 5;
  T = S + 1;
  for (LL i = 1; i <= n; i++) {
    cin >> x >> y >> w;
    add_edge(S, i, w, 0);
    add_edge(i, n + n + 1, INF, +x + y);
    add_edge(i, n + n + 2, INF, +x - y);
    add_edge(i, n + n + 3, INF, -x + y);
    add_edge(i, n + n + 4, INF, -x - y);
  }
  for (LL i = 1; i <= n; i++) {
    cin >> x >> y >> w;
    add_edge(i + n, T, w, 0);
    add_edge(n + n + 1, i + n, INF, -x - y);
    add_edge(n + n + 2, i + n, INF, -x + y);
    add_edge(n + n + 3, i + n, INF, +x - y);
    add_edge(n + n + 4, i + n, INF, +x + y);
  }
  mcmf(S, T);
  cout << -mcf << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
typedef long long LL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL LMAX = 100;

LL ec = 1, to[2000005], cap[2000005], w[2000005], nxt[2000005],
   hed[1000005] = {0};
void add_edge(LL f, LL t, LL c, LL d) {
  ++ec;
  to[ec] = t;
  cap[ec] = c;
  w[ec] = d;
  nxt[ec] = hed[f];
  hed[f] = ec;
  ++ec;
  to[ec] = f;
  cap[ec] = 0;
  w[ec] = -d;
  nxt[ec] = hed[t];
  hed[t] = ec;
}

LL dis[4005], inq[4005], flw[4005];
LL pre[4005], prv[4005];

queue<LL> q;
LL spfa(LL s, LL t) {
  memset(dis, INF, sizeof(dis));
  memset(inq, 0, sizeof(inq));
  memset(flw, 0, sizeof(flw));
  prv[t] = -1;
  q.push(s);
  inq[s] = 1;
  dis[s] = 0;
  flw[s] = INF;
  while (!q.empty()) {
    LL u = q.front();
    q.pop();
    inq[u] = 0;
    for (LL i = hed[u]; i; i = nxt[i]) {
      LL v = to[i];
      if (dis[v] > dis[u] + w[i] && cap[i]) {
        dis[v] = dis[u] + w[i];
        prv[v] = u;
        pre[v] = i;
        flw[v] = min(flw[u], cap[i]);
        if (!inq[v]) {
          inq[v] = 1;
          q.push(v);
        }
      }
    }
  }
  return prv[t] != -1;
}

LL mxf = 0, mcf = 0;
void mcmf(LL s, LL t) {
  while (spfa(s, t)) {
    mxf += flw[t];
    mcf += flw[t] * dis[t];
    LL now = t;
    while (now != s) {
      cap[pre[now]] -= flw[t];
      cap[pre[now] ^ 1] += flw[t];
      now = prv[now];
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  LL n, x, y, w, S, T;
  cin >> n;
  S = n + n + 5;
  T = S + 1;
  for (LL i = 1; i <= n; i++) {
    cin >> x >> y >> w;
    add_edge(S, i, w, 0);
    add_edge(i, n + n + 1, INF, +x + y);
    add_edge(i, n + n + 2, INF, +x - y);
    add_edge(i, n + n + 3, INF, -x + y);
    add_edge(i, n + n + 4, INF, -x - y);
  }
  for (LL i = 1; i <= n; i++) {
    cin >> x >> y >> w;
    add_edge(i + n, T, w, 0);
    add_edge(n + n + 1, i + n, INF, -x - y);
    add_edge(n + n + 2, i + n, INF, -x + y);
    add_edge(n + n + 3, i + n, INF, +x - y);
    add_edge(n + n + 4, i + n, INF, +x + y);
  }
  mcmf(S, T);
  cout << -mcf << endl;
  return 0;
}