Problem p01368 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
struct edge {
  int to, cap, rev;
};
class Max_Flow {
public:
  vector<vector<edge>> G;
  vector<bool> used;
  int size__;
  void init(int _size) {
    G.resize(_size);
    used.resize(_size);
    size__ = _size;
  }
  void add_edge(int from, int to, int cap) {
    G[from].push_back((edge){to, cap, (int)G[to].size()});
    G[to].push_back((edge){from, 0, (int)G[from].size() - 1});
  }
  int dfs(int v, int t, int f) {
    if (v == t)
      return f;
    used[v] = true;
    for (int i = 0; i < G[v].size(); i++) {
      edge &e = G[v][i];
      if (!used[e.to] && e.cap > 0) {
        int d = dfs(e.to, t, min(t, 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 ret = 0;
    while (true) {
      for (int j = 0; j < size__; j++)
        used[j] = false;
      int f = dfs(s, t, 1000000007);
      if (f == 0)
        return ret;
      ret += f;
    }
    return ret;
  }
};

int N, M, L, a, b, c, d[100][100], e1[1000], e2[1000];
int main() {
  while (true) {
    cin >> N >> M >> L;
    if (N + M + L == 0)
      break;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++)
        d[i][j] = 1e9;
      d[i][i] = 0;
    }
    for (int i = 0; i < M; i++) {
      cin >> a >> b >> c;
      d[a][b] = c;
      d[b][a] = c;
    }
    for (int k = 0; k < N; k++) {
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
          d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        }
      }
    }
    for (int i = 0; i < L; i++) {
      cin >> e1[i] >> e2[i];
    }
    Max_Flow X;
    X.init(2 * L + 2);
    for (int i = 0; i < L; i++) {
      for (int j = 0; j < L; j++) {
        if (i == j || d[e1[i]][e1[j]] > e2[j] - e2[i])
          continue;
        X.add_edge(i, j + L, 1);
      }
    }
    for (int i = 0; i < L; i++) {
      X.add_edge(2 * L, i, 1);
    }
    for (int i = 0; i < L; i++) {
      X.add_edge(i + L, 2 * L + 1, 1);
    }
    cout << L - X.max_flow(2 * L, 2 * L + 1) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int n, m, l;
int d[1010][1010];
bool used[1010];
int match[1010];
vector<int> G[1010];
void addedge(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}
struct node {
  int p, t;
} a[1010];
bool cmp(node a1, node b1) { return a1.t < b1.t; }
bool dfs(int v) {
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    int u = G[v][i], w = match[u];
    if (w < 0 || !used[w] && dfs(w)) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}
int bipartite_matching() {
  int res = 0;
  memset(match, -1, sizeof match);
  for (int v = 0; v < 2 * l; v++) {
    if (match[v] < 0) {
      memset(used, 0, sizeof used);
      if (dfs(v))
        res++;
    }
  }
  return res;
}
int main() {
  while (scanf("%d%d%d", &n, &m, &l) != EOF) {
    if (n == 0 && m == 0 && l == 0)
      break;
    int u, v, c;
    memset(d, 0x3f, sizeof d);
    for (int i = 0; i < m; i++) {
      scanf("%d%d%d", &u, &v, &c);
      d[u][v] = d[v][u] = c;
    }
    for (int i = 0; i < n; i++)
      d[i][i] = 0;
    for (int k = 0; k < n; k++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        }
      }
    }
    for (int i = 0; i < l; i++) {
      scanf("%d%d", &a[i].p, &a[i].t);
    }
    for (int i = 0; i < 2 * l; i++) {
      G[i].clear();
    }
    sort(a, a + l, cmp);
    for (int i = 0; i < l; i++) {
      for (int j = i + 1; j < l; j++) {
        if (d[a[i].p][a[j].p] <= a[j].t - a[i].t) {
          addedge(i, j + l);
        }
      }
    }
    int ans = l - bipartite_matching();
    printf("%d\\n", ans);
  }
}