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

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>

using namespace std;
typedef long long ll;

/**
 * Dinic?????????????????§???
 *
 * ?????¨???????????¬????????????
 * template?????°???int V???????????°
 */
const int INF = 1 << 28;
template <int V> struct MaxFlow {
  typedef int T;
  struct Edge {
    int to, rev;
    T cap;
  };
  vector<Edge> g[V];
  int level[V];
  int iter[V];
  /// ?????????
  void init() {
    for (int i = 0; i < V; i++) {
      g[i].clear();
    }
  }
  /// ??????????????????
  void add(int from, int to, T cap) {
    g[from].push_back(Edge{to, (int)g[to].size(), cap});
    g[to].push_back(Edge{from, (int)g[from].size() - 1, 0});
  }
  /// ??????????????????
  void add_multi(int from, int to, T cap) {
    g[from].push_back(Edge{to, (int)g[to].size(), cap});
    g[to].push_back(Edge{from, (int)g[from].size() - 1, cap});
  }

  void bfs(int s) {
    fill_n(level, V, -1);
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (Edge e : g[v]) {
        if (e.cap <= 0)
          continue;
        if (level[e.to] < 0) {
          level[e.to] = level[v] + 1;
          que.push(e.to);
        }
      }
    }
  }

  T dfs(int v, int t, T 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)
        continue;
      if (level[v] < level[e.to]) {
        T d = dfs(e.to, t, min(f, e.cap));
        if (d <= 0)
          continue;
        e.cap -= d;
        g[e.to][e.rev].cap += d;
        return d;
      }
    }
    return 0;
  }

  // s??????t???????????§????????????
  T exec(int s, int t) {
    T flow = 0;
    while (true) {
      bfs(s);
      if (level[t] < 0)
        return flow;
      fill_n(iter, V, 0);
      T f;
      while ((f = dfs(s, t, INF)) > 0) {
        flow += f;
      }
    }
  }
};

typedef pair<int, int> P;
const int MN = 110;
const int ML = 1100;
MaxFlow<ML * 2> mf;

int dist[MN][MN];
P qu[ML];
bool solve() {
  mf.init();
  int n, m, l;
  cin >> n >> m >> l;
  if (!n)
    return false;
  for (int i = 0; i < MN; i++) {
    for (int j = 0; j < MN; j++) {
      if (i == j)
        dist[i][j] = 0;
      else
        dist[i][j] = 1e8;
    }
  }
  for (int i = 0; i < m; i++) {
    int u, v, d;
    cin >> u >> v >> d;
    dist[u][v] = d;
    dist[v][u] = d;
  }
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
      }
    }
  }

  for (int i = 0; i < l; i++) {
    int p, t;
    cin >> p >> t;
    qu[i] = P(t, p);
  }
  sort(qu, qu + l);

  int vs = 2 * ML - 1;
  int vt = 2 * ML - 2;
  for (int i = 0; i < l; i++) {
    for (int j = i + 1; j < l; j++) {
      int ap, at, bp, bt;
      tie(at, ap) = qu[i];
      tie(bt, bp) = qu[j];
      if (dist[ap][bp] <= bt - at) {
        mf.add(i, j + ML, 1);
      }
    }
  }
  for (int i = 0; i < l; i++) {
    mf.add(vs, i, 1);
    mf.add(i + ML, vt, 1);
  }
  cout << l - mf.exec(vs, vt) << endl;
  return true;
}

int main() {
  while (true) {
    if (!solve())
      break;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>

using namespace std;
typedef long long ll;

/**
 * Dinic?????????????????§???
 *
 * ?????¨???????????¬????????????
 * template?????°???int V???????????°
 */
const int INF = 1 << 28;
template <int V> struct MaxFlow {
  typedef int T;
  struct Edge {
    int to, rev;
    T cap;
  };
  vector<Edge> g[V];
  int level[V];
  int iter[V];
  /// ?????????
  void init() {
    for (int i = 0; i < V; i++) {
      g[i].clear();
    }
  }
  /// ??????????????????
  void add(int from, int to, T cap) {
    g[from].push_back(Edge{to, (int)g[to].size(), cap});
    g[to].push_back(Edge{from, (int)g[from].size() - 1, 0});
  }
  /// ??????????????????
  void add_multi(int from, int to, T cap) {
    g[from].push_back(Edge{to, (int)g[to].size(), cap});
    g[to].push_back(Edge{from, (int)g[from].size() - 1, cap});
  }

  void bfs(int s) {
    fill_n(level, V, -1);
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (Edge e : g[v]) {
        if (e.cap <= 0)
          continue;
        if (level[e.to] < 0) {
          level[e.to] = level[v] + 1;
          que.push(e.to);
        }
      }
    }
  }

  T dfs(int v, int t, T 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)
        continue;
      if (level[v] < level[e.to]) {
        T d = dfs(e.to, t, min(f, e.cap));
        if (d <= 0)
          continue;
        e.cap -= d;
        g[e.to][e.rev].cap += d;
        return d;
      }
    }
    return 0;
  }

  // s??????t???????????§????????????
  T exec(int s, int t) {
    T flow = 0;
    while (true) {
      bfs(s);
      if (level[t] < 0)
        return flow;
      fill_n(iter, V, 0);
      T f;
      while ((f = dfs(s, t, INF)) > 0) {
        flow += f;
      }
    }
  }
};

typedef pair<int, int> P;
const int MN = 110;
const int ML = 1100;
MaxFlow<MN * 2> mf;

int dist[MN][MN];
P qu[ML];
bool solve() {
  mf.init();
  int n, m, l;
  cin >> n >> m >> l;
  if (!n)
    return false;
  for (int i = 0; i < MN; i++) {
    for (int j = 0; j < MN; j++) {
      if (i == j)
        dist[i][j] = 0;
      else
        dist[i][j] = 1e8;
    }
  }
  for (int i = 0; i < m; i++) {
    int u, v, d;
    cin >> u >> v >> d;
    dist[u][v] = d;
    dist[v][u] = d;
  }
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
      }
    }
  }

  for (int i = 0; i < l; i++) {
    int p, t;
    cin >> p >> t;
    qu[i] = P(t, p);
  }
  sort(qu, qu + l);

  int vs = 2 * MN - 1;
  int vt = 2 * MN - 2;
  for (int i = 0; i < l; i++) {
    for (int j = i + 1; j < l; j++) {
      int ap, at, bp, bt;
      tie(at, ap) = qu[i];
      tie(bt, bp) = qu[j];
      if (dist[ap][bp] <= bt - at) {
        mf.add(i, j + MN, 1);
      }
    }
  }
  for (int i = 0; i < l; i++) {
    mf.add(vs, i, 1);
    mf.add(i + MN, vt, 1);
  }
  cout << l - mf.exec(vs, vt) << endl;
  return true;
}

int main() {
  while (true) {
    if (!solve())
      break;
  }
  return 0;
}