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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define inf (1 << 29)
#define MAX 5000
using namespace std;
typedef pair<int, int> ii;

// flow --- start
struct edge {
  int to, cap, rev;
  edge(int to = inf, int cap = inf, int rev = inf)
      : to(to), cap(cap), rev(rev) {}
};

int V;
vector<edge> G[MAX];
int level[MAX];
int iter[MAX];

void add_edge(int from, int to, int cap) {
  G[from].push_back(edge(to, cap, G[to].size()));
  G[to].push_back(edge(from, 0, G[from].size() - 1));
}

void bfs(int s) {
  rep(i, MAX) level[i] = -1;
  queue<int> que;
  level[s] = 0;
  que.push(s);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    rep(i, G[v].size()) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[e.to] < 0) {
        level[e.to] = level[v] + 1;
        que.push(e.to);
      }
    }
  }
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  for (int &i = iter[v]; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (e.cap > 0 && level[v] < level[e.to]) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int max_flow(int s, int t) {
  int flow = 0;
  for (;;) {
    bfs(s);
    if (level[t] < 0)
      return flow;
    rep(i, MAX) iter[i] = 0;
    int f;
    while ((f = dfs(s, t, inf)) > 0)
      flow += f;
  }
}

// flow --- end

int N, M, L;
int mincost[MAX][MAX];
vector<ii> ps;

int main() {
  while (cin >> N >> M >> L, N | M | L) {
    rep(i, MAX) G[i].clear();
    rep(i, N) rep(j, N) mincost[j][i] = inf;
    rep(i, N) mincost[i][i] = 0;

    int u, v, d;
    rep(i, M) {
      cin >> u >> v >> d;
      mincost[u][v] = mincost[v][u] = d;
    }
    rep(i, N) rep(j, N) rep(k, N) mincost[j][k] =
        min(mincost[j][k], mincost[j][i] + mincost[i][k]);

    ps.clear();
    ps.resize(L);
    rep(i, L) cin >> ps[i].first >> ps[i].second;
    V = L;

    int st = 2 * V, ed = 2 * V + 1;
    rep(i, V) add_edge(st, i, 1);
    rep(i, V) rep(j, V) if (i != j &&
                            ps[i].second + mincost[ps[i].first][ps[j].first] <=
                                ps[j].second) add_edge(i, V + j, 1);
    rep(i, V) add_edge(V + i, ed, 1);

    cout << V - max_flow(st, ed) << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define inf (1 << 29)
#define MAX 5000
using namespace std;
typedef pair<int, int> ii;

// flow --- start
struct edge {
  int to, cap, rev;
  edge(int to = inf, int cap = inf, int rev = inf)
      : to(to), cap(cap), rev(rev) {}
};

int V;
vector<edge> G[MAX];
int level[MAX];
int iter[MAX];

void add_edge(int from, int to, int cap) {
  G[from].push_back(edge(to, cap, G[to].size()));
  G[to].push_back(edge(from, 0, G[from].size() - 1));
}

void bfs(int s) {
  rep(i, MAX) level[i] = -1;
  queue<int> que;
  level[s] = 0;
  que.push(s);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    rep(i, G[v].size()) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[e.to] < 0) {
        level[e.to] = level[v] + 1;
        que.push(e.to);
      }
    }
  }
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  for (int &i = iter[v]; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (e.cap > 0 && level[v] < level[e.to]) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int max_flow(int s, int t) {
  int flow = 0;
  for (;;) {
    bfs(s);
    if (level[t] < 0)
      return flow;
    rep(i, MAX) iter[i] = 0;
    int f;
    while ((f = dfs(s, t, inf)) > 0)
      flow += f;
  }
}

// flow --- end

int N, M, L;
int mincost[MAX][MAX];
vector<ii> ps;

int main() {
  while (cin >> N >> M >> L, N | M | L) {
    rep(i, MAX) G[i].clear();
    rep(i, MAX) rep(j, MAX) mincost[j][i] = inf;
    rep(i, MAX) mincost[i][i] = 0;

    {
      int u, v, d;
      rep(i, M) {
        cin >> u >> v >> d;
        mincost[u][v] = mincost[v][u] = d;
      }
      rep(i, MAX) rep(j, MAX) rep(k, MAX) mincost[j][k] =
          min(mincost[j][k], mincost[j][i] + mincost[i][k]);

      ps.clear();
      ps.resize(L);
      rep(i, L) cin >> ps[i].first >> ps[i].second;
      V = L;

      int st = MAX - 2, ed = MAX - 1;
      rep(i, V) add_edge(st, i, inf);
      rep(i, V)
          rep(j, V) if (i != j && ps[i].second + mincost[i][j] <= ps[j].second)
              add_edge(i, V + j, 1);
      rep(i, V) add_edge(V + i, ed, 1);
      /*
      rep(i,2*N)
        {
          cout << "start " << i << endl;
          rep(j,G[i].size())
            {
              cout << "(" << G[i][j].to << "," << G[i][j].cap << ") ";
            }
          cout << endl;
        }
      */
      cout << N - max_flow(st, ed) << endl;
    }
  }
  return 0;
}