Problem p03099 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define MP make_pair

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const LL INF = 0x3f3f3f3f3f3f3f;

namespace flow {
const int MAXN = 400;
struct edge {
  int to, cap;
  LL cost;
};
int n, m;
edge ed[30005];
vector<int> G[405];

void clear() {
  m = 0;
  rep(i, n) G[i].clear();
}

void add_edge(int u, int v, int fl, LL co) {
  ed[m].to = v;
  ed[m].cap = fl;
  ed[m].cost = co;
  G[u].push_back(m);
  m++;
  ed[m].to = u;
  ed[m].cap = 0;
  ed[m].cost = -co;
  G[v].push_back(m);
  m++;
}

LL h[405];
LL pat[405];
int prev[405];
bool vis[405];

bool dijk(int s, int t) {
  rep(i, n) {
    pat[i] = INF;
    vis[i] = false;
  }
  pat[s] = 0;
  priority_queue<pair<LL, int>> que;
  que.push(MP(0, s));

  while (!que.empty()) {
    int cur = que.top().second;
    que.pop();
    if (vis[cur])
      continue;
    vis[cur] = true;
    rep(i, G[cur].size()) {
      int ce = G[cur][i];
      if (ed[ce].cap == 0)
        continue;
      if (pat[ed[ce].to] > pat[cur] + ed[ce].cost) {
        pat[ed[ce].to] = pat[cur] + ed[ce].cost;
        prev[ed[ce].to] = ce;
        que.push(MP(-pat[ed[ce].to], ed[ce].to));
      }
    }
  }

  return pat[t] != INF;
}

LL mcf(int s, int t, int mf) {
  rep(i, n) h[i] = 0;
  LL ans = 0;
  while (dijk(s, t)) {
    // in this occasion flow must be 1
    int cur = t, cf = 1;
    while (cur != s) {
      ed[prev[cur]].cap -= cf;
      ed[prev[cur] ^ 1].cap += cf;
      cur = ed[prev[cur] ^ 1].to;
    }
    rep(i, m) ed[i].cost += pat[ed[i ^ 1].to] - pat[ed[i].to];
    rep(i, m) h[i] += pat[i];
    ans += h[t] * cf;
    mf--;
    if (mf == 0)
      return ans;
  }
  return -1;
}
} // namespace flow

int n, x[85], y[85];
LL v[85];
int m, a[325], b[325];
char t[325];
LL ans;

int minx[85], maxx[85], miny[85], maxy[85];
LL solve(int tot) {
  rep(i, n) {
    minx[i] = miny[i] = -INF;
    maxx[i] = maxy[i] = INF;
  }

  rep(i, m) if (b[i] < tot) {
    if (t[i] == 'L')
      minx[b[i]] = max(minx[b[i]], a[i] + 1);
    else if (t[i] == 'R')
      maxx[tot - b[i] - 1] = min(maxx[tot - b[i] - 1], a[i] - 1);
    else if (t[i] == 'D')
      miny[b[i]] = max(miny[b[i]], a[i] + 1);
    else if (t[i] == 'U')
      maxy[tot - b[i] - 1] = min(maxy[tot - b[i] - 1], a[i] - 1);
  }
  rep(i, tot - 1) {
    minx[i + 1] = max(minx[i], minx[i + 1]);
    miny[i + 1] = max(miny[i], miny[i + 1]);
  }
  for (int i = tot - 1; i > 0; i--) {
    maxx[i - 1] = min(maxx[i - 1], maxx[i]);
    maxy[i - 1] = min(maxy[i - 1], maxy[i]);
  }

  flow::n = 2 + tot * 2 + n * 2;
  flow::clear();
  rep(i, tot) flow::add_edge(tot * 2 + n * 2, i, 1, 0);
  rep(i, tot) rep(j, n) if (x[j] >= minx[i] && x[j] <= maxx[i])
      flow::add_edge(i, j + tot * 2, 1, 0);
  rep(i, n) flow::add_edge(i + tot * 2, i + tot * 2 + n, 1, INF - v[i]);
  rep(i, tot) rep(j, n) if (y[j] >= miny[i] && y[j] <= maxy[i])
      flow::add_edge(j + tot * 2 + n, i + tot, 1, 0);
  rep(i, tot) flow::add_edge(i + tot, tot * 2 + n * 2 + 1, 1, 0);

  LL ret = flow::mcf(flow::n - 2, flow::n - 1, tot);

  return ret == -1 ? -1 : INF * tot - ret;
}

int main() {
  cin >> n;
  rep(i, n) cin >> x[i] >> y[i] >> v[i];
  cin >> m;
  rep(i, m) cin >> t[i] >> a[i] >> b[i];
  for (int t = 0; t <= n; t++)
    ans = max(ans, solve(t));
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define MP make_pair

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const LL INF = 0x3f3f3f3f3f3f3f;

namespace flow {
const int MAXN = 400;
struct edge {
  int to, cap;
  LL cost;
};
int n, m;
edge ed[30005];
vector<int> G[405];

void clear() {
  m = 0;
  rep(i, n) G[i].clear();
}

void add_edge(int u, int v, int fl, LL co) {
  ed[m].to = v;
  ed[m].cap = fl;
  ed[m].cost = co;
  G[u].push_back(m);
  m++;
  ed[m].to = u;
  ed[m].cap = 0;
  ed[m].cost = -co;
  G[v].push_back(m);
  m++;
}

LL h[405];
LL pat[405];
int prev[405];
bool vis[405];

bool dijk(int s, int t) {
  rep(i, n) {
    pat[i] = INF;
    vis[i] = false;
  }
  pat[s] = 0;
  priority_queue<pair<LL, int>> que;
  que.push(MP(0, s));

  while (!que.empty()) {
    int cur = que.top().second;
    que.pop();
    if (vis[cur])
      continue;
    vis[cur] = true;
    rep(i, G[cur].size()) {
      int ce = G[cur][i];
      if (ed[ce].cap == 0)
        continue;
      if (pat[ed[ce].to] > pat[cur] + ed[ce].cost) {
        pat[ed[ce].to] = pat[cur] + ed[ce].cost;
        prev[ed[ce].to] = ce;
        que.push(MP(-pat[ed[ce].to], ed[ce].to));
      }
    }
  }

  return pat[t] != INF;
}

LL mcf(int s, int t, int mf) {
  rep(i, n) h[i] = 0;
  LL ans = 0;
  while (dijk(s, t)) {
    // in this occasion flow must be 1
    int cur = t, cf = 1;
    while (cur != s) {
      ed[prev[cur]].cap -= cf;
      ed[prev[cur] ^ 1].cap += cf;
      cur = ed[prev[cur] ^ 1].to;
    }
    rep(i, m) ed[i].cost += pat[ed[i ^ 1].to] - pat[ed[i].to];
    rep(i, n) h[i] += pat[i];
    ans += h[t] * cf;
    mf--;
    if (mf == 0)
      return ans;
  }
  return -1;
}
} // namespace flow

int n, x[85], y[85];
LL v[85];
int m, a[325], b[325];
char t[325];
LL ans;

int minx[85], maxx[85], miny[85], maxy[85];
LL solve(int tot) {
  rep(i, n) {
    minx[i] = miny[i] = -INF;
    maxx[i] = maxy[i] = INF;
  }

  rep(i, m) if (b[i] < tot) {
    if (t[i] == 'L')
      minx[b[i]] = max(minx[b[i]], a[i] + 1);
    else if (t[i] == 'R')
      maxx[tot - b[i] - 1] = min(maxx[tot - b[i] - 1], a[i] - 1);
    else if (t[i] == 'D')
      miny[b[i]] = max(miny[b[i]], a[i] + 1);
    else if (t[i] == 'U')
      maxy[tot - b[i] - 1] = min(maxy[tot - b[i] - 1], a[i] - 1);
  }
  rep(i, tot - 1) {
    minx[i + 1] = max(minx[i], minx[i + 1]);
    miny[i + 1] = max(miny[i], miny[i + 1]);
  }
  for (int i = tot - 1; i > 0; i--) {
    maxx[i - 1] = min(maxx[i - 1], maxx[i]);
    maxy[i - 1] = min(maxy[i - 1], maxy[i]);
  }

  flow::n = 2 + tot * 2 + n * 2;
  flow::clear();
  rep(i, tot) flow::add_edge(tot * 2 + n * 2, i, 1, 0);
  rep(i, tot) rep(j, n) if (x[j] >= minx[i] && x[j] <= maxx[i])
      flow::add_edge(i, j + tot * 2, 1, 0);
  rep(i, n) flow::add_edge(i + tot * 2, i + tot * 2 + n, 1, INF - v[i]);
  rep(i, tot) rep(j, n) if (y[j] >= miny[i] && y[j] <= maxy[i])
      flow::add_edge(j + tot * 2 + n, i + tot, 1, 0);
  rep(i, tot) flow::add_edge(i + tot, tot * 2 + n * 2 + 1, 1, 0);

  LL ret = flow::mcf(flow::n - 2, flow::n - 1, tot);

  return ret == -1 ? -1 : INF * tot - ret;
}

int main() {
  cin >> n;
  rep(i, n) cin >> x[i] >> y[i] >> v[i];
  cin >> m;
  rep(i, m) cin >> t[i] >> a[i] >> b[i];
  for (int t = 0; t <= n; t++)
    ans = max(ans, solve(t));
  cout << ans << endl;
  return 0;
}