Problem p01410 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

struct Edge {
  int src, dst, cap, cost;
  Edge() {}
  Edge(int s, int d, int ca, int co) : src(s), dst(d), cap(ca), cost(co) {}
};
typedef vector<vector<Edge>> Graph;

int MinCostFlow(const Graph &_g, int begin, int end, int flow) {
  int size = _g.size();
  Graph g(size);
  vvi rev(size);
  rep(i, size) for (auto e : _g[i]) {
    g[i].push_back(e);
    g[e.dst].push_back(Edge(e.dst, e.src, 0, -e.cost));
    rev[i].push_back(g[e.dst].size() - 1);
    rev[e.dst].push_back(g[i].size() - 1);
  }

  int res = 0;
  vi pots(size);
  while (flow) {
    vi dist(size, INF);
    vector<pii> prev(size, mp(-1, -1));
    priority_queue<pii, vector<pii>, greater<pii>> pq;
    pq.push(mp(0, begin));
    dist[begin] = 0;
    while (!pq.empty()) {
      int curd = pq.top().first, curv = pq.top().second;
      pq.pop();
      if (dist[curv] < curd)
        continue;
      rep(i, g[curv].size()) {
        Edge e = g[curv][i];
        if (e.cap == 0)
          continue;
        int newdist = dist[e.src] + e.cost + pots[e.src] - pots[e.dst];
        if (dist[e.dst] > newdist) {
          dist[e.dst] = newdist;
          prev[e.dst] = mp(e.src, i);
          pq.push(mp(newdist, e.dst));
        }
      }
    }

    if (prev[end].first == -1)
      return -1;
    rep(i, size) pots[i] += dist[i];

    int f = flow;
    for (int v = end; v != begin; v = prev[v].first) {
      Edge e = g[prev[v].first][prev[v].second];
      f = min(f, e.cap);
    }
    for (int v = end; v != begin; v = prev[v].first) {
      int i = prev[v].first, j = prev[v].second;
      g[i][j].cap -= f;
      g[g[i][j].dst][rev[i][j]].cap += f;
    }
    flow -= f;
    res += f * pots[end];
  }

  return res;
}

int main() {
  for (int n; cin >> n && n;) {
    vi as(n), bs(n);
    rep(i, n) cin >> as[i] >> bs[i];
    map<int, int> f;
    rep(i, n) f.insert(mp(as[i], f.size()));
    rep(i, n) f.insert(mp(bs[i], f.size()));
    int m = f.size();
    Graph g(n + m + 2);
    int tap = n + m, sink = tap + 1;
    rep(i, n) {
      g[tap].emplace_back(tap, i, 1, 0);
      g[i].emplace_back(i, n + f[as[i]], 1, -bs[i]);
      g[i].emplace_back(i, n + f[bs[i]], 1, -as[i]);
      g[i].emplace_back(i, sink, 1, 0);
    }
    rep(i, m) g[n + i].emplace_back(n + i, sink, 1, 0);
    cout << -MinCostFlow(g, tap, sink, n) << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

struct Edge {
  int src, dst, cap, cost;
  Edge() {}
  Edge(int s, int d, int ca, int co) : src(s), dst(d), cap(ca), cost(co) {}
};
typedef vector<vector<Edge>> Graph;

int MinCostFlow(const Graph &_g, int begin, int end, int flow) {
  int size = _g.size();
  Graph g(size);
  vvi rev(size);
  rep(i, size) for (auto e : _g[i]) {
    g[i].push_back(e);
    g[e.dst].push_back(Edge(e.dst, e.src, 0, -e.cost));
    rev[i].push_back(g[e.dst].size() - 1);
    rev[e.dst].push_back(g[i].size() - 1);
  }

  int res = 0;
  vi pots(size);
  while (flow) {
    vi dist(size, INF);
    vector<pii> prev(size, mp(-1, -1));
    priority_queue<pii, vector<pii>, greater<pii>> pq;
    pq.push(mp(0, begin));
    dist[begin] = 0;
    while (!pq.empty()) {
      int curd = pq.top().first, curv = pq.top().second;
      pq.pop();
      if (dist[curv] < curd)
        continue;
      rep(i, g[curv].size()) {
        Edge e = g[curv][i];
        if (e.cap == 0)
          continue;
        int newdist = dist[e.src] + e.cost + pots[e.src] - pots[e.dst];
        if (dist[e.dst] > newdist) {
          dist[e.dst] = newdist;
          prev[e.dst] = mp(e.src, i);
          pq.push(mp(newdist, e.dst));
        }
      }
    }

    if (prev[end].first == -1)
      return -1;
    rep(i, size) pots[i] += dist[i];

    int f = flow;
    for (int v = end; v != begin; v = prev[v].first) {
      Edge e = g[prev[v].first][prev[v].second];
      f = min(f, e.cap);
    }
    for (int v = end; v != begin; v = prev[v].first) {
      int i = prev[v].first, j = prev[v].second;
      g[i][j].cap -= f;
      g[g[i][j].dst][rev[i][j]].cap += f;
    }
    flow -= f;
    res += f * pots[end];
  }

  return res;
}

int main() {
  for (int n; cin >> n && n;) {
    vi as(n), bs(n);
    rep(i, n) cin >> as[i] >> bs[i];
    int m = max(*max_element(all(as)), *max_element(all(bs)));
    Graph g(n + m + 2);
    int tap = n + m, sink = tap + 1;
    rep(i, n) {
      g[tap].emplace_back(tap, i, 1, 0);
      g[i].emplace_back(i, n + as[i] - 1, 1, -bs[i]);
      g[i].emplace_back(i, n + bs[i] - 1, 1, -as[i]);
      g[i].emplace_back(i, sink, 1, 0);
    }
    rep(i, m) g[n + i].emplace_back(n + i, sink, 1, 0);
    cout << -MinCostFlow(g, tap, sink, n) << endl;
  }
}