Problem p03020 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
using ll = long long;
int lst[3005], to[10005], pre[10005], cap[10005], cost[10005], que[2000005],
    tot;
ll dis[3005];
bool in[3005], vis[3005];
inline void add_edge(int u, int v, int _cap, int _cost) {
  auto _add = [&](int x, int y, int ca, int co) {
    to[tot] = y;
    pre[tot] = lst[x];
    cap[tot] = ca;
    cost[tot] = co;
    lst[x] = tot++;
  };
  _add(u, v, _cap, _cost);
  _add(v, u, 0, -_cost);
}
int dfs(int u, int t, int rem) {
  if (u == t)
    return rem;
  vis[u] = true;
  int ans = 0;
  for (int i = lst[u]; ~i; i = pre[i]) {
    if (vis[to[i]] || !cap[i] || dis[to[i]] != dis[u] + cost[i])
      continue;
    int v = to[i], res = dfs(v, t, std::min(rem - ans, cap[i]));
    cap[i] -= res;
    cap[i ^ 1] += res;
    ans += res;
    if (ans == rem)
      return ans;
  }
  return ans;
}
inline ll min_cost_max_flow(int s, int t) {
  ll res = 0;
  while (true) {
    memset(dis, 0x3f, sizeof(dis));
    memset(in, false, sizeof(in));
    int he = 0, ta = 0;
    que[ta++] = s;
    dis[s] = 0;
    in[s] = true;
    auto update = [&]() {
      if (he < ta && dis[que[he]] > dis[que[ta - 1]])
        std::swap(que[he], que[ta - 1]);
    };
    while (he < ta) {
      int u = que[he++];
      in[u] = false;
      update();
      for (int i = lst[u]; ~i; i = pre[i]) {
        if (cap[i] && dis[to[i]] > dis[u] + cost[i]) {
          dis[to[i]] = dis[u] + cost[i];
          if (!in[to[i]]) {
            in[to[i]] = true;
            que[ta++] = to[i];
            update();
          }
        }
      }
    }
    if (dis[t] >= 1e9)
      break;
    while (true) {
      for (int i = 0; i < ta; i++)
        vis[que[i]] = false;
      int mn = dfs(s, t, 1e9);
      if (!mn)
        break;
      res += (ll)mn * dis[t];
    }
  }
  return res;
}
int main() {
  // freopen("AGC034-D.in", "r", stdin);
  memset(lst, -1, sizeof(lst));
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int x, y, k;
    scanf("%d%d%d", &x, &y, &k);
    add_edge(0, i + 1, k, 0);
    add_edge(i + 1, 2 * n + 1, k, x + y);
    add_edge(i + 1, 2 * n + 2, k, -x + y);
    add_edge(i + 1, 2 * n + 3, k, x - y);
    add_edge(i + 1, 2 * n + 4, k, -x - y);
  }
  for (int i = 0; i < n; i++) {
    int x, y, k;
    scanf("%d%d%d", &x, &y, &k);
    add_edge(n + i + 1, 2 * n + 5, k, 0);
    add_edge(2 * n + 1, n + i + 1, k, -x - y);
    add_edge(2 * n + 2, n + i + 1, k, x - y);
    add_edge(2 * n + 3, n + i + 1, k, -x + y);
    add_edge(2 * n + 4, n + i + 1, k, x + y);
  }
  printf("%lld\n", -min_cost_max_flow(0, 2 * n + 5));
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using ll = long long;
int lst[3005], to[20005], pre[20005], cap[20005], cost[20005], que[2000005],
    tot;
ll dis[3005];
bool in[3005], vis[3005];
inline void add_edge(int u, int v, int _cap, int _cost) {
  auto _add = [&](int x, int y, int ca, int co) {
    to[tot] = y;
    pre[tot] = lst[x];
    cap[tot] = ca;
    cost[tot] = co;
    lst[x] = tot++;
  };
  _add(u, v, _cap, _cost);
  _add(v, u, 0, -_cost);
}
int dfs(int u, int t, int rem) {
  if (u == t)
    return rem;
  vis[u] = true;
  int ans = 0;
  for (int i = lst[u]; ~i; i = pre[i]) {
    if (vis[to[i]] || !cap[i] || dis[to[i]] != dis[u] + cost[i])
      continue;
    int v = to[i], res = dfs(v, t, std::min(rem - ans, cap[i]));
    cap[i] -= res;
    cap[i ^ 1] += res;
    ans += res;
    if (ans == rem)
      return ans;
  }
  return ans;
}
inline ll min_cost_max_flow(int s, int t) {
  ll res = 0;
  while (true) {
    memset(dis, 0x3f, sizeof(dis));
    memset(in, false, sizeof(in));
    int he = 0, ta = 0;
    que[ta++] = s;
    dis[s] = 0;
    in[s] = true;
    auto update = [&]() {
      if (he < ta && dis[que[he]] > dis[que[ta - 1]])
        std::swap(que[he], que[ta - 1]);
    };
    while (he < ta) {
      int u = que[he++];
      in[u] = false;
      update();
      for (int i = lst[u]; ~i; i = pre[i]) {
        if (cap[i] && dis[to[i]] > dis[u] + cost[i]) {
          dis[to[i]] = dis[u] + cost[i];
          if (!in[to[i]]) {
            in[to[i]] = true;
            que[ta++] = to[i];
            update();
          }
        }
      }
    }
    if (dis[t] >= 1e9)
      break;
    while (true) {
      for (int i = 0; i < ta; i++)
        vis[que[i]] = false;
      int mn = dfs(s, t, 1e9);
      if (!mn)
        break;
      res += (ll)mn * dis[t];
    }
  }
  return res;
}
int main() {
  // freopen("AGC034-D.in", "r", stdin);
  memset(lst, -1, sizeof(lst));
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int x, y, k;
    scanf("%d%d%d", &x, &y, &k);
    add_edge(0, i + 1, k, 0);
    add_edge(i + 1, 2 * n + 1, k, x + y);
    add_edge(i + 1, 2 * n + 2, k, -x + y);
    add_edge(i + 1, 2 * n + 3, k, x - y);
    add_edge(i + 1, 2 * n + 4, k, -x - y);
  }
  for (int i = 0; i < n; i++) {
    int x, y, k;
    scanf("%d%d%d", &x, &y, &k);
    add_edge(n + i + 1, 2 * n + 5, k, 0);
    add_edge(2 * n + 1, n + i + 1, k, -x - y);
    add_edge(2 * n + 2, n + i + 1, k, x - y);
    add_edge(2 * n + 3, n + i + 1, k, -x + y);
    add_edge(2 * n + 4, n + i + 1, k, x + y);
  }
  printf("%lld\n", -min_cost_max_flow(0, 2 * n + 5));
  return 0;
}