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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
typedef int Weight;
struct Edge {
  int src, dst;
  Weight cap, cost;
  int rev;
  Edge(int src, int dst, Weight cap, Weight cost, int rev = -1)
      : src(src), dst(dst), cap(cap), cost(cost), rev(rev) {}
};
typedef vector<Edge> Node;
typedef vector<Node> Graph;

void add_edge(Graph &G, int src, int dst, Weight cap, Weight cost) {
  G[src].push_back(Edge(src, dst, cap, cost, G[dst].size()));
  G[dst].push_back(Edge(dst, src, 0, -cost, G[src].size() - 1));
}

Weight min_cost_flow(Graph &G, int s, int t, Weight f) {
  typedef pair<Weight, int> P;
  Weight res = 0;
  int V = G.size();
  vector<Weight> h(V, 0);
  vector<int> prevv(V);
  vector<int> preve(V);
  if (s == t)
    return 0;
  bool start = true;
  while (f > 0) {
    vector<Weight> dist(V, INF);
    if (start) {
      start = false;
      bool update = true;
      dist[s] = 0;
      while (update) {
        update = false;
        REP(v, V) {
          if (dist[v] == INF)
            continue;
          REP(i, G[v].size()) {
            Edge &e = G[v][i];
            if (e.cap > 0 && dist[e.dst] > dist[v] + e.cost) {
              dist[e.dst] = dist[v] + e.cost;
              prevv[e.dst] = v;
              preve[e.dst] = i;
              update = true;
            }
          }
        }
      }
    } else {
      priority_queue<P, vector<P>, greater<P>> que;
      dist[s] = 0;
      que.push(P(0, s));
      while (!que.empty()) {
        P p = que.top();
        que.pop();
        int v = p.second;
        if (dist[v] < p.first)
          continue;
        REP(i, G[v].size()) {
          Edge &e = G[v][i];
          if (e.cap > 0 && dist[e.dst] > dist[v] + e.cost + h[v] - h[e.dst]) {
            dist[e.dst] = dist[v] + e.cost + h[v] - h[e.dst];
            prevv[e.dst] = v;
            preve[e.dst] = i;
            que.push(P(dist[e.dst], e.dst));
          }
        }
      }
    }
    if (dist[t] == INF)
      return -1;
    REP(v, V) h[v] += dist[v];

    Weight d = f;
    for (int v = t; v != s; v = prevv[v]) {
      d = min(d, G[prevv[v]][preve[v]].cap);
    }
    f -= d;
    res += d * h[t];
    for (int v = t; v != s; v = prevv[v]) {
      Edge &e = G[prevv[v]][preve[v]];
      e.cap -= d;
      G[v][e.rev].cap += d;
    }
  }
  return res;
}

int main() {
  int N;
  while (cin >> N && N) {
    Graph G(366);
    REP(i, N) {
      int a, b, c;
      cin >> a >> b >> c;
      add_edge(G, a - 1, b, 1, -c);
    }
    REP(i, 365) { add_edge(G, i, i + 1, 2, 0); }
    cout << -min_cost_flow(G, 0, 365, 2) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int N;
struct Event {
  int s, e, c;
  Event() {}
  Event(int s, int e, int c) : s(s), e(e), c(c) {}
  bool operator<(const Event &E) const {
    if (s != E.s)
      return s < E.s;
    else
      return e < E.e;
  }
};
typedef pair<int, int> P;
typedef pair<int, P> S;
map<S, int> memo;
int dfs(int idx, int t1, int t2, vector<Event> &e) {
  if (idx == N)
    return 0;
  if (t1 > t2)
    swap(t1, t2);
  S state(idx, P(t1, t2));
  if (memo.count(state))
    return memo[state];
  int res = dfs(idx + 1, t1, t2, e);
  if (t2 < e[idx].s) {
    res = max(res, e[idx].c + dfs(idx + 1, t1, e[idx].e, e));
  } else if (t1 < e[idx].s) {
    res = max(res, e[idx].c + dfs(idx + 1, e[idx].e, t2, e));
  }
  memo[state] = res;
  return res;
}
int main() {
  while (cin >> N && N) {
    vector<Event> events(N);
    REP(i, N) { scanf("%d %d %d", &events[i].s, &events[i].e, &events[i].c); }
    sort(events.begin(), events.end());
    memo.clear();
    cout << dfs(0, 0, 0, events) << endl;
  }
  return 0;
}