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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int const nMax = 1010;
long long road[2200][2200];
int n, m, L, x, y, cost, judge, g[2200][2200], tot;
int use[2200], from[2200];
struct SANTA {
  int house;
  long long time;
} santa[nMax];
struct present {
  int a;
  long long time;
} pre[nMax];
bool match(int x) {
  for (int i = L; i < 2 * L; i++) {
    if (g[x][i] && !use[i]) {
      use[i] = 1;
      if (from[i] == -1 || match(from[i])) {
        from[i] = x;
        return true;
      }
    }
  }
  return false;
}
int hungary() {
  tot = 0;
  memset(from, -1, sizeof(from));
  for (int i = 0; i < L; i++) {
    memset(use, 0, sizeof(use));
    if (match(i))
      ++tot;
  }
  return tot;
}
int main() {
  while (scanf("%d%d%d", &n, &m, &L), n || m || L) {
    int ans = 0;
    memset(road, 63, sizeof(road));
    for (int i = 1; i <= m; i++) {
      scanf("%d%d%d", &x, &y, &cost);
      if (road[x][y] > cost)
        road[x][y] = road[y][x] = cost;
    }
    for (int k = 0; k < n; k++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
          if (road[i][j] > road[i][k] + road[k][j])
            road[i][j] = road[i][k] + road[k][j];
      }
    }
    for (int i = 0; i < n; i++)
      road[i][i] = 0;
    for (int i = 0; i < L; i++)
      scanf("%d%d", &pre[i].a, &pre[i].time);
    memset(g, 0, sizeof(g));
    for (int i = 0; i < L; i++) {
      for (int j = 0; j < L; j++)
        if (i != j && road[pre[j].a][pre[i].a] <= pre[j].time - pre[i].time &&
            pre[j].time >= pre[i].time)
          g[i][j + L] = 1;
    }
    ans = L - hungary();
    printf("%d\\n", ans);
  }
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>

unsigned d[100][100];

struct node {
  int v, next;
} e[2000];
int v[2000], p[1000], t[1000], c[2000];
bool f[2000];

bool dfs(int n) {
  int i, j, k;

  f[n] = 1;
  for (i = v[n]; i >= 0; i = e[i].next) {
    if ((k = c[j = e[i].v]) < 0 || !f[k] && dfs(k)) {
      c[i] = j;
      c[j] = i;
      return 1;
    }
  }
  return 0;
}

int func(int n) {
  int s = 0, i;

  memset(c, -1, sizeof(c));
  for (i = 0; i < n; i++) {
    if (c[i] < 0) {
      memset(f, 0, sizeof(f));
      if (dfs(i))
        s++;
    }
  }

  return s;
}

int main() {
  int n, m, l, i, j, k, w;

  while (scanf("%d%d%d", &n, &m, &l)) {
    if (!m && !n && !l)
      break;

    memset(d, -1, sizeof(d));
    for (k = 0; k < n; k++)
      d[k][k] = 0;
    for (k = 0; k < m; k++) {
      scanf("%d%d%d", &i, &j, &w);
      d[i][j] = d[j][i] = w;
    }
    for (k = 0; k < n; k++)
      for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
          if (d[i][k] != -1 && d[k][j] != -1)
            d[i][j] = d[i][k] + d[k][j] < d[i][j] ? d[i][k] + d[k][j] : d[i][j];

    memset(v, -1, sizeof(v));
    for (k = 0; k < l; k++)
      scanf("%d%d", &p[k], &t[k]);
    for (k = i = 0; i < l; i++)
      for (j = 0; j < l; j++) {
        if (i != j && d[p[i]][p[j]] != -1 && t[i] + d[p[i]][p[j]] <= t[j]) {
          e[k].v = j + l;
          e[k].next = v[i];
          v[i] = k++;
          e[k].v = i;
          e[k].next = v[j + l];
          v[j + l] = k++;
        }
      }

    printf("%d\\n", l - func(2 * l));
  }

  return 0;
}