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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  EACH(x, vec) is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << endl;
    os << vec[i];
  }
  return os;
}

struct Edge {
  int to, cap, rev;
};
vector<vector<Edge>> G;
int dist[110][110];

void add_edge(int from, int to, int cap) {
  //	cout << from << " " << to << " " << cap << endl;
  G[from].pb({to, cap, (int)G[to].size()});
  G[to].pb({from, 0, (int)G[from].size() - 1});
}
int cost[3000];
void bfs(int s) {
  fill(cost, cost + 3000, inf);
  queue<int> Q;
  Q.push(s);
  cost[s] = 0;
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop();
    EACH(e, G[v]) {
      if (e.cap > 0 && cost[v] + 1 < cost[e.to]) {
        cost[e.to] = cost[v] + 1;
        Q.push(e.to);
      }
    }
  }
}
bool used[3000];
int dfs(int s, int t, int f) {
  if (s == t)
    return f;
  if (used[s])
    return 0;
  used[s] = true;
  EACH(e, G[s]) {
    if (e.cap > 0 && cost[e.to] == cost[s] + 1) {
      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 res = 0;
  while (1) {
    bfs(s);
    if (cost[t] == inf)
      break;
    while (1) {
      fill(used, used + 3000, false);
      int f = dfs(s, t, inf);
      if (f == 0)
        break;
      res += f;
    }
  }
  return res;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  /*
          G.resize(4);
          add_edge(0, 1, 5);
          add_edge(0, 2, 4);
          add_edge(1, 2, 3);
          add_edge(1, 3, 10);
          add_edge(2, 3, 5);
          cout << max_flow(0, 3) << endl;
          return 0;
  */
  int N, M, L;
  while (cin >> N >> M >> L, N || M || L) {
    fill(dist[0], dist[110], inf);
    REP(i, M) {
      int u, v, d;
      cin >> u >> v >> d;
      dist[u][v] = min(dist[u][v], d);
      dist[v][u] = min(dist[v][u], d);
    }
    REP(i, N) dist[i][i] = 0;
    REP(k, N)
    REP(i, N) REP(j, N) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
    vector<P> task(L);
    REP(i, L) {
      int p, t;
      cin >> p >> t;
      task[i] = P(p, t);
    }
    int s = L * 2, t = s + 1;
    G = vector<vector<Edge>>(t + 1);
    REP(i, L) REP(j, L) {
      if (i == j)
        continue;
      if (task[i].second + dist[task[i].first][task[j].first] > task[j].second)
        continue;
      add_edge(i, L + j, 1);
    }
    REP(i, L) add_edge(s, i, 1);
    REP(i, L) add_edge(L + i, t, 1);
    cout << L - max_flow(s, t) << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  EACH(x, vec) is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << endl;
    os << vec[i];
  }
  return os;
}

struct Edge {
  int to, cap, rev;
};
vector<vector<Edge>> G;
int dist[110][110];

void add_edge(int from, int to, int cap) {
  //	cout << from << " " << to << " " << cap << endl;
  G[from].pb({to, cap, (int)G[to].size()});
  G[to].pb({from, 0, (int)G[from].size() - 1});
}
int cost[3000];
void bfs(int s) {
  fill(cost, cost + 3000, inf);
  queue<int> Q;
  Q.push(s);
  cost[s] = 0;
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop();
    EACH(e, G[v]) {
      if (e.cap > 0 && cost[v] + 1 < cost[e.to]) {
        cost[e.to] = cost[v] + 1;
        Q.push(e.to);
      }
    }
  }
}
bool used[3000];
int dfs(int s, int t, int f) {
  if (s == t)
    return f;
  if (used[s])
    return 0;
  used[s] = true;
  EACH(e, G[s]) {
    if (e.cap > 0) {
      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 res = 0;
  while (1) {
    bfs(s);
    if (cost[t] == inf)
      break;
    while (1) {
      fill(used, used + 3000, false);
      int f = dfs(s, t, inf);
      if (f == 0)
        break;
      res += f;
    }
  }
  return res;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  /*
          G.resize(4);
          add_edge(0, 1, 5);
          add_edge(0, 2, 4);
          add_edge(1, 2, 3);
          add_edge(1, 3, 10);
          add_edge(2, 3, 5);
          cout << max_flow(0, 3) << endl;
          return 0;
  */
  int N, M, L;
  while (cin >> N >> M >> L, N || M || L) {
    fill(dist[0], dist[110], inf);
    REP(i, M) {
      int u, v, d;
      cin >> u >> v >> d;
      dist[u][v] = min(dist[u][v], d);
      dist[v][u] = min(dist[v][u], d);
    }
    REP(i, N) dist[i][i] = 0;
    REP(k, N)
    REP(i, N) REP(j, N) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
    vector<P> task(L);
    REP(i, L) {
      int p, t;
      cin >> p >> t;
      task[i] = P(p, t);
    }
    int s = L * 2, t = s + 1;
    G = vector<vector<Edge>>(t + 1);
    REP(i, L) REP(j, L) {
      if (i == j)
        continue;
      if (task[i].second + dist[task[i].first][task[j].first] > task[j].second)
        continue;
      add_edge(task[i].first, L + task[j].first, 1);
    }
    REP(i, L) add_edge(s, i, 1);
    REP(i, L) add_edge(L + i, t, 1);
    cout << L - max_flow(s, t) << endl;
  }
}