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

#include "bits/stdc++.h"

#define REP(i, n) for (ll i = 0; i < ll(n); ++i)
#define RREP(i, n) for (ll i = ll(n) - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < ll(n); ++i)
#define RFOR(i, m, n) for (ll i = ll(n) - 1; i >= ll(m); --i)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
#define DUMP(v)                                                                \\
  REP(aa, (v).size()) {                                                        \\
    cout << v[aa];                                                             \\
    if (aa != v.size() - 1)                                                    \\
      cout << " ";                                                             \\
    else                                                                       \\
      cout << endl;                                                            \\
  }
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll max(ll a, int b) { return max(a, ll(b)); }
ll max(int a, ll b) { return max(ll(a), b); }
ll min(ll a, int b) { return min(a, ll(b)); }
ll min(int a, ll b) { return min(ll(a), b); }
///(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)///
struct edge {
  int to, cap, rev, cost;
};

typedef vector<vector<edge>> Graph;
constexpr int MAX_V = 2002;

int level[MAX_V];
int iter[MAX_V];

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

void bfs(Graph &G, int s) {
  fill_n(level, MAX_V, -1);
  queue<int> q;
  level[s] = 0;
  q.push(s);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < G[v].size(); i++) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[e.to] < 0) {
        level[e.to] = level[v] + 1;
        q.push(e.to);
      }
    }
  }
}
ll dfs(Graph &G, 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(G, e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}
ll max_flow(Graph &G, int s, int t) {
  int flow = 0;
  for (;;) {
    bfs(G, s);
    if (level[t] < 0)
      return flow;
    fill_n(iter, MAX_V, 0);
    ll f;
    while ((f = dfs(G, s, t, INF)) > 0) {
      flow += f;
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m, l;
  while (cin >> n >> m >> l, n) {
    Graph g(2 * l + 2);
    vvi v(n, vi(n, INF));
    REP(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      v[a][b] = c;
      v[b][a] = c;
    }
    REP(i, n) v[i][i] = 0;
    REP(k, n) {
      REP(i, n) {
        REP(j, n) { v[i][j] = min(v[i][j], v[i][k] + v[k][j]); }
      }
    }
    vi p(l), t(l);
    REP(i, l) { cin >> p[i] >> t[i]; }
    REP(i, l) {
      REP(j, l) {
        if (i == j)
          continue;
        if (t[i] + v[p[i]][p[j]] <= t[j])
          add_edge(g, i + 1, j + l + 1, 1);
      }
    }
    REP(i, l) add_edge(g, 0, i + 1, 1);
    REP(i, l) add_edge(g, i + l + 1, 2 * l + 1, 1);
    cout << l - max_flow(g, 0, 2 * l + 1) << endl;
  }
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"

#define REP(i, n) for (ll i = 0; i < ll(n); ++i)
#define RREP(i, n) for (ll i = ll(n) - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < ll(n); ++i)
#define RFOR(i, m, n) for (ll i = ll(n) - 1; i >= ll(m); --i)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
#define DUMP(v)                                                                \\
  REP(aa, (v).size()) {                                                        \\
    cout << v[aa];                                                             \\
    if (aa != v.size() - 1)                                                    \\
      cout << " ";                                                             \\
    else                                                                       \\
      cout << endl;                                                            \\
  }
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll max(ll a, int b) { return max(a, ll(b)); }
ll max(int a, ll b) { return max(ll(a), b); }
ll min(ll a, int b) { return min(a, ll(b)); }
ll min(int a, ll b) { return min(ll(a), b); }
///(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)///
struct edge {
  int to, cap, rev, cost;
};

typedef vector<vector<edge>> Graph;
constexpr int MAX_V = 2002;

int level[MAX_V];
int iter[MAX_V];

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

void bfs(Graph &G, int s) {
  fill_n(level, MAX_V, -1);
  queue<int> q;
  level[s] = 0;
  q.push(s);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < G[v].size(); i++) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[e.to] < 0) {
        level[e.to] = level[v] + 1;
        q.push(e.to);
      }
    }
  }
}
ll dfs(Graph &G, 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(G, e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}
ll max_flow(Graph &G, int s, int t) {
  int flow = 0;
  for (;;) {
    bfs(G, s);
    if (level[t] < 0)
      return flow;
    fill_n(iter, MAX_V, 0);
    ll f;
    while ((f = dfs(G, s, t, INF)) > 0) {
      flow += f;
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m, l;
  while (cin >> n >> m >> l, n) {
    Graph g(2 * l + 2);
    vvi v(n, vi(n, INF));
    REP(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      v[a][b] = c;
      v[b][a] = c;
    }
    REP(k, n) {
      REP(i, n) {
        REP(j, n) { v[i][j] = min(v[i][j], v[i][k] + v[k][j]); }
      }
    }
    vi p(l), t(l);
    REP(i, l) { cin >> p[i] >> t[i]; }
    REP(i, l) {
      REP(j, l) {
        if (i == j)
          continue;
        if (t[i] + v[p[i]][p[j]] <= t[j])
          add_edge(g, p[i] + 1, p[j] + l + 1, 1);
      }
    }
    REP(i, l) add_edge(g, 0, i + 1, 1);
    REP(i, l) add_edge(g, i + l + 1, 2 * l + 1, 1);
    cout << l - max_flow(g, 0, 2 * l + 1) << endl;
    REP(i, MAX_V) {

      level[i] = 0;
      iter[i] = 0;
    }
  }
}