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

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
#define rrep(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define erep(i, m, n) for (int(i) = (m); (i) <= (n); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrev(i, m, n) for (int(i) = (n)-1; (i) >= (m); (i)--)
#define erev(i, m, n) for (int(i) = (n); (i) >= (m); (i)--)
#define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define vrep(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
template <class T, class S> inline bool minup(T &m, const S x) {
  return m > (T)x ? (m = (T)x, true) : false;
}
template <class T, class S> inline bool maxup(T &m, const S x) {
  return m < (T)x ? (m = (T)x, true) : false;
}

static const int INF = 1000000000;
static const ll MOD = 1000000007LL;
static const double EPS = 1E-10;

template <typename T> struct edge {
  int to;
  T cap;
  int reverse;
  edge(int to, T cap, int reverse) : to(to), cap(cap), reverse(reverse) {}
};

template <typename T> class Dinic {
private:
  static const T INF = (T)1000000000;
  vector<edge<T>> *G;
  int N;
  int *level;
  int *iter;

  void bfs(int s);
  T dfs(int v, int t, T f);

public:
  Dinic(int N) : N(N) { this->init(); }
  ~Dinic();
  void init();
  void addEdge(int from, int to, T cap);
  T MaximumFlow(int s, int t);
};

template <typename T> Dinic<T>::~Dinic() {
  delete[] G;
  delete[] level;
  delete[] iter;
}

template <typename T> void Dinic<T>::init() {
  G = new vector<edge<T>>[N];
  level = new int[N];
  iter = new int[N];
  rep(i, N) G[i].clear();
}

template <typename T> void Dinic<T>::addEdge(int from, int to, T cap) {
  G[from].pb(edge<T>(to, cap, G[to].size()));
  G[to].pb(edge<T>(from, (T)0, G[from].size() - 1));
}

template <typename T> void Dinic<T>::bfs(int s) {
  rep(i, N) level[i] = -1;
  queue<int> que;
  level[s] = 0;
  que.push(s);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    vrep(w, G[v]) {
      edge<T> &e = *w;
      if (e.cap > (T)0 && level[e.to] < 0) {
        level[e.to] = level[v] + 1;
        que.push(e.to);
      }
    }
  }
}

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

template <typename T> T Dinic<T>::MaximumFlow(int s, int t) {
  T flow = (T)0;
  while (true) {
    bfs(s);
    if (level[t] < (T)0)
      return flow;
    rep(i, N) iter[i] = 0;
    T f;
    while ((f = dfs(s, t, Dinic::INF)) > (T)0) {
      flow += f;
    }
  }
}

int N, M, L;
int u, v, dd;
int d[111][111];
int p[1111];
int t[1111];

int main(int argc, char *argv[]) {
  while (cin >> N >> M >> L, N) {
    Dinic<int> dinic = Dinic<int>(2 * L + 2);
    rep(i, N) rep(j, N) d[i][j] = INF;
    rep(i, N) d[i][i] = 0;
    rep(i, M) {
      cin >> u >> v >> dd;
      d[u][v] = dd;
      d[v][u] = dd;
    }
    rep(k, N) rep(i, N) rep(j, N) minup(d[i][j], d[i][k] + d[k][j]);
    rep(i, L) cin >> p[i] >> t[i];
    rep(i, L) rep(j, L) if (i != j && t[i] + d[p[i]][p[j]] <= t[j])
        dinic.addEdge(i, j + L, 1);
    int s = 2 * L;
    int t = s + 1;
    rep(i, L) dinic.addEdge(s, i, 1);
    rep(j, L) dinic.addEdge(j + L, t, 1);
    cout << L - dinic.MaximumFlow(s, t) << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
#define rrep(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define erep(i, m, n) for (int(i) = (m); (i) <= (n); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrev(i, m, n) for (int(i) = (n)-1; (i) >= (m); (i)--)
#define erev(i, m, n) for (int(i) = (n); (i) >= (m); (i)--)
#define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define vrep(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
template <class T, class S> inline bool minup(T &m, const S x) {
  return m > (T)x ? (m = (T)x, true) : false;
}
template <class T, class S> inline bool maxup(T &m, const S x) {
  return m < (T)x ? (m = (T)x, true) : false;
}

static const int INF = 1000000000;
static const ll MOD = 1000000007LL;
static const double EPS = 1E-10;

template <typename T> struct edge {
  int to;
  T cap;
  int reverse;
  edge(int to, T cap, int reverse) : to(to), cap(cap), reverse(reverse) {}
};

template <typename T> class Dinic {
private:
  static const T INF = (T)1000000000;
  vector<edge<T>> *G;
  int N;
  int *level;
  int *iter;

  void bfs(int s);
  T dfs(int v, int t, T f);

public:
  Dinic(int N) : N(N) { this->init(); }
  void init();
  void addEdge(int from, int to, T cap);
  T MaximumFlow(int s, int t);
};

template <typename T> void Dinic<T>::init() {
  G = new vector<edge<T>>[N];
  level = new int[N];
  iter = new int[N];
  rep(i, N) G[i].clear();
}

template <typename T> void Dinic<T>::addEdge(int from, int to, T cap) {
  G[from].pb(edge<T>(to, cap, G[to].size()));
  G[to].pb(edge<T>(from, (T)0, G[from].size() - 1));
}

template <typename T> void Dinic<T>::bfs(int s) {
  rep(i, N) level[i] = -1;
  queue<int> que;
  level[s] = 0;
  que.push(s);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    vrep(w, G[v]) {
      edge<T> &e = *w;
      if (e.cap > (T)0 && level[e.to] < 0) {
        level[e.to] = level[v] + 1;
        que.push(e.to);
      }
    }
  }
}

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

template <typename T> T Dinic<T>::MaximumFlow(int s, int t) {
  T flow = (T)0;
  while (true) {
    bfs(s);
    if (level[t] < (T)0)
      return flow;
    rep(i, N) iter[i] = 0;
    T f;
    while ((f = dfs(s, t, Dinic::INF)) > (T)0) {
      flow += f;
    }
  }
}

int N, M, L;
int u, v, dd;
int d[111][111];
int p[1111];
int t[1111];

int main(int argc, char *argv[]) {
  while (cin >> N >> M >> L, N) {
    Dinic<int> dinic = Dinic<int>(2 * L + 2);
    rep(i, N) rep(j, N) d[i][j] = INF;
    rep(i, N) d[i][i] = 0;
    rep(i, M) {
      cin >> u >> v >> dd;
      d[u][v] = dd;
      d[v][u] = dd;
    }
    rep(k, N) rep(i, N) rep(j, N) minup(d[i][j], d[i][k] + d[k][j]);
    rep(i, L) cin >> p[i] >> t[i];
    rep(i, L) rep(j, L) if (i != j && t[i] + d[p[i]][p[j]] <= t[j])
        dinic.addEdge(i, j + L, 1);
    int s = 2 * L;
    int t = s + 1;
    rep(i, L) dinic.addEdge(s, i, 1);
    rep(j, L) dinic.addEdge(j + L, t, 1);
    cout << L - dinic.MaximumFlow(s, t) << endl;
  }

  return 0;
}