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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#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 long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

typedef int Weight;
struct Edge {
  int src, dst;
  Weight capacity, cost;
  int rev;
  Edge(int src, int dst, Weight capacity, Weight cost)
      : src(src), dst(dst), capacity(capacity), cost(cost) {}
  Edge(int src, int dst, Weight capacity, Weight cost, int rev)
      : src(src), dst(dst), capacity(capacity), cost(cost), rev(rev) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.cost != f.cost ? e.cost > f.cost : // !!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;

void add_edge(Graph &g, int s, int d, Weight cap, Weight cost) {
  g[s].push_back(Edge(s, d, cap, cost, g[d].size()));
  g[d].push_back(Edge(d, s, 0, -cost, g[s].size() - 1));
}

pair<Weight, Weight> minimumCostFlow4(Graph g, int s, int t) {
  const int n = g.size();
  pair<Weight, Weight> total; // (cost, flow)

  vector<Weight> h(n, INF);
  h[s] = 0;
  REP(k, n) {
    bool update = 0;
    REP(i, n) {
      FOR(e, g[i]) if (e->capacity) if (h[e->dst] > h[e->src] + e->cost) {
        h[e->dst] = h[e->src] + e->cost;
        update = 1;
      }
    }
    if (!update)
      break;
  }

  for (int F = INF; F > 0;) { // residual flow
    vector<Weight> d(n, INF);
    d[s] = 0;
    vector<int> pv(n, -1), pe(n);
    priority_queue<Edge> Q;
    for (Q.push(Edge(-2, s, 0, 0)); !Q.empty();) {
      Edge e = Q.top();
      Q.pop();
      if (pv[e.dst] != -1)
        continue;
      pv[e.dst] = e.src;
      pe[e.dst] = e.rev;
      REP(i, g[e.dst].size()) {
        Edge &f = g[e.dst][i];
        if (f.capacity > 0 &&
            d[f.dst] > d[f.src] + f.cost + h[f.src] - h[f.dst]) {
          d[f.dst] = d[f.src] + f.cost + h[f.src] - h[f.dst];
          Q.push(Edge(f.src, f.dst, 0, d[f.dst], i));
        }
      }
    }
    if (pv[t] == -1)
      break;
    Weight f = F;
    for (int u = t; u != s; u = pv[u])
      f = min(f, g[pv[u]][pe[u]].capacity);
    if (!f)
      break;
    for (int u = t; u != s; u = pv[u]) {
      total.first += f * g[pv[u]][pe[u]].cost;
      g[pv[u]][pe[u]].capacity -= f;
      g[u][g[pv[u]][pe[u]].rev].capacity += f;
    }
    F -= f;
    total.second += f;
    REP(u, n) if (h[u] != INF) h[u] += d[u];
  }
  return total;
}

int m[100];
int a[100][20];
int b[100][20];
int c[100][20];
map<int, int> mp[100];

int num[101];
int cnt;
int n;

int SOURCE2() { return num[n - 1] + cnt * 2 + 2; }
int SOURCE() { return num[n - 1] + cnt * 2; }
int SINK() { return num[n - 1] + cnt * 2 + 1; }
int NODE(int i, int j) { return num[i] + j; }
int IN(int k) { return num[n - 1] + k * 2; }
int OUT(int k) { return num[n - 1] + k * 2 + 1; }

int main() {
  while (cin >> n, n) {
    cnt = 0;
    num[0] = 0;
    REP(i, n - 1) mp[i].clear();
    REP(i, n - 1) {
      cin >> m[i];
      num[i + 1] = num[i] + m[i];
      REP(j, m[i]) {
        cin >> a[i][j] >> b[i][j] >> c[i][j];
        if (mp[i].count(b[i][j]) == 0)
          mp[i][b[i][j]] = cnt++;
      }
    }
    int group;
    cin >> group;
    Graph g(num[n - 1] + 2 * cnt + 3);

    REP(i, n - 1) {
      REP(j, m[i]) { add_edge(g, NODE(i, j), IN(mp[i][b[i][j]]), 1, c[i][j]); }
      FOR(it, mp[i]) { add_edge(g, IN(it->second), OUT(it->second), 1, 0); }
      if (i) {
        FOR(it, mp[i - 1]) {
          REP(j, m[i]) {
            if (it->first <= a[i][j]) {
              add_edge(g, OUT(it->second), NODE(i, j), 1, 0);
            }
          }
        }
      }
    }
    REP(i, m[0]) { add_edge(g, SOURCE2(), NODE(0, i), 1, 0); }
    add_edge(g, SOURCE(), SOURCE2(), group, 0);
    FOR(it, mp[n - 2]) { add_edge(g, OUT(it->second), SINK(), 1, 0); }
    pii ans = minimumCostFlow4(g, SOURCE(), SINK());
    cout << ans.second << " " << ans.first << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#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 long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

typedef int Weight;
struct Edge {
  int src, dst;
  Weight capacity, cost;
  int rev;
  Edge(int src, int dst, Weight capacity, Weight cost)
      : src(src), dst(dst), capacity(capacity), cost(cost) {}
  Edge(int src, int dst, Weight capacity, Weight cost, int rev)
      : src(src), dst(dst), capacity(capacity), cost(cost), rev(rev) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.cost != f.cost ? e.cost > f.cost : // !!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;

void add_edge(Graph &g, int s, int d, Weight cap, Weight cost) {
  // cout << s << " " << d << " " << cap << " " << cost << endl;
  g[s].push_back(Edge(s, d, cap, cost));
  g[d].push_back(Edge(d, s, 0, -cost));
}

#define RESIDUE(u, v) (capacity[u][v] - flow[u][v])
#define RCOST(u, v) (cost[u][v] + h[u] - h[v])

// Dijkstra
pair<Weight, Weight> minimumCostFlow(const Graph &g, int s, int t) {
  const int n = g.size();
  Matrix capacity(n, Array(n)), cost(n, Array(n)), flow(n, Array(n));
  REP(u, n) FOR(e, g[u]) {
    capacity[e->src][e->dst] += e->capacity;
    cost[e->src][e->dst] += e->cost;
  }
  pair<Weight, Weight> total; // (cost, flow)
  vector<Weight> h(n, INF);
  h[s] = 0;
  // テ」ツδ凖」ツδォテ」ツδ榲」ツδウテ」ツδ陛」ツつゥテ」ツδシテ」ツδ嘉」ツ?ァテ」ツδ敕」ツδ?」ツδウテ」ツつキテ」ツδ」テ」ツδォテ」ツつ津ヲツアツづ」ツつ?」ツ?ヲティツイツ?ィツセツコテ」ツ?ォテ・ツッツセテ・ツソツ?
  REP(k, n)
  REP(i, n) FOR(e, g[i]) if (capacity[e->src][e->dst]) h[e->dst] =
      min(h[e->dst], h[e->src] + cost[e->src][e->dst]);

  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;
    for (Q.push(Edge(-2, s, 0, 0)); !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)
    if (h[u] != INF) h[u] += d[u]; // ifテ」ツ??」ツつ嘉」ツ?ェテ」ツ??ッツシツ?
  }
  return total;
}

int m[100];
int a[100][20];
int b[100][20];
int c[100][20];
map<int, int> mp[100];

int num[101];
int cnt;
int n;

int SOURCE2() { return num[n - 1] + cnt * 2 + 2; }
int SOURCE() { return num[n - 1] + cnt * 2; }
int SINK() { return num[n - 1] + cnt * 2 + 1; }
int NODE(int i, int j) { return num[i] + j; }
int IN(int k) { return num[n - 1] + k * 2; }
int OUT(int k) { return num[n - 1] + k * 2 + 1; }

int main() {
  while (cin >> n, n) {
    cnt = 0;
    num[0] = 0;
    REP(i, n - 1) mp[i].clear();
    REP(i, n - 1) {
      cin >> m[i];
      num[i + 1] = num[i] + m[i];
      REP(j, m[i]) {
        cin >> a[i][j] >> b[i][j] >> c[i][j];
        if (mp[i].count(b[i][j]) == 0)
          mp[i][b[i][j]] = cnt++;
      }
    }
    int group;
    cin >> group;
    Graph g(num[n - 1] + 2 * cnt + 3);

    REP(i, n - 1) {
      REP(j, m[i]) { add_edge(g, NODE(i, j), IN(mp[i][b[i][j]]), 1, c[i][j]); }
      FOR(it, mp[i]) { add_edge(g, IN(it->second), OUT(it->second), 1, 0); }
      if (i) {
        FOR(it, mp[i - 1]) {
          REP(j, m[i]) {
            if (it->first <= a[i][j]) {
              add_edge(g, OUT(it->second), NODE(i, j), 1, 0);
            }
          }
        }
      }
    }
    REP(i, m[0]) { add_edge(g, SOURCE2(), NODE(0, i), 1, 0); }
    add_edge(g, SOURCE(), SOURCE2(), group, 0);
    FOR(it, mp[n - 2]) { add_edge(g, OUT(it->second), SINK(), 1, 0); }
    pii ans = minimumCostFlow(g, SOURCE(), SINK());
    cout << ans.second << " " << ans.first << endl;
  }
}