Problem p03020 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

typedef long long ll;

namespace CF {
#define ad(x) ((x - 1 ^ 1) + 1)

const int N = 2054, M = 8000054;
const ll INF = 0x3f3f3f3f3f3f3f3fll;

struct edge {
  int u, v, c, f;
  edge(int u0 = 0, int v0 = 0, int c0 = 0, int f0 = 0)
      : u(u0), v(v0), c(c0), f(f0) {}
} e[M];

int V, E, si = 1, ti = 2;
ll flow, cost;
int first[N], next[M];
int cur[N], que[M << 1];
ll dep[N];
bool in_que[N], used[N];

inline void addedge(int u, int v, int c, int f) {
  e[++E] = edge(u, v, c, f), next[E] = first[u], first[u] = E;
  e[++E] = edge(v, u, -c), next[E] = first[v], first[v] = E;
}

bool bfs() {
  int h = M, t = h + 1, i, x, y;
  memset(dep, 63, sizeof dep);
  que[h] = ti, dep[ti] = 0, in_que[ti] = true;
  for (; h < t;) {
    x = que[h++], in_que[x] = false;
    for (i = first[x]; i; i = next[i])
      if (dep[y = e[i].v] > dep[x] - e[i].c && e[ad(i)].f) {
        dep[y] = dep[x] - e[i].c;
        if (!in_que[y])
          in_que[y] = true, (dep[y] >= dep[que[h]] ? que[t++] : que[--h]) = y;
      }
  }
  return dep[si] < INF;
}

int dfs(int x, int lim) {
  int a, c, f = 0;
  if (x == ti || !lim)
    return lim;
  used[x] = true;
  for (int &i = cur[x]; i; i = next[i])
    if (dep[e[i].v] == dep[x] - e[i].c && e[i].f && !used[e[i].v]) {
      a = std::min(lim - f, e[i].f);
      c = dfs(e[i].v, a);
      e[i].f -= c, e[ad(i)].f += c;
      if ((f += c) == lim)
        return f;
    }
  return f;
}

void Dinic() {
  int f;
  for (cost = flow = 0; bfs();) {
    memcpy(cur, first, sizeof cur);
    memset(used, 0, sizeof used);
    flow += f = dfs(si, INT_MAX);
    cost += dep[si] * f;
  }
}
} // namespace CF

int main() {
  int i, n, w, x, y;
  scanf("%d", &n);
  for (i = 3; i < 3 + n; ++i) {
    scanf("%d%d%d", &x, &y, &w);
    CF::addedge(1, i, 0, w);
    CF::addedge(i, 3 + n * 2, x + y, w);
    CF::addedge(i, 4 + n * 2, x - y, w);
    CF::addedge(i, 5 + n * 2, -x + y, w);
    CF::addedge(i, 6 + n * 2, -x - y, w);
  }
  for (; i < 3 + n * 2; ++i) {
    scanf("%d%d%d", &x, &y, &w);
    CF::addedge(i, 2, 0, w);
    CF::addedge(3 + n * 2, i, -x - y, w);
    CF::addedge(4 + n * 2, i, -x + y, w);
    CF::addedge(5 + n * 2, i, x - y, w);
    CF::addedge(6 + n * 2, i, x + y, w);
  }
  CF::Dinic(), printf("%lld\n", -CF::cost);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

typedef long long ll;

namespace CF {
#define ad(x) ((x - 1 ^ 1) + 1)

const int N = 2054, M = 8000054;
const ll INF = 0x3f3f3f3f3f3f3f3fll;

struct edge {
  int u, v, c, f;
  edge(int u0 = 0, int v0 = 0, int c0 = 0, int f0 = 0)
      : u(u0), v(v0), c(c0), f(f0) {}
} e[M];

int V, E, si = 1, ti = 2;
ll flow, cost;
int first[N], next[M];
int cur[N], que[M << 1];
ll dep[N];
bool in_que[N], used[N];

inline void addedge(int u, int v, int c, int f) {
  e[++E] = edge(u, v, c, f), next[E] = first[u], first[u] = E;
  e[++E] = edge(v, u, -c), next[E] = first[v], first[v] = E;
}

bool bfs() {
  int h = M, t = h + 1, i, x, y;
  memset(dep, 63, sizeof dep);
  que[h] = ti, dep[ti] = 0, in_que[ti] = true;
  for (; h < t;) {
    x = que[h++], in_que[x] = false;
    for (i = first[x]; i; i = next[i])
      if (dep[y = e[i].v] > dep[x] - e[i].c && e[ad(i)].f) {
        dep[y] = dep[x] - e[i].c;
        if (!in_que[y])
          in_que[y] = true,
          (1 || dep[y] >= dep[que[h]] ? que[t++] : que[--h]) = y;
      }
  }
  return dep[si] < INF;
}

int dfs(int x, int lim) {
  int a, c, f = 0;
  if (x == ti || !lim)
    return lim;
  used[x] = true;
  for (int &i = cur[x]; i; i = next[i])
    if (dep[e[i].v] == dep[x] - e[i].c && e[i].f && !used[e[i].v]) {
      a = std::min(lim - f, e[i].f);
      c = dfs(e[i].v, a);
      e[i].f -= c, e[ad(i)].f += c;
      if ((f += c) == lim)
        return f;
    }
  return f;
}

void Dinic() {
  int f;
  for (cost = flow = 0; bfs();) {
    memcpy(cur, first, sizeof cur);
    memset(used, 0, sizeof used);
    flow += f = dfs(si, INT_MAX);
    cost += dep[si] * f;
  }
}
} // namespace CF

int main() {
  int i, n, w, x, y;
  scanf("%d", &n);
  for (i = 3; i < 3 + n; ++i) {
    scanf("%d%d%d", &x, &y, &w);
    CF::addedge(1, i, 0, w);
    CF::addedge(i, 3 + n * 2, x + y, w);
    CF::addedge(i, 4 + n * 2, x - y, w);
    CF::addedge(i, 5 + n * 2, -x + y, w);
    CF::addedge(i, 6 + n * 2, -x - y, w);
  }
  for (; i < 3 + n * 2; ++i) {
    scanf("%d%d%d", &x, &y, &w);
    CF::addedge(i, 2, 0, w);
    CF::addedge(3 + n * 2, i, -x - y, w);
    CF::addedge(4 + n * 2, i, -x + y, w);
    CF::addedge(5 + n * 2, i, x - y, w);
    CF::addedge(6 + n * 2, i, x + y, w);
  }
  CF::Dinic(), printf("%lld\n", -CF::cost);
  return 0;
}