Problem p03099 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 100
#define M 400
#define V 200
#define E 1000
#define INF 1000000000000000000LL
using namespace std;
int n;
struct jewel {
  int x, y;
  long long v;
} J[N];
struct table {
  int n, a[N];
  void add(int x) { a[++n] = x; }
  void su() {
    sort(a + 1, a + n + 1);
    n = unique(a + 1, a + n + 1) - a - 1;
  }
  int lst(int x) { return upper_bound(a + 1, a + n + 1, x) - a - 1; }
  int nxt(int x) { return lower_bound(a + 1, a + n + 1, x) - a; }
} X, Y;
int m;
struct cond {
  char t;
  int a, b;
} C[M];
int e, head[V], nxt[E], to[E], cap[E], op[E];
long long val[E];
void add(int x, int y, int c, long long v) {
  nxt[++e] = head[x], to[head[x] = e] = y, cap[e] = c, val[e] = v,
  op[e] = e + 1;
  nxt[++e] = head[y], to[head[y] = e] = x, cap[e] = 0, val[e] = -v,
  op[e] = e - 1;
}
int S, T, from[V], from_edge[V];
long long f[V];
bool vis[V];
queue<int> q;
long long spfa() {
  for (int i = 0; i <= X.n + Y.n + 3; ++i)
    f[i] = -INF * 2;
  f[S] = 0, vis[S] = 1, q.push(S);
  while (!q.empty()) {
    int x = q.front();
    vis[x] = 0, q.pop();
    for (int i = head[x]; i; i = nxt[i])
      if (cap[i] && f[x] + val[i] > f[to[i]]) {
        f[to[i]] = f[x] + val[i];
        from[to[i]] = x, from_edge[to[i]] = i;
        if (!vis[to[i]])
          vis[to[i]] = 1, q.push(to[i]);
      }
  }
  return f[T];
}
int flow;
long long cost;
void augment() {
  int delta = N;
  for (int i = T; i != S; i = from[i])
    delta = min(delta, cap[from_edge[i]]);
  flow += delta;
  for (int i = T; i != S; i = from[i]) {
    cap[from_edge[i]] -= delta;
    cap[op[from_edge[i]]] += delta;
    if (val[from_edge[i]] == -INF)
      continue;
    cost += val[from_edge[i]] * delta;
  }
}
int R_min[N], R_max[N], U_min[N], U_max[N];
int in[V];
long long solve(int k) {
  int s = 0, t = X.n + 1, ss = X.n + Y.n + 2, tt = X.n + Y.n + 3;
  e = 0;
  for (int i = 0; i <= X.n + Y.n + 3; ++i)
    head[i] = 0;
  for (int i = 1; i <= X.n + 1; ++i)
    R_min[i] = 0, R_max[i] = N;
  for (int i = 1; i <= Y.n + 1; ++i)
    U_min[i] = 0, U_max[i] = N;
  R_max[X.n + 1] = U_max[Y.n + 1] = 0;
  R_min[1] = R_max[1] = k;
  for (int i = 1; i <= m; ++i)
    if (C[i].t == 'L')
      R_min[C[i].a + 1] = max(R_min[C[i].a + 1], k - C[i].b);
    else if (C[i].t == 'R')
      R_max[C[i].a] = min(R_max[C[i].a], C[i].b);
    else if (C[i].t == 'D')
      U_min[C[i].a + 1] = max(U_min[C[i].a + 1], k - C[i].b);
    else
      U_max[C[i].a] = min(U_max[C[i].a], C[i].b);
  for (int i = 1; i <= X.n + 1; ++i)
    if (R_min[i] > R_max[i])
      return 0;
  for (int i = 1; i <= Y.n + 1; ++i)
    if (U_min[i] > U_max[i])
      return 0;
  for (int i = 0; i <= X.n + Y.n + 1; ++i)
    in[i] = 0;
  for (int i = 1; i <= X.n; ++i) {
    add(i - 1, i, R_max[i] - R_min[i], 0);
    in[i] += R_min[i], in[i - 1] -= R_min[i];
  }
  for (int i = 1; i <= n; ++i)
    add(X.nxt(J[i].x), t + Y.nxt(J[i].y), 1, J[i].v);
  for (int i = 1; i <= Y.n; ++i) {
    add(t + i, t + i - 1, U_max[i] - U_min[i], 0);
    in[t + i] -= U_min[i], in[t + i - 1] += U_min[i];
  }
  int total = 0;
  for (int i = 0; i <= X.n + Y.n + 1; ++i)
    if (in[i] > 0)
      total += in[i], add(ss, i, in[i], 0);
    else if (in[i] < 0)
      add(i, tt, -in[i], 0);
  add(t, s, N, -INF);
  S = ss, T = tt, flow = 0, cost = 0;
  while (spfa() > -INF * 2)
    augment();
  if (flow < total)
    return 0;
  S = s, T = t;
  head[s] = nxt[head[s]];
  head[e] = nxt[head[t]];
  while (spfa() > 0)
    augment();
  return cost;
}
int main() {
  cin >> n;
  X.n = Y.n = 0;
  X.add(0), Y.add(0);
  X.add(101), Y.add(101);
  for (int i = 1; i <= n; ++i) {
    cin >> J[i].x >> J[i].y >> J[i].v;
    X.add(J[i].x), Y.add(J[i].y);
  }
  X.su(), Y.su();
  cin >> m;
  for (int i = 1; i <= m; ++i) {
    char ch = getchar();
    while (ch != 'L' && ch != 'R' && ch != 'D' && ch != 'U')
      ch = getchar();
    C[i].t = ch;
    cin >> C[i].a >> C[i].b;
    if (ch == 'L')
      C[i].a = X.lst(C[i].a);
    else if (ch == 'R')
      C[i].a = X.nxt(C[i].a);
    else if (ch == 'D')
      C[i].a = Y.lst(C[i].a);
    else
      C[i].a = Y.nxt(C[i].a);
  }
  long long ans = 0;
  for (int i = 1; i <= n; ++i)
    ans = max(ans, solve(i));
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define N 1000
#define M 1000
#define V 1000
#define E 10000
#define INF 1000000000000000000LL
using namespace std;
int n;
struct jewel {
  int x, y;
  long long v;
} J[N];
struct table {
  int n, a[N];
  void add(int x) { a[++n] = x; }
  void su() {
    sort(a + 1, a + n + 1);
    n = unique(a + 1, a + n + 1) - a - 1;
  }
  int lst(int x) { return upper_bound(a + 1, a + n + 1, x) - a - 1; }
  int nxt(int x) { return lower_bound(a + 1, a + n + 1, x) - a; }
} X, Y;
int m;
struct cond {
  char t;
  int a, b;
} C[M];
int e, head[V], nxt[E], to[E], cap[E], op[E];
long long val[E];
void add(int x, int y, int c, long long v) {
  nxt[++e] = head[x], to[head[x] = e] = y, cap[e] = c, val[e] = v,
  op[e] = e + 1;
  nxt[++e] = head[y], to[head[y] = e] = x, cap[e] = 0, val[e] = -v,
  op[e] = e - 1;
}
int S, T, from[V], from_edge[V];
long long f[V];
bool vis[V];
queue<int> q;
long long spfa() {
  for (int i = 0; i <= X.n + Y.n + 3; ++i)
    f[i] = -INF * 2;
  f[S] = 0, vis[S] = 1, q.push(S);
  while (!q.empty()) {
    int x = q.front();
    vis[x] = 0, q.pop();
    for (int i = head[x]; i; i = nxt[i])
      if (cap[i] && f[x] + val[i] > f[to[i]]) {
        f[to[i]] = f[x] + val[i];
        from[to[i]] = x, from_edge[to[i]] = i;
        if (!vis[to[i]])
          vis[to[i]] = 1, q.push(to[i]);
      }
  }
  return f[T];
}
int flow;
long long cost;
void augment() {
  int delta = N;
  for (int i = T; i != S; i = from[i])
    delta = min(delta, cap[from_edge[i]]);
  flow += delta;
  for (int i = T; i != S; i = from[i]) {
    cap[from_edge[i]] -= delta;
    cap[op[from_edge[i]]] += delta;
    if (val[from_edge[i]] == -INF)
      continue;
    cost += val[from_edge[i]] * delta;
  }
}
int R_min[N], R_max[N], U_min[N], U_max[N];
int in[V];
long long solve(int k) {
  int s = 0, t = X.n + 1, ss = X.n + Y.n + 2, tt = X.n + Y.n + 3;
  e = 0;
  for (int i = 0; i <= X.n + Y.n + 3; ++i)
    head[i] = 0;
  for (int i = 1; i <= X.n + 1; ++i)
    R_min[i] = 0, R_max[i] = N;
  for (int i = 1; i <= Y.n + 1; ++i)
    U_min[i] = 0, U_max[i] = N;
  R_max[X.n + 1] = U_max[Y.n + 1] = 0;
  R_min[1] = R_max[1] = k;
  for (int i = 1; i <= m; ++i)
    if (C[i].t == 'L')
      R_min[C[i].a + 1] = max(R_min[C[i].a + 1], k - C[i].b);
    else if (C[i].t == 'R')
      R_max[C[i].a] = min(R_max[C[i].a], C[i].b);
    else if (C[i].t == 'D')
      U_min[C[i].a + 1] = max(U_min[C[i].a + 1], k - C[i].b);
    else
      U_max[C[i].a] = min(U_max[C[i].a], C[i].b);
  for (int i = 1; i <= X.n + 1; ++i)
    if (R_min[i] > R_max[i])
      return 0;
  for (int i = 1; i <= Y.n + 1; ++i)
    if (U_min[i] > U_max[i])
      return 0;
  for (int i = 0; i <= X.n + Y.n + 1; ++i)
    in[i] = 0;
  for (int i = 1; i <= X.n; ++i) {
    add(i - 1, i, R_max[i] - R_min[i], 0);
    in[i] += R_min[i], in[i - 1] -= R_min[i];
  }
  for (int i = 1; i <= n; ++i)
    add(X.nxt(J[i].x), t + Y.nxt(J[i].y), 1, J[i].v);
  for (int i = 1; i <= Y.n; ++i) {
    add(t + i, t + i - 1, U_max[i] - U_min[i], 0);
    in[t + i] -= U_min[i], in[t + i - 1] += U_min[i];
  }
  int total = 0;
  for (int i = 0; i <= X.n + Y.n + 1; ++i)
    if (in[i] > 0)
      total += in[i], add(ss, i, in[i], 0);
    else if (in[i] < 0)
      add(i, tt, -in[i], 0);
  add(t, s, N, -INF);
  S = ss, T = tt, flow = 0, cost = 0;
  while (spfa() > -INF * 2)
    augment();
  if (flow < total)
    return 0;
  S = s, T = t;
  head[s] = nxt[head[s]];
  head[e] = nxt[head[t]];
  while (spfa() > 0)
    augment();
  return cost;
}
int main() {
  cin >> n;
  X.n = Y.n = 0;
  X.add(0), Y.add(0);
  X.add(101), Y.add(101);
  for (int i = 1; i <= n; ++i) {
    cin >> J[i].x >> J[i].y >> J[i].v;
    X.add(J[i].x), Y.add(J[i].y);
  }
  X.su(), Y.su();
  cin >> m;
  for (int i = 1; i <= m; ++i) {
    char ch = getchar();
    while (ch != 'L' && ch != 'R' && ch != 'D' && ch != 'U')
      ch = getchar();
    C[i].t = ch;
    cin >> C[i].a >> C[i].b;
    if (ch == 'L')
      C[i].a = X.lst(C[i].a);
    else if (ch == 'R')
      C[i].a = X.nxt(C[i].a);
    else if (ch == 'D')
      C[i].a = Y.lst(C[i].a);
    else
      C[i].a = Y.nxt(C[i].a);
  }
  long long ans = 0;
  for (int i = 1; i <= n; ++i)
    ans = max(ans, solve(i));
  cout << ans << endl;
}