Problem p03020 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fo(i, a, b) for (int i = a, _e = b; i <= _e; ++i)
#define fd(i, a, b) for (int i = b, _e = a; i >= _e; --i)
#define ll long long
using namespace std;
const int N = 1005;
int n, x, y, w, i, js, la[N], T;
int d[N * N], fr[N];
ll ans, f[N];
bool vi[N];
struct edge {
  int ne, y, l, c;
} e[N * 20];
void link(int x, int y, int l, int c) {
  e[++js] = (edge){la[x], y, l, c};
  la[x] = js;
  e[++js] = (edge){la[y], x, 0, -c};
  la[y] = js;
}
int main() {
  cin >> n;
  js = 1;
  T = n + n + 5;
  fo(i, 1, n) {
    cin >> x >> y >> w;
    link(0, i, w, 0);
    link(i, n + n + 1, w, -x - y);
    link(i, n + n + 2, w, -x + y);
    link(i, n + n + 3, w, x - y);
    link(i, n + n + 4, w, x + y);
  }
  fo(i, 1, n) {
    cin >> x >> y >> w;
    link(n + i, T, w, 0);
    link(n + n + 1, n + i, w, x + y);
    link(n + n + 2, n + i, w, x - y);
    link(n + n + 3, n + i, w, -x + y);
    link(n + n + 4, n + i, w, -x - y);
  }
  for (;;) {
    fo(i, 0, T) f[i] = 1e18;
    f[0] = 0;
    vi[0] = 1;
    for (int l = 0, r = 1, x, y; x = d[++l], l <= r; vi[x] = 0)
      for (int i = la[x]; i; i = e[i].ne)
        if (e[i].l && f[x] + e[i].c < f[y = e[i].y]) {
          f[y] = f[x] + e[i].c;
          fr[y] = i ^ 1;
          if (!vi[y])
            d[++r] = y, vi[y] = 1;
        }
    if (f[T] == 1e18)
      break;
    int mi = 10000;
    for (int x = T; x; x = e[fr[x]].y)
      mi = min(mi, e[fr[x] ^ 1].l);
    ans += f[T] * mi;
    for (int x = T; x; x = e[fr[x]].y)
      e[fr[x]].l += mi, e[fr[x] ^ 1].l -= mi;
  }
  cout << -ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define fo(i, a, b) for (int i = a, _e = b; i <= _e; ++i)
#define fd(i, a, b) for (int i = b, _e = a; i >= _e; --i)
#define ll long long
using namespace std;
const int N = 1005;
int n, x, y, w, i, js, la[N * 2], T;
int d[N * N * 2], fr[N * 2];
ll ans, f[N * 2];
bool vi[N * 2];
struct edge {
  int ne, y, l, c;
} e[N * 20];
void link(int x, int y, int l, int c) {
  e[++js] = (edge){la[x], y, l, c};
  la[x] = js;
  e[++js] = (edge){la[y], x, 0, -c};
  la[y] = js;
}
int main() {
  cin >> n;
  js = 1;
  T = n + n + 5;
  fo(i, 1, n) {
    cin >> x >> y >> w;
    link(0, i, w, 0);
    link(i, n + n + 1, w, -x - y);
    link(i, n + n + 2, w, -x + y);
    link(i, n + n + 3, w, x - y);
    link(i, n + n + 4, w, x + y);
  }
  fo(i, 1, n) {
    cin >> x >> y >> w;
    link(n + i, T, w, 0);
    link(n + n + 1, n + i, w, x + y);
    link(n + n + 2, n + i, w, x - y);
    link(n + n + 3, n + i, w, -x + y);
    link(n + n + 4, n + i, w, -x - y);
  }
  for (;;) {
    fo(i, 0, T) f[i] = 1e18;
    f[0] = 0;
    vi[0] = 1;
    for (int l = 0, r = 1, x, y; x = d[++l], l <= r; vi[x] = 0)
      for (int i = la[x]; i; i = e[i].ne)
        if (e[i].l && f[x] + e[i].c < f[y = e[i].y]) {
          f[y] = f[x] + e[i].c;
          fr[y] = i ^ 1;
          if (!vi[y])
            d[++r] = y, vi[y] = 1;
        }
    if (f[T] == 1e18)
      break;
    int mi = 10000;
    for (int x = T; x; x = e[fr[x]].y)
      mi = min(mi, e[fr[x] ^ 1].l);
    ans += f[T] * mi;
    for (int x = T; x; x = e[fr[x]].y)
      e[fr[x]].l += mi, e[fr[x] ^ 1].l -= mi;
  }
  cout << -ans;
}