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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;
typedef complex<double> P;

// container util

//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define MT(a, b, c) MP(a, MP(b, c))
#define T1 first
#define T2 second.first
#define T3 second.second

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

// AOJ 2293

struct edge {
  int to, cost, cap, rev;
  edge(int _to, int _cost, int _cap, int _ret)
      : to(_to), cost(_cost), cap(_cap), rev(_ret){};
};

int n, a[1111], b[1111];
int inf = 1111111111;
vector<vector<edge>> g;
set<int> si;
int nid[1111111];
int dist[3333], nv, pe[3333], pv[3333];

void addedge(int from, int to, int cost, int cap) {
  g[from].PB(edge(to, cost, cap, g[to].size()));
  g[to].PB(edge(from, -cost, 0, g[from].size() - 1));
}

inline void shortest_path(int s) {
  int flag = true;
  REP(i, nv) dist[i] = inf;
  dist[s] = 0;
  while (flag) {
    flag = false;
    REP(i, nv) {
      REP(j, g[i].size()) {
        if (g[i][j].cap > 0 && dist[g[i][j].to] > dist[i] + g[i][j].cost) {
          dist[g[i][j].to] = dist[i] + g[i][j].cost;
          pe[g[i][j].to] = j;
          pv[g[i][j].to] = i;
          flag = true;
        }
      }
    }
  }
}

int MinCostFlow(int s, int t, int f) {
  int ret = 0;
  while (f) {
    shortest_path(s);
    if (dist[t] == inf)
      return inf;
    int d = f, c = t;
    while (c != s) {
      d = min(d, g[pv[c]][pe[c]].cap);
      c = pv[c];
    }
    f -= d;
    ret += d * dist[t];
    c = t;
    while (c != s) {
      g[pv[c]][pe[c]].cap -= d;
      g[c][g[pv[c]][pe[c]].rev].cap += d;
      c = pv[c];
    }
  }
  return ret;
}

int main() {
  cin >> n;
  REP(i, n) {
    scanf("%d%d", &a[i], &b[i]);
    si.insert(a[i]);
    si.insert(b[i]);
  }
  nv = si.size() + n + 3;
  int k = n + 1;
  memset(nid, -1, sizeof(nid));
  g = vector<vector<edge>>(nv);
  REP(i, n) {
    int va, vb;
    addedge(0, i + 1, 0, 1);
    if (nid[a[i]] == -1)
      nid[a[i]] = k++;
    if (nid[b[i]] == -1)
      nid[b[i]] = k++;
    addedge(i + 1, nid[a[i]], -b[i], 1);
    addedge(i + 1, nid[b[i]], -a[i], 1);
    addedge(i + 1, nv - 2, 0, 1);
  }
  FOR(i, n + 1, nv - 2) addedge(i, nv - 1, 0, 1);
  addedge(nv - 2, nv - 1, 0, inf);
  cout << -MinCostFlow(0, nv - 1, n) << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;
typedef complex<double> P;

// container util

//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define MT(a, b, c) MP(a, MP(b, c))
#define T1 first
#define T2 second.first
#define T3 second.second

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

// AOJ 2293

struct edge {
  int to, cost, cap, rev;
  edge(int _to, int _cost, int _cap, int _ret)
      : to(_to), cost(_cost), cap(_cap), rev(_ret){};
};

int n, a[1111], b[1111];
int inf = 1111111111;
vector<vector<edge>> g;
set<int> si;
int nid[111111];
int dist[3333], nv, pe[3333], pv[3333];

void addedge(int from, int to, int cost, int cap) {
  g[from].PB(edge(to, cost, cap, g[to].size()));
  g[to].PB(edge(from, -cost, 0, g[from].size() - 1));
}

inline void shortest_path(int s) {
  int flag = true;
  REP(i, nv) dist[i] = inf;
  dist[s] = 0;
  while (flag) {
    flag = false;
    REP(i, nv) {
      REP(j, g[i].size()) {
        if (g[i][j].cap > 0 && dist[g[i][j].to] > dist[i] + g[i][j].cost) {
          dist[g[i][j].to] = dist[i] + g[i][j].cost;
          pe[g[i][j].to] = j;
          pv[g[i][j].to] = i;
          flag = true;
        }
      }
    }
  }
}

int MinCostFlow(int s, int t, int f) {
  int ret = 0;
  while (f) {
    shortest_path(s);
    if (dist[t] == inf)
      return inf;
    int d = f, c = t;
    while (c != s) {
      d = min(d, g[pv[c]][pe[c]].cap);
      c = pv[c];
    }
    f -= d;
    ret += d * dist[t];
    c = t;
    while (c != s) {
      g[pv[c]][pe[c]].cap -= d;
      g[c][g[pv[c]][pe[c]].rev].cap += d;
      c = pv[c];
    }
  }
  return ret;
}

int main() {
  cin >> n;
  REP(i, n) {
    scanf("%d%d", &a[i], &b[i]);
    si.insert(a[i]);
    si.insert(b[i]);
  }
  nv = si.size() + n + 3;
  int k = n + 1;
  memset(nid, -1, sizeof(nid));
  g = vector<vector<edge>>(nv);
  REP(i, n) {
    int va, vb;
    addedge(0, i + 1, 0, 1);
    if (nid[a[i]] == -1)
      nid[a[i]] = k++;
    if (nid[b[i]] == -1)
      nid[b[i]] = k++;
    addedge(i + 1, nid[a[i]], -b[i], 1);
    addedge(i + 1, nid[b[i]], -a[i], 1);
    addedge(i + 1, nv - 2, 0, 1);
  }
  FOR(i, n + 1, nv - 2) addedge(i, nv - 1, 0, 1);
  addedge(nv - 2, nv - 1, 0, inf);
  cout << -MinCostFlow(0, nv - 1, n) << endl;
}