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

#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

////////////////////////最大流开始//////////////////////////////////////
#define MAX_V 1000 * 2 + 16
int V;                // 顶点数
vector<int> G[MAX_V]; // 图的邻接表
int match[MAX_V];     // 所匹配的顶点
bool used[MAX_V];     // DFS中用到的访问标记

// 向图中增加一条连接u和v的边
void add_edge(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}

// 通过DFS寻找增广路
bool dfs(int v) {
  used[v] = true;
  for (vector<int>::iterator it = G[v].begin(); it != G[v].end(); ++it) {
    int u = *it, 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, 0, sizeof(used));
      if (dfs(v)) {
        ++res;
      }
    }
  }

  return res;
}

void clear() {
  for (int i = 0; i < V; ++i) {
    G[i].clear();
  }
}

///////////////////////////////最大流结束/////////////////////////////////////
#define MAX_N 100
#define MAX_L 1000
#define INF 0x3f3f3f3f

int distance_matrix[MAX_N][MAX_N]; // 两点间最短路
int p[MAX_L], t[MAX_L];            // 房间编号，时刻

bool update_min(int &v, const int &t) {
  if (t < v) {
    v = t;
    return true;
  }
  return false;
}

///////////////////////////SubMain//////////////////////////////////
int main(int argc, char *argv[]) {
  int N, M, L;
  while (scanf("%d%d%d", &N, &M, &L) != EOF && N) {
    V = L * 2;
    clear();
    memset(distance_matrix, INF, sizeof(distance_matrix));

    for (int _ = 0; _ < M; ++_) {
      int u, v, d;
      scanf("%d%d%d", &u, &v, &d);
      distance_matrix[u][v] = distance_matrix[v][u] = d;
    }

    for (int i = 0; i < L; ++i) {
      scanf("%d%d", p + i, t + i);
    }

    // warshall_floyd 两点间最短路
    for (int k = 0; k < N; ++k) {
      distance_matrix[k][k] = 0;
      for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
          update_min(distance_matrix[i][j],
                     distance_matrix[i][k] + distance_matrix[k][j]);
        }
      }
    }

    for (int i = 0; i < L; ++i) {
      for (int j = 0; j < L; ++j) {
        if (i != j && t[i] + distance_matrix[p[i]][p[j]] <= t[j]) {
          add_edge(2 * i, 2 * j + 1); // 可以在i之后处理j，连一条边
        }
      }
    }

    printf("%d\\n", L - bipartite_matching());
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define nl n << 1
#define nr (n << 1) | 1
#define first fi
#define second se
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const ll INFF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-9;
const ll mod = 1e9 + 7;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch<'0' | ch> '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
void Out(int aa) {
  if (aa > 9)
    Out(aa / 10);
  putchar(aa % 10 + '0');
}
int n, m, l;
int mapp[105][105];
int str[2005][2005];
int use[2005], girl[2005];
void floyd() {
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      for (int k = 1; k <= n; k++)
        mapp[j][k] = min(mapp[j][k], mapp[j][i] + mapp[i][k]);
}
bool dfs(int x) {
  for (int i = 1; i <= l; i++) {
    if (str[x][i] && !use[i]) {
      use[i] = true;
      if (!girl[i] | dfs(girl[i])) {
        girl[i] = x;
        return true;
      }
    }
  }
  return false;
}

int solve() {
  int ans = 0;
  memset(girl, 0, sizeof(girl));
  for (int i = 1; i <= l; i++) {
    memset(use, false, sizeof(use));
    if (dfs(i))
      ans++;
  }
  return ans;
}
int p[1010], t[1010];
int main() {
  while (scanf("%d%d%d", &n, &m, &l) != EOF) {
    if (n + m + l == 0)
      break;
    memset(mapp, INF, sizeof(mapp));
    memset(str, 0, sizeof(str));
    for (int i = 1; i <= m; i++) {
      int a = read(), b = read(), c = read();
      mapp[a + 1][b + 1] = mapp[b + 1][a + 1] = c;
      mapp[i][i] = 0;
    }
    floyd();
    for (int i = 1; i <= l; i++)
      p[i] = read() + 1, t[i] = read();
    for (int i = 1; i <= l; i++) {
      for (int j = 1; j <= l; j++) {
        if (i == j)
          continue;
        if (mapp[p[i]][p[j]] <= t[j] - t[i]) {
          str[i][j] = 1;
        }
      }
    }
    printf("%d\\n", l - solve());
  }
  return 0;
}