Problem p03020 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define SZ(x) ((int)x.size())
#define L(i, u) for (register int i = head[u]; i; i = nxt[i])
#define rep(i, a, b) for (register int i = (a); i <= (b); i++)
#define per(i, a, b) for (register int i = (a); i >= (b); i--)
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> Pii;
typedef vector<int> Vi;
template <class T> inline void read(T &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
template <class T> inline void umin(T &x, T y) { x = x < y ? x : y; }
template <class T> inline void umax(T &x, T y) { x = x > y ? x : y; }
inline ui R() {
  static ui seed = 416;
  return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13;
}
const int N = 25555;
int n, a[4], tot;
const int M = 200000;
const ll inf = 1e16;
int S, T, edgenum = 1, head[N], cur[N], to[M], nxt[M], cap[M], val[M];
inline void addedge(int u, int v, int c, int w) {
  to[++edgenum] = v;
  cap[edgenum] = c;
  val[edgenum] = w;
  nxt[edgenum] = head[u];
  head[u] = edgenum;
}
inline void add(int u, int v, int c, int w) {
  assert(u <= T && v <= T && u && v);
  w = -w;
  addedge(u, v, c, w);
  addedge(v, u, 0, -w);
}
int rec[N], q[N];
bool vis[N];
ll dis[N];
inline void inc(int &x) {
  x++;
  x = x == N ? 0 : x;
}
inline bool spfa() {
  int f = 0, r = 1;
  rep(i, 0, T) dis[i] = inf;
  dis[T] = 0;
  q[f] = T;
  while (f != r) {
    int u = q[f];
    inc(f);
    vis[u] = 0;
    L(i, u) if (cap[i ^ 1] && dis[to[i]] > dis[u] + val[i ^ 1]) {
      dis[to[i]] = dis[u] + val[i ^ 1];
      rec[to[i]] = i;
      if (!vis[to[i]]) {
        if (dis[to[i]] >= dis[q[f]])
          q[r] = to[i], inc(r);
        else
          f = f ? f - 1 : N - 1, q[f] = to[i];
        vis[to[i]] = 1;
      }
    }
  }
  return dis[S] < inf;
}
bool incir[N];
inline int dfs(int u, int flow) {
  if (u == T || !flow)
    return flow;
  assert(!incir[u]);
  int tot = 0;
  incir[u] = 1;
  for (register int &i = cur[u]; i; i = nxt[i])
    if (cap[i] && dis[u] == dis[to[i]] + val[i] && !incir[to[i]]) {
      int tmp = dfs(to[i], min(flow, cap[i]));
      cap[i] -= tmp;
      cap[i ^ 1] += tmp;
      tot += tmp;
      flow -= tmp;
      if (!flow)
        break;
    }
  incir[u] = 0;
  return tot;
}
inline ll solve() {
  ll res = 0;
  while (spfa()) {
    memcpy(cur, head, 4 * (T + 2));
    int qaq = dfs(S, 0x3f3f3f3f);
    res += 1LL * qaq * dis[S];
  }
  return -res;
}
int main() {
  read(n);
  S = 2 * n + 11;
  T = S + 1;
  tot = 2 * n + 1;
  rep(j, 0, 3) a[j] = tot + j;
  rep(i, 1, n) {
    int x, y, c;
    read(x);
    read(y);
    read(c);
    add(S, i, c, 0);
    add(i, a[0], c, -x - y);
    add(i, a[1], c, -x + y);
    add(i, a[2], c, x - y);
    add(i, a[3], c, x + y);
  }
  rep(i, 1, n) {
    int x, y, c;
    read(x);
    read(y);
    read(c);
    add(n + i, T, c, 0);
    add(a[0], n + i, c, x + y);
    add(a[1], n + i, c, x - y);
    add(a[2], n + i, c, -x + y);
    add(a[3], n + i, c, -x - y);
  }
  cout << solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define SZ(x) ((int)x.size())
#define L(i, u) for (register int i = head[u]; i; i = nxt[i])
#define rep(i, a, b) for (register int i = (a); i <= (b); i++)
#define per(i, a, b) for (register int i = (a); i >= (b); i--)
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> Pii;
typedef vector<int> Vi;
template <class T> inline void read(T &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
template <class T> inline void umin(T &x, T y) { x = x < y ? x : y; }
template <class T> inline void umax(T &x, T y) { x = x > y ? x : y; }
inline ui R() {
  static ui seed = 416;
  return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13;
}
const int N = 25555;
int n, a[4], tot;
const int M = 200000;
const ll inf = 1e16;
int S, T, edgenum = 1, head[N], cur[N], to[M], nxt[M], cap[M], val[M];
inline void addedge(int u, int v, int c, int w) {
  to[++edgenum] = v;
  cap[edgenum] = c;
  val[edgenum] = w;
  nxt[edgenum] = head[u];
  head[u] = edgenum;
}
inline void add(int u, int v, int c, int w) {
  assert(u <= T && v <= T && u && v);
  w = -w;
  addedge(u, v, c, w);
  addedge(v, u, 0, -w);
}
int rec[N], q[N];
bool vis[N];
ll dis[N];
inline void inc(int &x) {
  x++;
  x = x == N ? 0 : x;
}
inline bool spfa() {
  int f = 0, r = 1;
  rep(i, 0, T) dis[i] = inf;
  dis[T] = 0;
  q[f] = T;
  while (f != r) {
    int u = q[f];
    inc(f);
    vis[u] = 0;
    L(i, u) if (cap[i ^ 1] && dis[to[i]] > dis[u] + val[i ^ 1]) {
      dis[to[i]] = dis[u] + val[i ^ 1];
      rec[to[i]] = i;
      if (!vis[to[i]]) {
        q[r] = to[i], inc(r);
        vis[to[i]] = 1;
      }
    }
  }
  return dis[S] < inf;
}
bool incir[N];
inline int dfs(int u, int flow) {
  if (u == T || !flow)
    return flow;
  assert(!incir[u]);
  int tot = 0;
  incir[u] = 1;
  for (register int &i = cur[u]; i; i = nxt[i])
    if (cap[i] && dis[u] == dis[to[i]] + val[i] && !incir[to[i]]) {
      int tmp = dfs(to[i], min(flow, cap[i]));
      cap[i] -= tmp;
      cap[i ^ 1] += tmp;
      tot += tmp;
      flow -= tmp;
      if (!flow)
        break;
    }
  incir[u] = 0;
  return tot;
}
inline ll solve() {
  ll res = 0;
  while (spfa()) {
    memcpy(cur, head, 4 * (T + 2));
    int qaq = dfs(S, 0x3f3f3f3f);
    res += 1LL * qaq * dis[S];
  }
  return -res;
}
int main() {
  read(n);
  S = 2 * n + 11;
  T = S + 1;
  tot = 2 * n + 1;
  rep(j, 0, 3) a[j] = tot + j;
  rep(i, 1, n) {
    int x, y, c;
    read(x);
    read(y);
    read(c);
    add(S, i, c, 0);
    add(i, a[0], c, -x - y);
    add(i, a[1], c, -x + y);
    add(i, a[2], c, x - y);
    add(i, a[3], c, x + y);
  }
  rep(i, 1, n) {
    int x, y, c;
    read(x);
    read(y);
    read(c);
    add(n + i, T, c, 0);
    add(a[0], n + i, c, x + y);
    add(a[1], n + i, c, x - y);
    add(a[2], n + i, c, -x + y);
    add(a[3], n + i, c, -x - y);
  }
  cout << solve();
  return 0;
}