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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <utility>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define ft first
#define sd second
#define MAX 1000 * 2 + 16
vector<int> arr[MAX];
int x[MAX], vis[MAX];
int dist[105][105];
int N, M, L;
void floyd() {
  int k, i, j;
  for (i = 0; i < N; i++)
    dist[i][i] = 0;
  for (k = 0; k < N; k++)
    for (i = 0; i < N; i++)
      for (j = 0; j < N; j++)
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}

int dfs(int v) {
  vis[v] = 1;
  for (int i = 0; i < arr[v].size(); i++) {
    int m = arr[v][i];
    int w = x[m];
    if (w < 0 || (!vis[w] && dfs(w))) {
      x[v] = m;
      x[m] = v;
      return 1;
    }
  }
  return 0;
}
int V;
int solve() {
  int ans = 0;
  memset(x, -1, sizeof(x));
  for (int i = 0; i < V; i++) {
    if (x[i] < 0) {
      memset(vis, 0, sizeof(vis));
      ans += dfs(i);
    }
  }
  return ans;
}
void add(int u, int v) {
  arr[u].push_back(v);
  arr[v].push_back(u);
}
int p[1050], t[1050];
int main() {
  while (scanf("%d%d%d", &N, &M, &L) != EOF && N) {
    V = L * 2;
    memset(dist, INF, sizeof(dist));
    int i, m1, m2, m3;
    for (i = 0; i < M; i++) {
      scanf("%d%d%d", &m1, &m2, &m3);
      dist[m1][m2] = dist[m2][m1] = m3;
    }
    floyd();
    for (i = 0; i < L; i++) {
      scanf("%d%d", &m1, &m2);
      p[i] = m1;
      t[i] = m2;
    }
    for (i = 0; i < L; i++) {
      for (int j = 0; j < L; j++) {
        if (i != j && dist[p[i]][p[j]] + t[i] <= t[j]) {
          add(i, L + j);
        }
      }
    }
    printf("%d\\n", L - solve());
    for (i = 0; i < V; i++)
      arr[i].clear();
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

const int MAX_N = 102;
const int MAX_L = 1005;
const int MAX_V = MAX_N << 1;

struct Request {
  int p;
  int t;
  bool operator<(const Request &y) const { return t < y.t; }
} r[MAX_L];

int G[MAX_V][MAX_V];
int deg[MAX_V];
int match[MAX_V];
bool used[MAX_V];

void add_edge(int u, int v);
bool dfs(const int &v);
int bipartite_matching(const int V);

int MAT[MAX_N][MAX_N];
int _min(const int &a, int b);
void warshall_floyd(int n);

int main() {
  int N, M, L, u, v, d;

  while (true) {
    scanf("%d%d%d", &N, &M, &L);
    if (!N) {
      break;
    }
    memset(deg, 0, sizeof(deg));
    memset(MAT, 8, sizeof(MAT));

    while (M--) {
      scanf("%d%d%d", &u, &v, &d);
      MAT[u][v] = d;
      MAT[v][u] = d;
    }
    for (int i = 0; i < N; i++) {
      MAT[i][i] = 0;
    }
    warshall_floyd(N);

    for (int i = 0; i < L; i++) {
      scanf("%d%d", &r[i].p, &r[i].t);
    }
    sort(r, r + L);

    for (int i = 1; i < L; i++) {
      for (int j = 0; j < i; j++) {
        if (r[i].t - r[j].t >= MAT[r[i].p][r[j].p]) {
          add_edge(j, i + L);
        }
      }
    }

    printf("%d\\n", L - bipartite_matching(L << 1));
  }

  return 0;
}

void add_edge(int u, int v) {
  G[u][deg[u]++] = v;
  G[v][deg[v]++] = u;
}

bool dfs(const int &v) {
  used[v] = true;
  for (int i = 0; i < deg[v]; i++) {
    const int &u = G[v][i];
    const int &w = match[u];

    if (w == -1 || !used[w] && dfs(w)) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

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

int _min(const int &a, int b) { return a < b ? a : b; }

void warshall_floyd(int n) {
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        MAT[i][j] = _min(MAT[i][j], MAT[i][k] + MAT[k][j]);
      }
    }
  }
}