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

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, a) FOR(i, 0, a)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define sz size()
#define MP make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const static int INF = 1e8;

struct edge {
  int from, to, cost, cap, rev;
  edge(int a, int b, int c, int d, int e)
      : from(a), to(b), cost(c), cap(d), rev(e) {}
};

// Based on Dijkstra (using potential)
struct minFlow {
  int v;
  vector<vector<edge>> graph;
  vi d, use, pv, pe;

  minFlow(int n) : v(n) {
    graph.resize(v);
    pv.resize(v);
    pe.resize(v);
  }

  void add(int s, int g, int c, int p) {
    graph[s].pb(edge(s, g, c, p, graph[g].sz));
    graph[g].pb(edge(g, s, -c, 0, graph[s].sz - 1));
  }

  int cal(int s, int t, int f) {
    int res = 0;
    while (f > 0) {
      d = vi(v, INF);
      d[s] = 0;

      rep(azu, v) {
        bool update = false;
        rep(u, v) {
          rep(i, graph[u].sz) {
            edge &e = graph[u][i];
            if (e.cap > 0 && d[e.to] > d[u] + e.cost) {
              update = true;
              d[e.to] = d[u] + e.cost;
              pv[e.to] = u;
              pe[e.to] = i;
            }
          }
        }
        if (!update)
          break;
      }

      if (d[t] == INF)
        return -1;

      int x = f;
      for (int u = t; u != s; u = pv[u])
        x = min(x, graph[pv[u]][pe[u]].cap);
      f -= x;
      res += x * d[t];
      for (int u = t; u != s; u = pv[u]) {
        edge &e = graph[pv[u]][pe[u]];
        e.cap -= x;
        graph[u][e.rev].cap += x;
      }
    }
    return res;
  }
};

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  vector<int> len;

  rep(i, n) {
    cin >> a[i] >> b[i];
    len.pb(a[i]);
    len.pb(b[i]);
  }
  sort(all(len));
  len.erase(unique(all(len)), len.end());

  int m = len.size();
  int V = n + m + 3;
  minFlow mf(V);
  rep(i, n) {
    mf.add(0, i + 1, 0, 1);

    int id1 = lower_bound(all(len), a[i]) - len.begin();
    mf.add(i + 1, n + 1 + id1, -b[i], 1);
    int id2 = lower_bound(all(len), b[i]) - len.begin();
    mf.add(i + 1, n + 1 + id2, -a[i], 1);

    mf.add(i + 1, n + m + 1, 0, 1);
  }

  rep(i, m) { mf.add(n + 1 + i, n + m + 2, 0, 1); }

  mf.add(n + m + 1, n + m + 2, 0, n);

  cout << -mf.cal(0, V - 1, n) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, a) FOR(i, 0, a)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define sz size()
#define MP make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const static int INF = 1e8;

struct edge {
  int from, to, cost, cap, rev;
  edge(int a, int b, int c, int d, int e)
      : from(a), to(b), cost(c), cap(d), rev(e) {}
};

// Based on Dijkstra (using potential)
struct minFlow {
  int v;
  vector<vector<edge>> graph;
  vi d, use, pv, pe;

  minFlow(int n) : v(n) {
    graph.resize(v);
    pv.resize(v);
    pe.resize(v);
  }

  void add(int s, int g, int c, int p) {
    graph[s].pb(edge(s, g, c, p, graph[g].sz));
    graph[g].pb(edge(g, s, -c, 0, graph[s].sz - 1));
  }

  int cal(int s, int t, int f) {
    int res = 0;
    while (f > 0) {
      d = vi(v, INF);
      d[s] = 0;

      rep(azu, v) rep(u, v) {
        {
          rep(i, graph[u].sz) {
            edge &e = graph[u][i];
            if (e.cap > 0 && d[e.to] > d[u] + e.cost) {
              d[e.to] = d[u] + e.cost;
              pv[e.to] = u;
              pe[e.to] = i;
            }
          }
        }
      }

      if (d[t] == INF)
        return -1;

      int x = f;
      for (int u = t; u != s; u = pv[u])
        x = min(x, graph[pv[u]][pe[u]].cap);
      f -= x;
      res += x * d[t];
      for (int u = t; u != s; u = pv[u]) {
        edge &e = graph[pv[u]][pe[u]];
        e.cap -= x;
        graph[u][e.rev].cap += x;
      }
    }
    return res;
  }
};

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  vector<int> len;

  rep(i, n) {
    cin >> a[i] >> b[i];
    len.pb(a[i]);
    len.pb(b[i]);
  }
  sort(all(len));
  len.erase(unique(all(len)), len.end());

  int m = len.size();
  int V = n + m + 3;
  minFlow mf(V);
  rep(i, n) {
    mf.add(0, i + 1, 0, 1);

    int id1 = lower_bound(all(len), a[i]) - len.begin();
    mf.add(i + 1, n + 1 + id1, -b[i], 1);
    int id2 = lower_bound(all(len), b[i]) - len.begin();
    mf.add(i + 1, n + 1 + id2, -a[i], 1);

    mf.add(i + 1, n + m + 1, 0, 1);
  }

  rep(i, m) { mf.add(n + 1 + i, n + m + 2, 0, 1); }

  mf.add(n + m + 1, n + m + 2, 0, n);

  cout << -mf.cal(0, V - 1, n) << endl;
}