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

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(kbrni, s) cout << " " << kbrni;                                         \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

struct Edge {
  Edge(const short to, const short reverse, const char capacity, const int cost,
       bool is_reverse = false)
      : to{to}, reverse{reverse}, capacity{capacity}, flow{0}, cost{cost},
        is_reverse{is_reverse} {}
  short to;
  short reverse;
  char capacity;
  char flow;
  int cost;
  bool is_reverse;
};

class tsort {
public:
  vector<int> deg;
  vector<short> order;
  short V;
  tsort(vector<vector<Edge>> &g) {
    V = len(g);
    deg.resize(V, 0);
    rep(i, len(g)) {
      for (const Edge &e : g[i]) {
        if (!e.is_reverse) {
          deg[e.to]++;
        }
      }
    }
    make(g);
  }
  void make(const vector<vector<Edge>> &G) {
    queue<short> que;
    rep(i, V) {
      if (deg[i] == 0) {
        que.push(i);
      }
    }
    while (!que.empty()) {
      short p = que.front();
      que.pop();
      order.push_back(p);
      rep(i, G[p].size()) {
        if (--deg[G[p][i].to] == 0) {
          que.push(G[p][i].to);
        }
      }
    }
    if (*max_element(deg.begin(), deg.end()) != 0) {
      order.clear();
    }
  }
};

class CostFlow {
public:
  using T = int;

  CostFlow(const short v) : V{v} {
    edge.resize(v);
    m_dist.resize(v);
    m_potential.resize(v);
    m_prev_v.resize(v);
    m_prev_e.resize(v);
  }

  void addEdge(const short from, const short to, const char capacity,
               const T cost) {
    edge[from].pb(Edge{to, (short)edge[to].size(), capacity, cost, false});
    edge[to].pb(Edge{from, (short)(edge[from].size() - 1), 0, -cost, true});
  }

  T minCostFlow(const short s, const short t, char f) {
    //    const T INF = numeric_limits<T>::max()/100;
    using P = pair<T, short>;
    T res = 0;
    fill(all(m_potential), 0);
    tsort ts(edge);
    // potential
    for (short i = 0; i < V; i++) {
      m_potential[i] = INF;
    }
    m_potential[s] = 0;
    //        assert(s == order[0]);
    //    svec(order);

    for (const short i : ts.order) {
      if (m_potential[i] != INF) {
        for (const auto &e : edge[i]) {
          if (e.is_reverse) {
            continue;
          }
          m_potential[e.to] = min(m_potential[e.to], m_potential[i] + e.cost);
        }
      }
    }

    //        svec(m_potential);

    while (f > 0) {
      priority_queue<P, vector<P>, greater<P>> q;
      fill(all(m_dist), INF);
      m_dist[s] = 0;
      q.push(make_pair(0, s));
      while (not q.empty()) {
        const P p = q.top();
        q.pop();
        const short v = p.second;
        if (m_dist[v] < p.first) {
          continue;
        }
        for (short i = 0; i < edge[v].size(); i++) {
          const auto &e = edge[v][i];
          if (e.capacity > e.flow and m_dist[e.to] > m_dist[v] + e.cost +
                                                         m_potential[v] -
                                                         m_potential[e.to]) {
            m_dist[e.to] =
                m_dist[v] + e.cost + m_potential[v] - m_potential[e.to];
            m_prev_v[e.to] = v;
            m_prev_e[e.to] = i;
            q.push(make_pair(m_dist[e.to], e.to));
          }
        }
      }

      if (m_dist[t] == INF) {
        return -1;
      }
      for (short v = 0; v < V; v++) {
        m_potential[v] += m_dist[v];
      }
      char d = f;
      for (short v = t; v != s; v = m_prev_v[v]) {
        const auto &e = edge[m_prev_v[v]][m_prev_e[v]];
        d = min(d, (char)(e.capacity - e.flow));
      }
      f -= d;
      res += d * m_potential[t];
      for (short v = t; v != s; v = m_prev_v[v]) {
        auto &e = edge[m_prev_v[v]][m_prev_e[v]];
        e.flow += d;
        edge[v][e.reverse].flow -= d;
      }
    }
    return res;
  }

  const short V;
  vector<vector<Edge>> edge;

private:
  vector<T> m_dist;
  vector<T> m_potential;
  vector<short> m_prev_v;
  vector<short> m_prev_e;
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, v, xr, xl;
  cin >> n >> v >> xr >> xl;
  vvl vec(n, vl(3));
  rep(i, n) {
    rep(j, 3) { cin >> vec[i][j]; }
  }
  CostFlow mc(2 * n + 4);
  rep(i, n) { mc.addEdge(i, n + i, 1, 0); }
  rep(i, 2) { mc.addEdge(2 * n, 2 * n + 1 + i, 1, 0); }
  rep(i, n) {
    if (abs(xl - vec[i][0]) <= v * vec[i][1]) {
      mc.addEdge(2 * n + 1, i, 1, 0);
    }
  }
  rep(i, n) {
    if (abs(xr - vec[i][0]) <= v * vec[i][1]) {
      mc.addEdge(2 * n + 2, i, 1, 0);
    }
  }
  rep(i, n) {
    rep(j, n) {
      if (i != j) {
        if (abs(vec[i][0] - vec[j][0]) <= v * (vec[j][1] - vec[i][1])) {
          mc.addEdge(n + i, j, 1, -vec[i][2]);
        }
      }
    }
  }
  rep(i, n) { mc.addEdge(n + i, 2 * n + 3, 1, -vec[i][2]); }
  rep(i, 2) { mc.addEdge(2 * n + 1 + i, 2 * n + 3, 1, 0); }
  cout << -mc.minCostFlow(2 * n, 2 * n + 3, 2) << "\\n";
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(kbrni, s) cout << " " << kbrni;                                         \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

struct Edge {
  Edge(const short to, const short reverse, const int capacity, const int cost,
       bool is_reverse = false)
      : to{to}, reverse{reverse}, capacity{capacity}, flow{0}, cost{cost},
        is_reverse{is_reverse} {}
  short to;
  short reverse;
  int capacity;
  int flow;
  int cost;
  bool is_reverse;
};

class tsort {
public:
  vector<int> deg;
  vector<short> order;
  short V;
  tsort(vector<vector<Edge>> &g) {
    V = len(g);
    deg.resize(V, 0);
    rep(i, len(g)) {
      for (const Edge &e : g[i]) {
        if (!e.is_reverse) {
          deg[e.to]++;
        }
      }
    }
    make(g);
  }
  void make(const vector<vector<Edge>> &G) {
    queue<short> que;
    rep(i, V) {
      if (deg[i] == 0) {
        que.push(i);
      }
    }
    while (!que.empty()) {
      short p = que.front();
      que.pop();
      order.push_back(p);
      rep(i, G[p].size()) {
        if (--deg[G[p][i].to] == 0) {
          que.push(G[p][i].to);
        }
      }
    }
    if (*max_element(deg.begin(), deg.end()) != 0) {
      order.clear();
    }
  }
};

class CostFlow {
public:
  using T = int;

  CostFlow(const short v) : V{v} {
    edge.resize(v);
    m_dist.resize(v);
    m_potential.resize(v);
    m_prev_v.resize(v);
    m_prev_e.resize(v);
  }

  void addEdge(const short from, const short to, const int capacity,
               const T cost) {
    edge[from].pb(Edge{to, (short)edge[to].size(), capacity, cost, false});
    edge[to].pb(Edge{from, (short)(edge[from].size() - 1), 0, -cost, true});
  }

  T minCostFlow(const short s, const short t, int f) {
    //    const T INF = numeric_limits<T>::max()/100;
    using P = pair<T, short>;
    T res = 0;
    fill(all(m_potential), 0);
    tsort ts(edge);
    // potential
    for (short i = 0; i < V; i++) {
      m_potential[i] = INF;
    }
    m_potential[s] = 0;
    //        assert(s == order[0]);
    //    svec(order);

    for (const short i : ts.order) {
      if (m_potential[i] != INF) {
        for (const auto &e : edge[i]) {
          if (e.is_reverse) {
            continue;
          }
          m_potential[e.to] = min(m_potential[e.to], m_potential[i] + e.cost);
        }
      }
    }

    //        svec(m_potential);

    while (f > 0) {
      priority_queue<P, vector<P>, greater<P>> q;
      fill(all(m_dist), INF);
      m_dist[s] = 0;
      q.push(make_pair(0, s));
      while (not q.empty()) {
        const P p = q.top();
        q.pop();
        const short v = p.second;
        if (m_dist[v] < p.first) {
          continue;
        }
        for (short i = 0; i < edge[v].size(); i++) {
          const auto &e = edge[v][i];
          if (e.capacity > e.flow and m_dist[e.to] > m_dist[v] + e.cost +
                                                         m_potential[v] -
                                                         m_potential[e.to]) {
            m_dist[e.to] =
                m_dist[v] + e.cost + m_potential[v] - m_potential[e.to];
            m_prev_v[e.to] = v;
            m_prev_e[e.to] = i;
            q.push(make_pair(m_dist[e.to], e.to));
          }
        }
      }

      if (m_dist[t] == INF) {
        return -1;
      }
      for (short v = 0; v < V; v++) {
        m_potential[v] += m_dist[v];
      }
      int d = f;
      for (short v = t; v != s; v = m_prev_v[v]) {
        const auto &e = edge[m_prev_v[v]][m_prev_e[v]];
        d = min(d, e.capacity - e.flow);
      }
      f -= d;
      res += d * m_potential[t];
      for (short v = t; v != s; v = m_prev_v[v]) {
        auto &e = edge[m_prev_v[v]][m_prev_e[v]];
        e.flow += d;
        edge[v][e.reverse].flow -= d;
      }
    }
    return res;
  }

  const short V;
  vector<vector<Edge>> edge;

private:
  vector<T> m_dist;
  vector<T> m_potential;
  vector<short> m_prev_v;
  vector<short> m_prev_e;
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, v, xr, xl;
  cin >> n >> v >> xr >> xl;
  vvl vec(n, vl(3));
  rep(i, n) {
    rep(j, 3) { cin >> vec[i][j]; }
  }
  CostFlow mc(2 * n + 4);
  rep(i, n) { mc.addEdge(i, n + i, 1, 0); }
  rep(i, 2) { mc.addEdge(2 * n, 2 * n + 1 + i, 1, 0); }
  rep(i, n) {
    if (abs(xl - vec[i][0]) <= v * vec[i][1]) {
      mc.addEdge(2 * n + 1, i, 1, 0);
    }
  }
  rep(i, n) {
    if (abs(xr - vec[i][0]) <= v * vec[i][1]) {
      mc.addEdge(2 * n + 2, i, 1, 0);
    }
  }
  rep(i, n) {
    rep(j, n) {
      if (i != j) {
        if (abs(vec[i][0] - vec[j][0]) <= v * (vec[j][1] - vec[i][1])) {
          mc.addEdge(n + i, j, 1, -vec[i][2]);
        }
      }
    }
  }
  rep(i, n) { mc.addEdge(n + i, 2 * n + 3, 1, -vec[i][2]); }
  rep(i, 2) { mc.addEdge(2 * n + 1 + i, 2 * n + 3, 1, 0); }
  cout << -mc.minCostFlow(2 * n, 2 * n + 3, 2) << "\\n";
  return 0;
}