Problem p03099 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

#define fo(i, j, l) for (int i = j; i <= l; ++i)
#define fd(i, j, l) for (int i = j; i >= l; --i)

using namespace std;
typedef long long ll;
const ll N = 600, M = N * 10, maxn = 1e18;

bool bz[N];
int x[N], y[N];
ll v[N];
int la[N], lb[M], ne[M], le[M];
ll cost[M];
int fr[N], f1[N];

int n, m, oo, S, T;
char c[N];
int a[N], b[N], d[M];
int p[N][2], q[N][2];
ll dis[N];
ll rec;

inline ll max(ll a, ll b) { return a > b ? a : b; }

inline int min(int a, int b) { return a < b ? a : b; }

inline void llb(int a, int b, int c, ll co) {
  ne[++oo] = la[a];
  la[a] = oo;
  lb[oo] = b;
  le[oo] = c;
  cost[oo] = co;
}

inline void gg(int a, int b, int c, ll d) {
  llb(a, b, c, d);
  llb(b, a, 0, -d);
}

inline bool flow() {
  fo(i, 1, T) dis[i] = maxn;
  dis[S] = 0;
  int l = 0, r = 1;
  d[1] = S;
  bz[S] = true;
  while (l < r) {
    int o = d[++l];
    for (int y = la[o]; y; y = ne[y])
      if (dis[o] - cost[y] < dis[lb[y]] && le[y] == 1) {
        dis[lb[y]] = dis[o] - cost[y];
        fr[lb[y]] = o;
        f1[lb[y]] = y;
        if (!bz[lb[y]])
          d[++r] = lb[y], bz[lb[y]] = true;
      }
    bz[o] = false;
  }
  if (dis[T] == maxn)
    return false;
  rec = 0;
  for (int u = T; u != S; u = fr[u]) {
    int k = f1[u];
    le[k] = 0;
    le[k ^ 1] = 1;
    rec = rec + cost[k];
  }
  return true;
}

int main() {
  scanf("%d", &n);
  fo(i, 1, n) scanf("%d%d%lld", &x[i], &y[i], &v[i]);
  scanf("%d", &m);
  fo(i, 1, m) {
    char ch = ' ';
    for (; ch != 'L' && ch != 'R' && ch != 'D' && ch != 'U'; ch = getchar())
      ;
    c[i] = ch;
    scanf("%d%d", &a[i], &b[i]);
  }
  ll ans = 0;
  fo(i, 1, n) {
    fo(l, 1, i) p[l][0] = 1, p[l][1] = 100;
    fo(l, 1, i) q[l][0] = 1, q[l][1] = 100;
    fo(l, 1, m) if (c[l] == 'L' && b[l] < i) {
      fo(j, b[l] + 1, i) p[j][0] = max(p[j][0], a[l] + 1);
    }
    else if (c[l] == 'R' && b[l] < i) {
      fo(j, 1, i - b[l]) p[j][1] = min(p[j][1], a[l] - 1);
    }
    else if (c[l] == 'U' && b[l] < i) {
      fo(j, 1, i - b[l]) q[j][1] = min(q[j][1], a[l] - 1);
    }
    else if (c[l] == 'D' && b[l] < i) {
      fo(j, b[l] + 1, i) q[j][0] = max(q[j][0], a[l] + 1);
    }
    int ok = 0;
    fo(l, 1, i) if (p[l][0] > p[l][1] || q[l][0] > q[l][1]) {
      ok = 1;
      break;
    }
    if (ok)
      continue;
    oo = 1;
    S = 2 * i + 200 + 1, T = S + 1;
    fo(j, 1, T) la[j] = 0;
    fo(l, 1, i) gg(S, l, 1, 0);
    fo(l, 1, i) gg(i + 200 + l, T, 1, 0);
    fo(j, 1, i) fo(l, p[j][0], p[j][1]) gg(j, i + l, 1, 0);
    fo(j, 1, i) fo(l, q[j][0], q[j][1]) gg(100 + i + l, 200 + i + j, 1, 0);
    fo(l, 1, n) gg(i + x[l], i + 100 + y[l], 1, v[l]);
    ll da = 0;
    int cs = 0;
    while (flow()) {
      da = da + rec;
      ++cs;
    }
    if (cs == i)
      ans = max(ans, da);
  }
  cout << ans;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

#define fo(i, j, l) for (int i = j; i <= l; ++i)
#define fd(i, j, l) for (int i = j; i >= l; --i)

using namespace std;
typedef long long ll;
const ll N = 1000, M = N * 50, maxn = 1e18;

bool bz[N];
int x[N], y[N];
ll v[N];
int la[N], lb[M], ne[M], le[M];
ll cost[M];
int fr[N], f1[N];

int n, m, oo, S, T;
char c[N];
int a[N], b[N], d[M];
int p[N][2], q[N][2];
ll dis[N];
ll rec;

inline ll max(ll a, ll b) { return a > b ? a : b; }

inline int min(int a, int b) { return a < b ? a : b; }

inline void llb(int a, int b, int c, ll co) {
  ne[++oo] = la[a];
  la[a] = oo;
  lb[oo] = b;
  le[oo] = c;
  cost[oo] = co;
}

inline void gg(int a, int b, int c, ll d) {
  llb(a, b, c, d);
  llb(b, a, 0, -d);
}

inline bool flow() {
  fo(i, 1, T) dis[i] = maxn;
  dis[S] = 0;
  int l = 0, r = 1;
  d[1] = S;
  bz[S] = true;
  while (l < r) {
    int o = d[++l];
    for (int y = la[o]; y; y = ne[y])
      if (dis[o] - cost[y] < dis[lb[y]] && le[y] == 1) {
        dis[lb[y]] = dis[o] - cost[y];
        fr[lb[y]] = o;
        f1[lb[y]] = y;
        if (!bz[lb[y]])
          d[++r] = lb[y], bz[lb[y]] = true;
      }
    bz[o] = false;
  }
  if (dis[T] == maxn)
    return false;
  rec = 0;
  for (int u = T; u != S; u = fr[u]) {
    int k = f1[u];
    le[k] = 0;
    le[k ^ 1] = 1;
    rec = rec + cost[k];
  }
  return true;
}

int main() {
  scanf("%d", &n);
  fo(i, 1, n) scanf("%d%d%lld", &x[i], &y[i], &v[i]);
  scanf("%d", &m);
  fo(i, 1, m) {
    char ch = ' ';
    for (; ch != 'L' && ch != 'R' && ch != 'D' && ch != 'U'; ch = getchar())
      ;
    c[i] = ch;
    scanf("%d%d", &a[i], &b[i]);
  }
  ll ans = 0;
  fo(i, 1, n) {
    fo(l, 1, i) p[l][0] = 1, p[l][1] = 100;
    fo(l, 1, i) q[l][0] = 1, q[l][1] = 100;
    fo(l, 1, m) if (c[l] == 'L' && b[l] < i) {
      fo(j, b[l] + 1, i) p[j][0] = max(p[j][0], a[l] + 1);
    }
    else if (c[l] == 'R' && b[l] < i) {
      fo(j, 1, i - b[l]) p[j][1] = min(p[j][1], a[l] - 1);
    }
    else if (c[l] == 'U' && b[l] < i) {
      fo(j, 1, i - b[l]) q[j][1] = min(q[j][1], a[l] - 1);
    }
    else if (c[l] == 'D' && b[l] < i) {
      fo(j, b[l] + 1, i) q[j][0] = max(q[j][0], a[l] + 1);
    }
    int ok = 0;
    fo(l, 1, i) if (p[l][0] > p[l][1] || q[l][0] > q[l][1]) {
      ok = 1;
      break;
    }
    if (ok)
      continue;
    oo = 1;
    S = 2 * i + 200 + 1, T = S + 1;
    fo(j, 1, T) la[j] = 0;
    fo(l, 1, i) gg(S, l, 1, 0);
    fo(l, 1, i) gg(i + 200 + l, T, 1, 0);
    fo(j, 1, i) fo(l, p[j][0], p[j][1]) gg(j, i + l, 1, 0);
    fo(j, 1, i) fo(l, q[j][0], q[j][1]) gg(100 + i + l, 200 + i + j, 1, 0);
    fo(l, 1, n) gg(i + x[l], i + 100 + y[l], 1, v[l]);
    ll da = 0;
    int cs = 0;
    while (flow()) {
      da = da + rec;
      ++cs;
    }
    if (cs == i)
      ans = max(ans, da);
  }
  cout << ans;
}