Problem p01368 (C++)

Buggy submission (status: Runtime Error):
// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

int N, M, L;
const int MAX_V = 300;
int V;
VI biG[MAX_V];
VI origG[MAX_V];
int match[MAX_V];
bool used[MAX_V];

void add_edge(int u, int v) {
  biG[u].push_back(v);
  biG[v].push_back(u);
}

bool dfs(int v) {
  used[v] = true;
  for (int i = 0; i < biG[v].size(); ++i) {
    int u = biG[v][i], w = match[u];
    if (w < 0 || !used[w] && dfs(w)) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

int bipartite_matching() {
  int res = 0;
  memset(match, -1, sizeof(match));
  for (int v = 0; v < V; ++v) {
    if (match[v] < 0) {
      memset(used, false, sizeof(used));
      if (dfs(v))
        ++res;
    }
  }
  return res;
}

int findMinPathcover(int n) {
  REP(i, MAX_V) biG[i].clear();
  V = 2 * n;
  REP(i, n) REP(j, SZ(origG[i])) { add_edge(i, n + origG[i][j]); }
  int res = bipartite_matching();
  return n - res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  while (cin >> N >> M >> L, N) {
    REP(i, MAX_V) origG[i].clear();
    vector<vector<LL>> dist(N, vector<LL>(N, 1e+15));
    REP(i, N) dist[i][i] = 0;
    REP(i, M) {
      int u, v, d;
      cin >> u >> v >> d;
      dist[u][v] = dist[v][u] = d;
    }

    REP(k, N)
    REP(i, N) REP(j, N) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);

    vector<LL> p(L), t(L);
    REP(i, L) cin >> p[i] >> t[i];
    REP(i, L)
    REP(j, L) if (i != j && t[i] + dist[p[i]][p[j]] <= t[j]) origG[i].PB(j);
    cout << findMinPathcover(L) << endl;
  }

  return 0;
}

Fixed submission:
// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

int N, M, L;
const int MAX_V = 2100;
int V;
VI biG[MAX_V];
VI origG[MAX_V];
int match[MAX_V];
bool used[MAX_V];

void add_edge(int u, int v) {
  biG[u].push_back(v);
  biG[v].push_back(u);
}

bool dfs(int v) {
  used[v] = true;
  for (int i = 0; i < biG[v].size(); ++i) {
    int u = biG[v][i], w = match[u];
    if (w < 0 || !used[w] && dfs(w)) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

int bipartite_matching() {
  int res = 0;
  memset(match, -1, sizeof(match));
  for (int v = 0; v < V; ++v) {
    if (match[v] < 0) {
      memset(used, false, sizeof(used));
      if (dfs(v))
        ++res;
    }
  }
  return res;
}

int findMinPathcover(int n) {
  REP(i, MAX_V) biG[i].clear();
  V = 2 * n;
  REP(i, n) REP(j, SZ(origG[i])) { add_edge(i, n + origG[i][j]); }
  int res = bipartite_matching();
  return n - res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  while (cin >> N >> M >> L, N) {
    REP(i, MAX_V) origG[i].clear();
    vector<vector<LL>> dist(N, vector<LL>(N, 1e+15));
    REP(i, N) dist[i][i] = 0;
    REP(i, M) {
      int u, v, d;
      cin >> u >> v >> d;
      dist[u][v] = dist[v][u] = d;
    }

    REP(k, N)
    REP(i, N) REP(j, N) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);

    vector<LL> p(L), t(L);
    REP(i, L) cin >> p[i] >> t[i];
    REP(i, L)
    REP(j, L) if (i != j && t[i] + dist[p[i]][p[j]] <= t[j]) origG[i].PB(j);
    cout << findMinPathcover(L) << endl;
  }

  return 0;
}