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

#include <bits/stdc++.h>

using namespace std;

struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} init;

typedef long long LL;
struct edge {
  int flow, to, rev;
  LL cost;
};
typedef vector<edge> E;
typedef vector<E> FlowGraph;

void addedge(FlowGraph &g, int from, int to, int f, LL cost) {
  int n = g[from].size();
  int m = g[to].size();
  g[from].push_back(edge{f, to, m, cost});
  g[to].push_back(edge{0, from, n, -cost});
}

namespace _ssc {
#define SZ 4000
LL h[SZ];
LL d[SZ];
int pV[SZ];
int pE[SZ];
}; // namespace _ssc
struct ssc {
  LL *h, *d;
  int *pV, *pE;
  ssc(int V) : h(_ssc::h), d(_ssc::d), pV(_ssc::pV), pE(_ssc::pE) {
    fill(h, h + V, 0);
  }
};
const LL INF = 1e12;
LL min_cost_flow(int s, int t, int f, FlowGraph &g, ssc &info) {
  using P = tuple<LL, int>;
  LL *h = info.h;
  LL *d = info.d;
  int *pV = info.pV;
  int *pE = info.pE;

  const int V = g.size();
  LL res = 0;
  while (f > 0) {
    // cout<<f<<endl;
    priority_queue<P> que;
    fill(d, d + V, INF);
    d[s] = 0;
    que.push(P(0, s));
    while (que.size()) {
      auto p = que.top();
      que.pop();
      LL cost;
      int v;
      tie(cost, v) = p;
      cost = -cost;
      if (d[v] < cost)
        continue;
      for (int i = 0, sz = g[v].size(); i < sz; i++) {
        auto &e = g[v][i];
        if (e.flow > 0 && d[e.to] > d[v] + e.cost + h[v] - h[e.to]) {
          d[e.to] = d[v] + e.cost + h[v] - h[e.to];
          pV[e.to] = v;
          pE[e.to] = i;
          que.push(P(-d[e.to], e.to));
        }
      }
    }
    if (d[t] == INF)
      return -1;

    for (int v = 0; v < V; v++)
      h[v] += d[v];
    int d = f;
    for (int v = t; v != s; v = pV[v])
      d = min(d, g[pV[v]][pE[v]].flow);
    f -= d;
    res += d * h[t];
    for (int v = t; v != s; v = pV[v]) {
      auto &e = g[pV[v]][pE[v]];
      e.flow -= d;
      g[v][e.rev].flow += d;
    }
  }
  return res;
}
typedef vector<int> V;
int main() {
  int N;
  cin >> N;
  map<int, int> m;
  LL sum = 0;
  V A(N), B(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
    if (B[i] < A[i])
      swap(A[i], B[i]);
    sum += B[i];
    m[A[i]] = 0;
    m[B[i]] = 0;
  }
  int cnt = N;
  for (auto &it : m)
    it.second = cnt++;
  int S = cnt++;
  int G = cnt++;
  FlowGraph fg(cnt);
  for (int i = 0; i < N; i++) {
    addedge(fg, S, i, 1, 0);
    addedge(fg, i, G, 1, B[i]);
    addedge(fg, i, m[A[i]], 1, 0);
    addedge(fg, i, m[B[i]], 1, B[i] - A[i]);
  }
  for (auto &it : m)
    addedge(fg, it.second, G, 1, 0);
  ssc info(cnt);
  LL dec = min_cost_flow(S, G, N, fg, info);
  cout << sum - dec << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} init;

typedef long long LL;
struct edge {
  int flow, to, rev;
  LL cost;
};
typedef vector<edge> E;
typedef vector<E> FlowGraph;

void addedge(FlowGraph &g, int from, int to, int f, LL cost) {
  int n = g[from].size();
  int m = g[to].size();
  g[from].push_back(edge{f, to, m, cost});
  g[to].push_back(edge{0, from, n, -cost});
}

namespace _ssc {
#define SZ 2000
LL h[SZ];
LL d[SZ];
int pV[SZ];
int pE[SZ];
}; // namespace _ssc
struct ssc {
  LL *h, *d;
  int *pV, *pE;
  ssc(int V) : h(_ssc::h), d(_ssc::d), pV(_ssc::pV), pE(_ssc::pE) {
    fill(h, h + V, 0);
  }
};
const LL INF = 1e12;
LL min_cost_flow(int s, int t, int f, FlowGraph &g, ssc &info) {
  using P = tuple<LL, int>;
  LL *h = info.h;
  LL *d = info.d;
  int *pV = info.pV;
  int *pE = info.pE;

  const int V = g.size();
  LL res = 0;
  while (f > 0) {
    priority_queue<P> que;
    fill(d, d + V, INF);
    d[s] = 0;
    que.push(P(0, s));
    while (que.size()) {
      auto p = que.top();
      que.pop();
      LL cost;
      int v;
      tie(cost, v) = p;
      cost = -cost;
      if (d[v] < cost)
        continue;
      for (int i = 0, sz = g[v].size(); i < sz; i++) {
        auto &e = g[v][i];
        if (e.flow > 0 && d[e.to] > d[v] + e.cost + h[v] - h[e.to]) {
          d[e.to] = d[v] + e.cost + h[v] - h[e.to];
          pV[e.to] = v;
          pE[e.to] = i;
          que.push(P(-d[e.to], e.to));
        }
      }
    }
    if (d[t] == INF)
      return -1;

    for (int v = 0; v < V; v++)
      h[v] += d[v];
    int d = f;
    for (int v = t; v != s; v = pV[v])
      d = min(d, g[pV[v]][pE[v]].flow);
    f -= d;
    res += d * h[t];
    for (int v = t; v != s; v = pV[v]) {
      auto &e = g[pV[v]][pE[v]];
      e.flow -= d;
      g[v][e.rev].flow += d;
    }
  }
  return res;
}
typedef vector<int> V;
int main() {
  int N;
  cin >> N;
  map<int, int> m;
  LL sum = 0;
  V A(N), B(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
    if (B[i] < A[i])
      swap(A[i], B[i]);
    sum += B[i];
    m[A[i]] = 0;
    m[B[i]] = 0;
  }
  int cnt = N;
  for (auto &it : m)
    it.second = cnt++;
  int S = cnt++;
  int G = cnt++;
  FlowGraph fg(cnt);
  for (int i = 0; i < N; i++) {
    addedge(fg, S, i, 1, 0);
    addedge(fg, i, G, 1, B[i]);
    addedge(fg, i, m[A[i]], 1, 0);
    addedge(fg, i, m[B[i]], 1, B[i] - A[i]);
  }
  for (auto &it : m)
    addedge(fg, it.second, G, 1, 0);
  ssc info(cnt);
  LL dec = min_cost_flow(S, G, N, fg, info);
  cout << sum - dec << endl;
  return 0;
}