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

#include <bits/stdc++.h>
using namespace std;
#define INF (2e9)
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Weight capacity;

  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
  Edge(int src, int dst, Weight capacity, Weight weight)
      : src(src), dst(dst), capacity(capacity), weight(weight) {}
  Edge(int src, int dst) : src(src), dst(dst) { weight = 1; }
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

#define RESIDUE(u, v) (capacity[u][v] - flow[u][v])
#define RCOST(u, v) (cost[u][v] + h[u] - h[v])
pair<Weight, Weight> minimumCostFlow(Graph &g, int s, int t) {
  unordered_map<int, int> capacity[3333];
  unordered_map<int, int> cost[3333];
  unordered_map<int, int> flow[3333];

  const int n = g.size();
  REP(u, n) FOR(e, g[u]) {
    capacity[e->src][e->dst] += e->capacity;
    cost[e->src][e->dst] += e->weight;
  }
  pair<Weight, Weight> total; // (cost, flow)
  vector<Weight> h(n, INF);
  h[s] = 0;
  for (int k = 0; k <= g.size(); k++) {
    int flag = 0;
    for (int i = 0; i < g.size(); i++) {
      for (auto &e : g[i]) {
        if (e.capacity == 0)
          continue;
        if (h[e.dst] > h[e.src] + e.weight) {
          h[e.dst] = h[e.src] + e.weight;
          flag = 1;
        }
      }
    }
    if (!flag)
      break;
  }
  for (Weight F = INF; F > 0;) { // residual flow
    vector<Weight> d(n, INF);
    d[s] = 0;
    vector<int> p(n, -1);
    priority_queue<Edge> Q; // "e < f" <=> "e.cost > f.cost"
    for (Q.push(Edge(-2, s)); !Q.empty();) {
      Edge e = Q.top();
      Q.pop();
      if (p[e.dst] != -1)
        continue;
      p[e.dst] = e.src;
      FOR(f, g[e.dst]) if (RESIDUE(f->src, f->dst) > 0) {
        if (d[f->dst] > d[f->src] + RCOST(f->src, f->dst)) {
          d[f->dst] = d[f->src] + RCOST(f->src, f->dst);
          Q.push(Edge(f->src, f->dst, 0, d[f->dst]));
        }
      }
    }
    if (p[t] == -1)
      break;

    Weight f = F;
    for (int u = t; u != s; u = p[u])
      f = min(f, RESIDUE(p[u], u));
    for (int u = t; u != s; u = p[u]) {
      total.first += f * cost[p[u]][u];
      flow[p[u]][u] += f;
      flow[u][p[u]] -= f;
    }
    F -= f;
    total.second += f;
    REP(u, n) h[u] += d[u];
  }
  return total;
}

int A[1000], B[1000];

void addEdge(Graph &g, int a, int b, int c, long long d) {
  // cout << a << "=>" << b << " " << c << " (cost:" << d << ")" << endl;
  g[a].push_back(Edge(a, b, c, d));
  g[b].push_back(Edge(b, a, 0, -d));
}
signed main() {
  int N;
  cin >> N;

  vector<int> u;
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
    u.push_back(A[i]);
    u.push_back(B[i]);
  }
  sort(u.begin(), u.end());
  u.erase(unique(u.begin(), u.end()), u.end());
  Graph g(u.size() + N + 3);
  int s = g.size() - 1;
  int t = g.size() - 2;
  for (int i = 0; i < u.size(); i++) {
    addEdge(g, s, i, 1, 0);
    addEdge(g, i, t, 1, 0);
  }

  for (int i = 0; i < N; i++) {
    int a = lower_bound(u.begin(), u.end(), A[i]) - u.begin();
    int b = lower_bound(u.begin(), u.end(), B[i]) - u.begin();
    addEdge(g, a, u.size() + i, 1, -B[i]);
    if (a != b)
      addEdge(g, b, u.size() + i, 1, -A[i]);
    addEdge(g, u.size() + i, t, 1, 0);
  }
  cout << -minimumCostFlow(g, s, t).first << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
#define INF (2e9)
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Weight capacity;

  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
  Edge(int src, int dst, Weight capacity, Weight weight)
      : src(src), dst(dst), capacity(capacity), weight(weight) {}
  Edge(int src, int dst) : src(src), dst(dst) { weight = 1; }
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

#define RESIDUE(u, v) (capacity[u][v] - flow[u][v])
#define RCOST(u, v) (cost[u][v] + h[u] - h[v])
pair<Weight, Weight> minimumCostFlow(Graph &g, int s, int t) {
  map<int, int> capacity[3333];
  map<int, int> cost[3333];
  map<int, int> flow[3333];

  const int n = g.size();
  REP(u, n) FOR(e, g[u]) {
    capacity[e->src][e->dst] += e->capacity;
    cost[e->src][e->dst] += e->weight;
  }
  pair<Weight, Weight> total; // (cost, flow)
  vector<Weight> h(n, INF);
  h[s] = 0;
  for (int k = 0; k <= g.size(); k++) {
    int flag = 0;
    for (int i = 0; i < g.size(); i++) {
      for (auto &e : g[i]) {
        if (e.capacity == 0)
          continue;
        if (h[e.dst] > h[e.src] + e.weight) {
          h[e.dst] = h[e.src] + e.weight;
          flag = 1;
        }
      }
    }
    if (!flag)
      break;
  }
  for (Weight F = INF; F > 0;) { // residual flow
    vector<Weight> d(n, INF);
    d[s] = 0;
    vector<int> p(n, -1);
    priority_queue<Edge> Q; // "e < f" <=> "e.cost > f.cost"
    for (Q.push(Edge(-2, s)); !Q.empty();) {
      Edge e = Q.top();
      Q.pop();
      if (p[e.dst] != -1)
        continue;
      p[e.dst] = e.src;
      FOR(f, g[e.dst]) if (RESIDUE(f->src, f->dst) > 0) {
        if (d[f->dst] > d[f->src] + RCOST(f->src, f->dst)) {
          d[f->dst] = d[f->src] + RCOST(f->src, f->dst);
          Q.push(Edge(f->src, f->dst, 0, d[f->dst]));
        }
      }
    }
    if (p[t] == -1)
      break;

    Weight f = F;
    for (int u = t; u != s; u = p[u])
      f = min(f, RESIDUE(p[u], u));
    for (int u = t; u != s; u = p[u]) {
      total.first += f * cost[p[u]][u];
      flow[p[u]][u] += f;
      flow[u][p[u]] -= f;
    }
    F -= f;
    total.second += f;
    REP(u, n) h[u] += d[u];
  }
  return total;
}

int A[1000], B[1000];

void addEdge(Graph &g, int a, int b, int c, long long d) {
  // cout << a << "=>" << b << " " << c << " (cost:" << d << ")" << endl;
  g[a].push_back(Edge(a, b, c, d));
  g[b].push_back(Edge(b, a, 0, -d));
}
signed main() {
  int N;
  cin >> N;

  vector<int> u;
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
    u.push_back(A[i]);
    u.push_back(B[i]);
  }
  sort(u.begin(), u.end());
  u.erase(unique(u.begin(), u.end()), u.end());
  Graph g(u.size() + N + 3);
  int s = g.size() - 1;
  int t = g.size() - 2;
  for (int i = 0; i < u.size(); i++) {
    addEdge(g, s, i, 1, 0);
    addEdge(g, i, t, 1, 0);
  }

  for (int i = 0; i < N; i++) {
    int a = lower_bound(u.begin(), u.end(), A[i]) - u.begin();
    int b = lower_bound(u.begin(), u.end(), B[i]) - u.begin();
    addEdge(g, a, u.size() + i, 1, -B[i]);
    if (a != b)
      addEdge(g, b, u.size() + i, 1, -A[i]);
    addEdge(g, u.size() + i, t, 1, 0);
  }
  cout << -minimumCostFlow(g, s, t).first << endl;
}