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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

#define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define BIT(n, m) (((n) >> (m)) & 1)

typedef long long ll;
typedef pair<int, int> PI;

const ll inf = 1e15;
const ll mod = 1000 * 1000 * 1000 + 7;
const double eps = 1e-9;

template <typename S, typename T>
ostream &operator<<(ostream &out, const pair<S, T> &p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  REP(i, v.size()) {
    if (i > 0)
      out << ", ";
    out << v[i];
  }
  out << "]";
  return out;
}

class MaxFlow {
  typedef long long ll;

  struct edge {
    int to;
    ll cap;
    int rev;
    edge() {}
    edge(int to, ll cap, int rev) : to(to), cap(cap), rev(rev) {}
  };

  std::vector<std::vector<edge>> G;
  std::vector<int> level;
  std::vector<int> iter;

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

  ll dfs(int v, int t, ll f) {
    if (v == t)
      return f;

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

public:
  MaxFlow(int V)
      : G(std::vector<std::vector<edge>>(V)), level(std::vector<int>(V)),
        iter(std::vector<int>(V)) {}

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

  ll solve(int s, int t) {
    ll res = 0, f;
    for (;;) {
      bfs(s);
      if (level[t] < 0)
        break;
      std::fill(iter.begin(), iter.end(), 0);
      while ((f = dfs(s, t, LONG_LONG_MAX)) > 0)
        res += f;
    }
    return res;
  }
};

int main() {

  int n, m, l;
  const int inf = 1000 * 1000 * 1000 + 7;

  while (cin >> n >> m >> l && n + m + l) {
    vector<vector<int>> adj(n, vector<int>(n, inf));
    REP(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      assert(a < n && b < n);
      adj[a][b] = c;
      adj[b][a] = c;
    }

    REP(i, n) adj[i][i] = 0;

    REP(k, n) REP(i, n) REP(j, n) {
      adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
    }
    int s = 2 * l;
    int t = s + 1;
    int V = t + 1;
    MaxFlow mf(V);
    vector<int> p(l), time(l);

    REP(i, l) cin >> p[i] >> time[i];

    REP(i, l) REP(j, l) if (i != j) {
      if (time[i] + adj[p[i]][p[j]] <= time[j]) {
        mf.add_edge(i, j + l, 1);
      }
    }
    REP(i, l) {
      mf.add_edge(s, i, 1);
      mf.add_edge(i + l, t, 1);
    }
    cout << l - mf.solve(s, t) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

#define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define BIT(n, m) (((n) >> (m)) & 1)

typedef long long ll;
typedef pair<int, int> PI;

const ll inf = 1e15;
const ll mod = 1000 * 1000 * 1000 + 7;
const double eps = 1e-9;

template <typename S, typename T>
ostream &operator<<(ostream &out, const pair<S, T> &p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  REP(i, v.size()) {
    if (i > 0)
      out << ", ";
    out << v[i];
  }
  out << "]";
  return out;
}

class MaxFlow {
  typedef long long ll;

  struct edge {
    int to;
    ll cap;
    int rev;
    edge() {}
    edge(int to, ll cap, int rev) : to(to), cap(cap), rev(rev) {}
  };

  std::vector<std::vector<edge>> G;
  std::vector<int> level;
  std::vector<int> iter;

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

  ll dfs(int v, int t, ll f) {
    if (v == t)
      return f;

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

public:
  MaxFlow(int V)
      : G(std::vector<std::vector<edge>>(V)), level(std::vector<int>(V)),
        iter(std::vector<int>(V)) {}

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

  ll solve(int s, int t) {
    ll res = 0, f;
    for (;;) {
      bfs(s);
      if (level[t] < 0)
        break;
      std::fill(iter.begin(), iter.end(), 0);
      while ((f = dfs(s, t, LONG_LONG_MAX)) > 0)
        res += f;
    }
    return res;
  }
};

int main() {

  int n, m, l;
  const int inf = 1000 * 1000 * 1000 + 7;

  while (cin >> n >> m >> l && n + m + l) {
    vector<vector<int>> adj(n, vector<int>(n, inf));
    REP(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      assert(a < n && b < n);
      adj[a][b] = c;
      adj[b][a] = c;
    }

    REP(k, n) REP(i, n) REP(j, n) {
      adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
    }
    int s = 2 * l;
    int t = s + 1;
    int V = t + 1;
    MaxFlow mf(V);
    vector<int> p(l), time(l);

    REP(i, l) cin >> p[i] >> time[i];

    REP(i, l) REP(j, l) if (p[i] != p[j]) {
      if (time[i] + adj[p[i]][p[j]] <= time[j]) {
        mf.add_edge(i, j + l, 1);
      }
    }
    REP(i, l) {
      mf.add_edge(s, i, 1);
      mf.add_edge(i + n, t, 1);
    }
    cout << l - mf.solve(s, t) << endl;
  }
  return 0;
}