Problem p01410 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
namespace MCF {
// required <string.h> <vector> <queue> <algorithm>
#define MAXN 3100
#define MAXM 21000
#define wint int
#define cint long long
const wint wEPS = 0;
const wint wINF = 1001001001;
const cint cEPS = 0;
const cint cINF = 1001001001001001LL;
int n, m, ptr[MAXN], next[MAXM], zu[MAXM];
wint capa[MAXM], tof;
cint cost[MAXM], toc, d[MAXN], pot[MAXN];
int vis[MAXN], pree[MAXN];
void init(int _n) {
  n = _n;
  m = 0;
  memset(ptr, ~0, n * 4);
}
void ae(int u, int v, wint w, cint c) {
  next[m] = ptr[u];
  ptr[u] = m;
  zu[m] = v;
  capa[m] = w;
  cost[m] = +c;
  ++m;
  next[m] = ptr[v];
  ptr[v] = m;
  zu[m] = u;
  capa[m] = 0;
  cost[m] = -c;
  ++m;
}
bool solve(int src, int ink, wint flo = wINF) {
  int i, u, v;
  wint f;
  cint c, cc;
  memset(pot, 0, n * sizeof(cint));
  //*
  for (bool cont = 1; cont;) {
    cont = 0;
    for (u = 0; u < n; ++u)
      for (i = ptr[u]; ~i; i = next[i])
        if (capa[i] > wEPS) {
          if (pot[zu[i]] > pot[u] + cost[i] + cEPS) {
            pot[zu[i]] = pot[u] + cost[i];
            cont = 1;
          }
        }
  }
  //*/
  for (toc = 0, tof = 0; tof + wEPS < flo;) {
    typedef pair<cint, int> node;
    priority_queue<node, vector<node>, greater<node>> q;
    for (u = 0; u < n; ++u) {
      d[u] = cINF;
      vis[u] = 0;
    }
    for (q.push(make_pair(d[src] = 0, src)); !q.empty();) {
      c = q.top().first;
      u = q.top().second;
      q.pop();
      if (vis[u]++)
        continue;
      for (i = ptr[u]; ~i; i = next[i])
        if (capa[i] > wEPS) {
          cc = c + cost[i] + pot[u] - pot[v = zu[i]];
          if (d[v] > cc) {
            q.push(make_pair(d[v] = cc, v));
            pree[v] = i;
          }
        }
    }
    if (!vis[ink])
      return 0;
    f = flo - tof;
    for (v = ink; v != src; v = zu[i ^ 1]) {
      i = pree[v];
      f = min(f, capa[i]);
    }
    for (v = ink; v != src; v = zu[i ^ 1]) {
      i = pree[v];
      capa[i] -= f;
      capa[i ^ 1] += f;
    }
    tof += f;
    toc += f * (d[ink] - pot[src] + pot[ink]);
    for (u = 0; u < n; ++u)
      pot[u] += d[u];
  }
  return 1;
}
} // namespace MCF
int z[2100];
int b[1100];
int c[1100];
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 0; i < a; i++) {
    scanf("%d%d", b + i, c + i);
    z[i * 2] = b[i];
    z[i * 2 + 1] = c[i];
  }
  MCF::init(a * 3 + 2);
  std::sort(z, z + a * 2);
  int s = a * 3;
  int t = a * 3 + 1;
  for (int i = 0; i < a; i++) {
    MCF::ae(s, i, 1, 0);
    MCF::ae(i, t, 1, 1000000);
    int L = lower_bound(z, z + a * 2, b[i]) - z;
    int R = lower_bound(z, z + a * 2, c[i]) - z;
    MCF::ae(i, a + L, 1, 1000000 - c[i]);
    MCF::ae(i, a + R, 1, 1000000 - b[i]);
  }
  for (int i = 0; i < a * 2; i++)
    MCF::ae(a + i, t, 1, 0);
  MCF::solve(s, t, a);
  printf("%lld\\n", -MCF::toc + 1000000 * a);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
namespace MCF {
// required <string.h> <vector> <queue> <algorithm>
#define MAXN 2100
#define MAXM 21000
#define wint int
#define cint long long
const wint wEPS = 0;
const wint wINF = 1001001001;
const cint cEPS = 0;
const cint cINF = 1001001001001001LL;
int n, m, ptr[MAXN], next[MAXM], zu[MAXM];
wint capa[MAXM], tof;
cint cost[MAXM], toc, d[MAXN], pot[MAXN];
int vis[MAXN], pree[MAXN];
void init(int _n) {
  n = _n;
  m = 0;
  memset(ptr, ~0, n * 4);
}
void ae(int u, int v, wint w, cint c) {
  next[m] = ptr[u];
  ptr[u] = m;
  zu[m] = v;
  capa[m] = w;
  cost[m] = +c;
  ++m;
  next[m] = ptr[v];
  ptr[v] = m;
  zu[m] = u;
  capa[m] = 0;
  cost[m] = -c;
  ++m;
}
bool solve(int src, int ink, wint flo = wINF) {
  int i, u, v;
  wint f;
  cint c, cc;
  memset(pot, 0, n * sizeof(cint));
  //*
  for (bool cont = 1; cont;) {
    cont = 0;
    for (u = 0; u < n; ++u)
      for (i = ptr[u]; ~i; i = next[i])
        if (capa[i] > wEPS) {
          if (pot[zu[i]] > pot[u] + cost[i] + cEPS) {
            pot[zu[i]] = pot[u] + cost[i];
            cont = 1;
          }
        }
  }
  //*/
  for (toc = 0, tof = 0; tof + wEPS < flo;) {
    typedef pair<cint, int> node;
    priority_queue<node, vector<node>, greater<node>> q;
    for (u = 0; u < n; ++u) {
      d[u] = cINF;
      vis[u] = 0;
    }
    for (q.push(make_pair(d[src] = 0, src)); !q.empty();) {
      c = q.top().first;
      u = q.top().second;
      q.pop();
      if (vis[u]++)
        continue;
      for (i = ptr[u]; ~i; i = next[i])
        if (capa[i] > wEPS) {
          cc = c + cost[i] + pot[u] - pot[v = zu[i]];
          if (d[v] > cc) {
            q.push(make_pair(d[v] = cc, v));
            pree[v] = i;
          }
        }
    }
    if (!vis[ink])
      return 0;
    f = flo - tof;
    for (v = ink; v != src; v = zu[i ^ 1]) {
      i = pree[v];
      f = min(f, capa[i]);
    }
    for (v = ink; v != src; v = zu[i ^ 1]) {
      i = pree[v];
      capa[i] -= f;
      capa[i ^ 1] += f;
    }
    tof += f;
    toc += f * (d[ink] - pot[src] + pot[ink]);
    for (u = 0; u < n; ++u)
      pot[u] += d[u];
  }
  return 1;
}
} // namespace MCF
int z[2100];
int b[1100];
int c[1100];
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 0; i < a; i++) {
    scanf("%d%d", b + i, c + i);
    z[i * 2] = b[i];
    z[i * 2 + 1] = c[i];
  }
  MCF::init(a * 3 + 2);
  std::sort(z, z + a * 2);
  int s = a * 3;
  int t = a * 3 + 1;
  for (int i = 0; i < a; i++) {
    MCF::ae(s, i, 1, 0);
    MCF::ae(i, t, 1, 1000000);
    int L = lower_bound(z, z + a * 2, b[i]) - z;
    int R = lower_bound(z, z + a * 2, c[i]) - z;
    MCF::ae(i, a + L, 1, 1000000 - c[i]);
    MCF::ae(i, a + R, 1, 1000000 - b[i]);
  }
  for (int i = 0; i < a * 2; i++)
    MCF::ae(a + i, t, 1, 0);
  MCF::solve(s, t, a);
  printf("%lld\\n", -MCF::toc + 1000000 * a);
}