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

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

struct edge {
  int to, cap, cost, rev;
};

const int INF = 1 << 29;
int N, L;
int NV;
vector<vector<edge>> G;

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

int min_cost_flow(int s, int t) {
  int nc = 0;
  for (int c = 1;; c++) {
    int dist[2002];
    fill(begin(dist), end(dist), INF);
    dist[0] = 0;
    int prevv[2002], preve[2002];
    bool update = true;
    while (update) {
      update = false;
      for (int v = 0; v < NV; v++) {
        if (dist[v] == INF)
          continue;
        for (int i = 0; i < G[v].size(); i++) {
          edge &e = G[v][i];
          if (e.cap > 0 && dist[e.to] > dist[v] + e.cost) {
            dist[e.to] = dist[v] + e.cost;
            prevv[e.to] = v;
            preve[e.to] = i;
            update = true;
          }
        }
      }
    }
    nc -= dist[t];
    if (nc == L)
      return c;
    for (int v = t; v != s; v = prevv[v]) {
      edge &e = G[prevv[v]][preve[v]];
      e.cap--;
      G[v][e.rev].cap++;
    }
  }
}

int main() {
  for (int M; cin >> N >> M >> L, N;) {
    NV = 2 * L + 2;
    int d[100][100];
    fill(d[0], d[100], INF);
    for (int i = 0; i < N; i++) {
      d[i][i] = 0;
    }
    while (M--) {
      int u, v, di;
      cin >> u >> v >> di;
      d[u][v] = d[v][u] = min(d[u][v], di);
    }
    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]);
        }
      }
    }
    G = vector<vector<edge>>(NV);
    vector<pair<int, int>> pt;
    for (int i = 0; i < L; i++) {
      int p, t;
      cin >> p >> t;
      pt.emplace_back(t, p);
    }
    sort(begin(pt), end(pt));
    for (int i = 0; i < pt.size(); i++) {
      add_edge(0, 2 * i + 2, 1, 0);
      add_edge(2 * i + 2, 2 * i + 3, 1, -1);
      add_edge(2 * i + 3, 1, 1, 0);
      for (int j = 0; j < i; j++) {
        if (pt[j].first + d[pt[j].second][pt[i].second] <= pt[i].first) {
          add_edge(2 * j + 3, 2 * i + 2, 1, 0);
        }
      }
    }
    cout << min_cost_flow(0, 1) << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

struct edge {
  int to, cap, cost, rev;
};

const int INF = 1 << 29;
int N, L;
int NV;
vector<vector<edge>> G;

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

int min_cost_flow(int s, int t) {
  int nc = 0;
  for (int c = 1;; c++) {
    int dist[202];
    fill(begin(dist), end(dist), INF);
    dist[0] = 0;
    int prevv[202], preve[202];
    bool update = true;
    while (update) {
      update = false;
      for (int v = 0; v < NV; v++) {
        if (dist[v] == INF)
          continue;
        for (int i = 0; i < G[v].size(); i++) {
          edge &e = G[v][i];
          if (e.cap > 0 && dist[e.to] > dist[v] + e.cost) {
            dist[e.to] = dist[v] + e.cost;
            prevv[e.to] = v;
            preve[e.to] = i;
            update = true;
          }
        }
      }
    }
    nc -= dist[t];
    if (nc == L)
      return c;
    for (int v = t; v != s; v = prevv[v]) {
      edge &e = G[prevv[v]][preve[v]];
      e.cap--;
      G[v][e.rev].cap++;
    }
  }
}

int main() {
  for (int M; cin >> N >> M >> L, N;) {
    NV = 2 * L + 2;
    int d[100][100];
    fill(d[0], d[100], INF);
    for (int i = 0; i < N; i++) {
      d[i][i] = 0;
    }
    while (M--) {
      int u, v, di;
      cin >> u >> v >> di;
      d[u][v] = d[v][u] = min(d[u][v], di);
    }
    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]);
        }
      }
    }
    G = vector<vector<edge>>(NV);
    vector<pair<int, int>> pt;
    for (int i = 0; i < L; i++) {
      int p, t;
      cin >> p >> t;
      pt.emplace_back(t, p);
    }
    sort(begin(pt), end(pt));
    for (int i = 0; i < pt.size(); i++) {
      add_edge(0, 2 * i + 2, 1, 0);
      add_edge(2 * i + 2, 2 * i + 3, 1, -1);
      add_edge(2 * i + 3, 1, 1, 0);
      for (int j = 0; j < i; j++) {
        if (pt[j].first + d[pt[j].second][pt[i].second] <= pt[i].first) {
          add_edge(2 * j + 3, 2 * i + 2, 1, 0);
        }
      }
    }
    cout << min_cost_flow(0, 1) << endl;
  }
}