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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P, int> pip;
typedef vector<pip> vip;
const int inf = 1 << 29;
const ll INF = 1ll << 50;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

class Graph {
private:
  int V;
  vvi g;

public:
  vi match, used;
  bool BM_dfs(int v) {
    used[v] = 1;
    for (int i = 0; i < g[v].size(); i++) {
      int u = g[v][i], w = match[u];
      if (w < 0 || !used[w] && BM_dfs(w)) {
        match[v] = u;
        match[u] = v;
        return 1;
      }
    }
    return 0;
  }
  int BM() {
    int res = 0;
    match = vi(V, -1);
    for (int i = 0; i < V; i++)
      if (match[i] < 0) {
        used = vi(V);
        if (BM_dfs(i))
          res++;
      }
    return res;
  }
  Graph(int v) {
    V = v;
    g = vvi(v);
  }
  void add_edge(int s, int t) {
    g[s].push_back(t);
    g[t].push_back(s);
  }
};

int n, m, l;

int main() {
  while (1) {
    cin >> n >> m >> l;
    if (!n)
      break;
    Graph g(2 * l);
    vvi G(n, vi(n, inf));
    vp a(l);
    for (int i = 0; i < m; i++) {
      int v, u, c;
      cin >> v >> u >> c;
      G[v][u] = G[u][v] = c;
    }
    for (int i = 0; i < l; i++) {
      int p, t;
      cin >> p >> t;
      a[i] = {t, p};
    }
    for (int i = 0; i < n; i++)
      G[i][i] = 0;
    for (int k = 0; k < n; k++)
      for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
          G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
    for (int i = 0; i < l; i++)
      for (int j = 0; j < l; j++) {
        if (i != j && G[a[i].second][a[j].second] <= a[j].first - a[i].first)
          g.add_edge(i, l + j);
      }
    cout << l - g.BM() << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P, int> pip;
typedef vector<pip> vip;
const int inf = 1 << 29;
const ll INF = 1ll << 50;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

class Union_Find_Tree {
public:
  vi Par, Rank, Size;
  Union_Find_Tree(int n) {
    Par = Rank = vi(n);
    Size = vi(n, 1);
    for (int i = 0; i < n; i++)
      Par[i] = i;
  }
  int Find_Par(int x) {
    if (Par[x] == x)
      return x;
    return Par[x] = Find_Par(Par[x]);
  }
  int Find_Size(int x) { return Size[Find_Par(x)]; }
  bool Unite(int x, int y) {
    x = Find_Par(x);
    y = Find_Par(y);
    if (x == y)
      return 0;
    if (Rank[x] < Rank[y]) {
      Par[x] = y;
      Size[y] += Size[x];
    } else {
      Par[y] = x;
      Size[x] += Size[y];
      if (Rank[x] == Rank[y])
        Rank[x]++;
    }
    return 1;
  }
  bool Same(int x, int y) { return Find_Par(x) == Find_Par(y); }
};

class Graph {
private:
  int V;
  vvi g;

public:
  vi match, used;
  bool BM_dfs(int v) {
    used[v] = 1;
    for (int i = 0; i < g[v].size(); i++) {
      int u = g[v][i], w = match[u];
      if (w < 0 || !used[u] && BM_dfs(u)) {
        match[v] = u;
        match[u] = v;
        return 1;
      }
    }
    return 0;
  }
  int BM() {
    int res = 0;
    match = vi(V, -1);
    for (auto i = 0; i < V; i++)
      if (match[i] < 0) {
        used = vi(V);
        if (BM_dfs(i))
          res++;
      }
    return res;
  }
  Graph(int v) {
    V = v;
    g = vvi(v);
  }
  void add_edge(int s, int t) {
    g[s].push_back(t);
    g[t].push_back(s);
  }
};

int n, m, l;

int main() {
  while (1) {
    cin >> n >> m >> l;
    if (!n)
      break;
    Graph g(2 * l);
    Union_Find_Tree uft(n);
    vvi G(n, vi(n, inf));
    vp a(l);
    for (int i = 0; i < m; i++) {
      int v, u, c;
      cin >> v >> u >> c;
      G[v][u] = c;
      G[u][v] = c;
    }
    for (int i = 0; i < l; i++) {
      int p, t;
      cin >> p >> t;
      a[i] = {t, p};
    }
    sort(a.begin(), a.end());
    for (int i = 0; i < n; i++)
      G[i][i] = 0;
    for (int k = 0; k < n; k++)
      for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
          G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
    for (int i = 1; i < l - 1; i++)
      for (int j = 0; j < l; j++)
        if (i + j < l) {
          int v = a[j].second, u = a[i + j].second;
          if (G[v][u] <= a[i + j].first - a[j].first && !uft.Same(v, u)) {
            uft.Unite(j, j + i);
            g.add_edge(j, j + i + u);
          }
        }
    cout << n - g.BM() << endl;
  }
}