Problem p03099 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#define ll long long
using namespace std;
const int INF = 2147483647;
struct Edge {
  int to, nxt;
  int flow;
  ll cost;
} e[10005];
int n, m, s, t, edgenum, head[1005], f[1005], pre[1005], x[85], y[85], a[325],
    b[325], l1[85], r1[85], l2[85], r2[85];
ll dis[1005], val[85];
bool flag[1005];
char ti[325];
queue<int> q;
void add(int u, int v, int f, ll c) {
  e[++edgenum].cost = c;
  e[edgenum].flow = f;
  e[edgenum].to = v;
  e[edgenum].nxt = head[u];
  head[u] = edgenum;
}
bool SPFA() {
  for (int i = 1; i <= t; i++)
    dis[i] = 1e18, flag[i] = 0, f[i] = 0;
  dis[s] = 0;
  f[s] = INF;
  flag[s] = 1;
  q.push(s);
  while (!q.empty()) {
    int node = q.front();
    q.pop();
    flag[node] = 0;
    for (int hd = head[node]; hd; hd = e[hd].nxt) {
      int to = e[hd].to;
      if (flag[to])
        continue;
      if (e[hd].flow == 0)
        continue;
      if (dis[to] > dis[node] + e[hd].cost) {
        dis[to] = dis[node] + e[hd].cost;
        f[to] = min(f[node], e[hd].flow);
        pre[to] = hd;
        q.push(to);
      }
    }
  }
  return dis[t] < 0;
}
ll EK() {
  ll ans = 0;
  while (SPFA()) {
    ans += f[t] * dis[t];
    int x = t;
    while (x) {
      e[pre[x]].flow -= f[t];
      e[pre[x] ^ 1].flow += f[t];
      x = e[pre[x] ^ 1].to;
    }
  }
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d%lld", &x[i], &y[i], &val[i]);
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    while (ti[i] < 'A' || ti[i] > 'Z')
      ti[i] = (char)getchar();
    scanf("%d%d", &b[i], &a[i]);
  }
  ll ans = 0;
  for (int c = 1; c <= n; c++) {
    for (int i = 1; i <= c; i++)
      l1[i] = l2[i] = 1, r1[i] = r2[i] = 100;
    for (int i = 1; i <= m; i++) {
      if (ti[i] == 'L')
        if (a[i] + 1 <= c)
          l1[a[i] + 1] = max(l1[a[i] + 1], b[i] + 1);
      if (ti[i] == 'R')
        if (a[i] + 1 <= c)
          r1[c - a[i]] = min(r1[c - a[i]], b[i] - 1);
      if (ti[i] == 'D')
        if (a[i] + 1 <= c)
          l2[a[i] + 1] = max(l2[a[i] + 1], b[i] + 1);
      if (ti[i] == 'U')
        if (a[i] + 1 <= c)
          r2[c - a[i]] = min(r2[c - a[i]], b[i] - 1);
    }
    for (int i = 2; i <= c; i++) {
      l1[i] = max(l1[i], l1[i - 1]);
      l2[i] = max(l2[i], l2[i - 1]);
    }
    for (int i = c - 1; i >= 1; i--) {
      r1[i] = min(r1[i], r1[i + 1]);
      r2[i] = min(r2[i], r2[i + 1]);
    }
    bool flag = 1;
    for (int i = 1; i <= c; i++) {
      if (l1[i] > r1[i])
        flag = 0;
      if (l2[i] > r2[i])
        flag = 0;
    }
    if (!flag)
      continue;
    s = 2 * c + 2 * n + 1, t = 2 * c + 2 * n + 2;
    edgenum = 1;
    for (int i = 1; i <= t; i++)
      head[i] = 0;
    for (int i = 1; i <= c; i++) {
      add(s, i + 2 * n, 1, 0);
      add(i + 2 * n, s, 0, 0);
      add(i + 2 * n + c, t, 1, 0);
      add(t, i + 2 * n + c, 0, 0);
      for (int j = 1; j <= n; j++) {
        if (x[j] >= l1[i] && x[j] <= r1[i]) {
          add(i + 2 * n, j, 1, 0);
          add(j, i + 2 * n, 0, 0);
        }
        if (y[j] >= l2[i] && y[j] <= r2[i]) {
          add(j + n, i + 2 * n + c, 1, 0);
          add(i + 2 * n + c, j + n, 0, 0);
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      add(i, i + n, 1, -val[i]);
      add(i + n, i, 0, val[i]);
    }
    ans = max(ans, -EK());
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#define ll long long
using namespace std;
const int INF = 2147483647;
struct Edge {
  int to, nxt;
  int flow;
  ll cost;
} e[100005];
int n, m, s, t, edgenum, head[1005], f[1005], pre[1005], x[85], y[85], a[325],
    b[325], l1[85], r1[85], l2[85], r2[85];
ll dis[1005], val[85];
bool flag[1005];
char ti[325];
queue<int> q;
void add(int u, int v, int f, ll c) {
  e[++edgenum].cost = c;
  e[edgenum].flow = f;
  e[edgenum].to = v;
  e[edgenum].nxt = head[u];
  head[u] = edgenum;
}
bool SPFA() {
  for (int i = 1; i <= t; i++)
    dis[i] = 1e18, flag[i] = 0, f[i] = 0;
  dis[s] = 0;
  f[s] = INF;
  flag[s] = 1;
  q.push(s);
  while (!q.empty()) {
    int node = q.front();
    q.pop();
    flag[node] = 0;
    for (int hd = head[node]; hd; hd = e[hd].nxt) {
      int to = e[hd].to;
      if (flag[to])
        continue;
      if (e[hd].flow == 0)
        continue;
      if (dis[to] > dis[node] + e[hd].cost) {
        dis[to] = dis[node] + e[hd].cost;
        f[to] = min(f[node], e[hd].flow);
        pre[to] = hd;
        q.push(to);
      }
    }
  }
  return dis[t] < 0;
}
ll EK() {
  ll ans = 0;
  while (SPFA()) {
    ans += f[t] * dis[t];
    int x = t;
    while (x) {
      e[pre[x]].flow -= f[t];
      e[pre[x] ^ 1].flow += f[t];
      x = e[pre[x] ^ 1].to;
    }
  }
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d%lld", &x[i], &y[i], &val[i]);
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    while (ti[i] < 'A' || ti[i] > 'Z')
      ti[i] = (char)getchar();
    scanf("%d%d", &b[i], &a[i]);
  }
  ll ans = 0;
  for (int c = 1; c <= n; c++) {
    for (int i = 1; i <= c; i++)
      l1[i] = l2[i] = 1, r1[i] = r2[i] = 100;
    for (int i = 1; i <= m; i++) {
      if (ti[i] == 'L')
        if (a[i] + 1 <= c)
          l1[a[i] + 1] = max(l1[a[i] + 1], b[i] + 1);
      if (ti[i] == 'R')
        if (a[i] + 1 <= c)
          r1[c - a[i]] = min(r1[c - a[i]], b[i] - 1);
      if (ti[i] == 'D')
        if (a[i] + 1 <= c)
          l2[a[i] + 1] = max(l2[a[i] + 1], b[i] + 1);
      if (ti[i] == 'U')
        if (a[i] + 1 <= c)
          r2[c - a[i]] = min(r2[c - a[i]], b[i] - 1);
    }
    for (int i = 2; i <= c; i++) {
      l1[i] = max(l1[i], l1[i - 1]);
      l2[i] = max(l2[i], l2[i - 1]);
    }
    for (int i = c - 1; i >= 1; i--) {
      r1[i] = min(r1[i], r1[i + 1]);
      r2[i] = min(r2[i], r2[i + 1]);
    }
    bool flag = 1;
    for (int i = 1; i <= c; i++) {
      if (l1[i] > r1[i])
        flag = 0;
      if (l2[i] > r2[i])
        flag = 0;
    }
    if (!flag)
      continue;
    s = 2 * c + 2 * n + 1, t = 2 * c + 2 * n + 2;
    edgenum = 1;
    for (int i = 1; i <= t; i++)
      head[i] = 0;
    for (int i = 1; i <= c; i++) {
      add(s, i + 2 * n, 1, 0);
      add(i + 2 * n, s, 0, 0);
      add(i + 2 * n + c, t, 1, 0);
      add(t, i + 2 * n + c, 0, 0);
      for (int j = 1; j <= n; j++) {
        if (x[j] >= l1[i] && x[j] <= r1[i]) {
          add(i + 2 * n, j, 1, 0);
          add(j, i + 2 * n, 0, 0);
        }
        if (y[j] >= l2[i] && y[j] <= r2[i]) {
          add(j + n, i + 2 * n + c, 1, 0);
          add(i + 2 * n + c, j + n, 0, 0);
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      add(i, i + n, 1, -val[i]);
      add(i + n, i, 0, val[i]);
    }
    ans = max(ans, -EK());
  }
  printf("%lld\n", ans);
  return 0;
}