Problem p03020 (C++)

Buggy submission (status: Runtime Error):
// Legends Never Die.

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef unsigned int UI;
typedef pair<int, int> pii;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define fst first
#define snd second
#define SZ(u) ((int)(u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

namespace fastIO {
const UI BUFFER_SIZE = (UI)1 << 15;
char buffer[BUFFER_SIZE], *cur, *ed;

inline char getc() {
  return *(cur == ed    ? ed = buffer + fread(buffer, 1, BUFFER_SIZE, stdin),
           cur = buffer : cur)++;
}
} // namespace fastIO
using fastIO::getc;

template <typename T> inline T read() {
  register T sum(0), fg(1);
  register char ch(getc());
  for (; !isdigit(ch); ch = getc())
    if (ch == '-')
      fg = -1;
  for (; isdigit(ch); ch = getc())
    sum = sum * 10 - '0' + ch;
  return sum * fg;
}

template <typename T> inline int chkmin(T &a, const T &b) {
  return b < a ? a = b, 1 : 0;
}
template <typename T> inline int chkmax(T &a, const T &b) {
  return b > a ? a = b, 1 : 0;
}

// END tpl.

const int MAXN = (int)1e3;

struct ball {
  int x, y, c;

  ball() {}
  ball(int _x, int _y, int _c) : x(_x), y(_y), c(_c) {}
};

int n;

ball R[MAXN + 5], B[MAXN + 5];

inline void input() {
  n = read<int>();
  for (int i = 1; i <= n; ++i)
    R[i].x = read<int>(), R[i].y = read<int>(), R[i].c = read<int>();
  for (int i = 1; i <= n; ++i)
    B[i].x = read<int>(), B[i].y = read<int>(), B[i].c = read<int>();
}

const int INF = (int)1e9;

namespace FLOW {
const int MAX_NODE = MAXN * 10, MAX_EDGE = MAXN * 50;

struct edge {
  int v, c, w, nxt;

  edge() {}
  edge(int _v, int _c, int _w, int _nxt) : v(_v), c(_c), w(_w), nxt(_nxt) {}
};

edge e[MAX_EDGE * 2 + 5];
int st[MAX_NODE + 5], edge_cnt;

int N;

inline void init() { memset(st, -1, sizeof st), edge_cnt = 0; }
inline int new_node() { return ++N; }
inline void add_edge(int u, int v, int c, int w, int type = 1) {
  e[edge_cnt] = edge(v, c, w, st[u]), st[u] = edge_cnt++;
  if (type)
    add_edge(v, u, 0, -w, 0);
}

LL d[MAX_NODE + 5];

inline int SPFA(int S, int T) {
  static const LL INFD = (LL)1e18;

  static queue<int> q;
  static int inq[MAXN + 5];

  for (int i = 1; i <= N; ++i)
    inq[i] = 0, d[i] = -INFD;

  d[S] = 0, inq[S] = 1, q.push(S);
  while (!q.empty()) {
    int u = q.front();
    inq[u] = 0, q.pop();

    for (int i = st[u]; ~i; i = e[i].nxt) {
      int v = e[i].v;
      if (e[i].c > 0 && chkmax(d[v], d[u] + e[i].w) && !inq[v])
        inq[v] = 1, q.push(v);
    }
  }

  return d[T] > -INFD;
}

int vis[MAX_NODE + 5], cur[MAX_NODE + 5];

inline int dfs(int u, int low, int T) {
  if (u == T)
    return low;
  vis[u] = 1;

  int sum = 0;
  for (int &i = cur[u]; ~i; i = e[i].nxt) {
    int v = e[i].v, k;
    if (!vis[v] && d[v] == d[u] + e[i].w && e[i].c > 0 &&
        (k = dfs(v, min(e[i].c, low), T)) > 0) {
      low -= k, sum += k;
      e[i].c -= k, e[i ^ 1].c += k;
      if (low == 0)
        break;
    }
  }

  vis[u] = 0;
  return sum;
}

inline LL solve(int S, int T) {
  LL ans = 0;
  while (SPFA(S, T)) {
    for (int i = 1; i <= N; ++i)
      cur[i] = st[i];
    ans += (LL)dfs(S, INF, T) * d[T];
  }
  return ans;
}
} // namespace FLOW

inline void solve() {
  FLOW::init();
  int S = FLOW::new_node(), T = FLOW::new_node();

  static int id[2][2];
  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 2; ++j)
      id[i][j] = FLOW::new_node();

  for (int i = 1; i <= n; ++i) {
    int u = FLOW::new_node();
    FLOW::add_edge(S, u, R[i].c, 0);
    for (int p = 0; p < 2; ++p)
      for (int q = 0; q < 2; ++q)
        FLOW::add_edge(u, id[p][q], INF,
                       (2 * p - 1) * R[i].x + (2 * q - 1) * R[i].y);
  }

  for (int i = 1; i <= n; ++i) {
    int u = FLOW::new_node();
    FLOW::add_edge(u, T, B[i].c, 0);
    for (int p = 0; p < 2; ++p)
      for (int q = 0; q < 2; ++q)
        FLOW::add_edge(id[p][q], u, INF,
                       -(2 * p - 1) * B[i].x + -(2 * q - 1) * B[i].y);
  }

  printf("%lld\n", FLOW::solve(S, T));
}

int main() {
#ifdef K_ON // K-ON!
  freopen("D.in", "r", stdin);
  freopen("D.out", "w", stdout);
#endif

  input();
  solve();

  return 0;
}


Fixed submission:
// Legends Never Die.

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef unsigned int UI;
typedef pair<int, int> pii;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define fst first
#define snd second
#define SZ(u) ((int)(u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

namespace fastIO {
const UI BUFFER_SIZE = (UI)1 << 15;
char buffer[BUFFER_SIZE], *cur, *ed;

inline char getc() {
  return *(cur == ed    ? ed = buffer + fread(buffer, 1, BUFFER_SIZE, stdin),
           cur = buffer : cur)++;
}
} // namespace fastIO
using fastIO::getc;

template <typename T> inline T read() {
  register T sum(0), fg(1);
  register char ch(getc());
  for (; !isdigit(ch); ch = getc())
    if (ch == '-')
      fg = -1;
  for (; isdigit(ch); ch = getc())
    sum = sum * 10 - '0' + ch;
  return sum * fg;
}

template <typename T> inline int chkmin(T &a, const T &b) {
  return b < a ? a = b, 1 : 0;
}
template <typename T> inline int chkmax(T &a, const T &b) {
  return b > a ? a = b, 1 : 0;
}

// END tpl.

const int MAXN = (int)1e3;

struct ball {
  int x, y, c;

  ball() {}
  ball(int _x, int _y, int _c) : x(_x), y(_y), c(_c) {}
};

int n;

ball R[MAXN + 5], B[MAXN + 5];

inline void input() {
  n = read<int>();
  for (int i = 1; i <= n; ++i)
    R[i].x = read<int>(), R[i].y = read<int>(), R[i].c = read<int>();
  for (int i = 1; i <= n; ++i)
    B[i].x = read<int>(), B[i].y = read<int>(), B[i].c = read<int>();
}

const int INF = (int)1e9;

namespace FLOW {
const int MAX_NODE = MAXN * 10, MAX_EDGE = MAXN * 50;

struct edge {
  int v, c, w, nxt;

  edge() {}
  edge(int _v, int _c, int _w, int _nxt) : v(_v), c(_c), w(_w), nxt(_nxt) {}
};

edge e[MAX_EDGE * 2 + 5];
int st[MAX_NODE + 5], edge_cnt;

int N;

inline void init() { memset(st, -1, sizeof st), edge_cnt = 0; }
inline int new_node() { return ++N; }
inline void add_edge(int u, int v, int c, int w, int type = 1) {
  e[edge_cnt] = edge(v, c, w, st[u]), st[u] = edge_cnt++;
  if (type)
    add_edge(v, u, 0, -w, 0);
}

LL d[MAX_NODE + 5];

inline int SPFA(int S, int T) {
  static const LL INFD = (LL)1e18;

  static queue<int> q;
  static int inq[MAX_NODE + 5];

  for (int i = 1; i <= N; ++i)
    inq[i] = 0, d[i] = -INFD;

  d[S] = 0, inq[S] = 1, q.push(S);
  while (!q.empty()) {
    int u = q.front();
    inq[u] = 0, q.pop();

    for (int i = st[u]; ~i; i = e[i].nxt) {
      int v = e[i].v;
      if (e[i].c > 0 && chkmax(d[v], d[u] + e[i].w) && !inq[v])
        inq[v] = 1, q.push(v);
    }
  }

  return d[T] > -INFD;
}

int vis[MAX_NODE + 5], cur[MAX_NODE + 5];

inline int dfs(int u, int low, int T) {
  if (u == T)
    return low;
  vis[u] = 1;

  int sum = 0;
  for (int &i = cur[u]; ~i; i = e[i].nxt) {
    int v = e[i].v, k;
    if (!vis[v] && d[v] == d[u] + e[i].w && e[i].c > 0 &&
        (k = dfs(v, min(e[i].c, low), T)) > 0) {
      low -= k, sum += k;
      e[i].c -= k, e[i ^ 1].c += k;
      if (low == 0)
        break;
    }
  }

  vis[u] = 0;
  return sum;
}

inline LL solve(int S, int T) {
  LL ans = 0;
  while (SPFA(S, T)) {
    for (int i = 1; i <= N; ++i)
      cur[i] = st[i];
    ans += (LL)dfs(S, INF, T) * d[T];
  }
  return ans;
}
} // namespace FLOW

inline void solve() {
  FLOW::init();
  int S = FLOW::new_node(), T = FLOW::new_node();

  static int id[2][2];
  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 2; ++j)
      id[i][j] = FLOW::new_node();

  for (int i = 1; i <= n; ++i) {
    int u = FLOW::new_node();
    FLOW::add_edge(S, u, R[i].c, 0);
    for (int p = 0; p < 2; ++p)
      for (int q = 0; q < 2; ++q)
        FLOW::add_edge(u, id[p][q], INF,
                       (2 * p - 1) * R[i].x + (2 * q - 1) * R[i].y);
  }

  for (int i = 1; i <= n; ++i) {
    int u = FLOW::new_node();
    FLOW::add_edge(u, T, B[i].c, 0);
    for (int p = 0; p < 2; ++p)
      for (int q = 0; q < 2; ++q)
        FLOW::add_edge(id[p][q], u, INF,
                       -(2 * p - 1) * B[i].x + -(2 * q - 1) * B[i].y);
  }

  printf("%lld\n", FLOW::solve(S, T));
}

int main() {
#ifdef K_ON // K-ON!
  freopen("D.in", "r", stdin);
  freopen("D.out", "w", stdout);
#endif

  input();
  solve();

  return 0;
}