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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int N, M, L;
int map[110][110];
int edge[1100][1100];

struct Node {
  int hou;
  int ti;
} node[1200];
int cx[1100];
int cy[1100];
bool vis[1100];
void floyed() {
  for (int k = 0; k < N; k++)
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++) {
        if (i != j && j != k && i != k) {
          map[i][j] = min(map[i][k] + map[k][j], map[i][j]);
        }
      }
}
int path(int u) {
  int v;
  for (v = 0; v < L; v++) {
    if (edge[u][v] && !vis[v]) {
      vis[v] = 1;
      if (cy[v] == -1 || path(cy[v])) {
        cx[u] = v;
        cy[v] = u;
        return 1;
      }
    }
  }
  return 0;
}

int main() {
  while (scanf("%d%d%d", &N, &M, &L) != EOF) {
    if (N == 0 && M == 0 && L == 0)
      break;
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++) {
        if (i == j)
          map[i][j] = 0;
        else
          map[i][j] = 0x7fffff;
      }
    for (int i = 0; i < M; i++) {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      map[a][b] = c;
      map[b][a] = c;
    }
    floyed();
    for (int i = 0; i < L; i++) {
      int a, b;
      scanf("%d%d", &a, &b);
      node[i].hou = a;
      node[i].ti = b;
    }
    memset(edge, 0, sizeof(edge));
    for (int i = 0; i < L; i++) {
      for (int j = 0; j < L; j++) {
        if (i != j &&
            node[i].ti - node[j].ti >= map[node[i].hou][node[j].hou]) {
          edge[j][i] = 1;
        }
      }
    }
    memset(cx, 0xfffff, sizeof(cx));
    memset(cy, 0xfffff, sizeof(cy));
    int ans = 0;
    for (int i = 0; i < L; i++) {
      if (cx[i] == -1) {
        memset(vis, 0, sizeof(vis));
        ans += path(i);
      }
    }
    printf("%d\\n", L - ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define INF 0x3fffffff
#define eps 1e-8
using namespace std;
int n, m, l;
int head[5005], cnt;
int dis[105][105];
int p[5005], t[5005];
int linked[5005];
bool used[5005];

struct ed {
  int next, to;
} edge[10005];

void addedge(int u, int v) {
  cnt++;
  edge[cnt].next = head[u];
  head[u] = cnt;
  edge[cnt].to = v;
}

bool dfs(int n) {
  int e, v;
  for (e = head[n]; e; e = edge[e].next) {
    v = edge[e].to;
    if (!used[v]) {
      used[v] = 1;
      if (linked[v] == -1 || dfs(linked[v])) {
        linked[v] = n;
        return true;
      }
    }
  }
  return false;
}

int solve() {
  memset(linked, -1, sizeof(linked));
  int i, res = 0;
  for (i = 0; i < l; ++i) {
    memset(used, 0, sizeof(used));
    if (dfs(i))
      res++;
  }
  return res;
}

void Floyd() {
  int i, j, k;
  for (k = 0; k < n; ++k)
    for (i = 0; i < n; ++i)
      for (j = 0; j < n; ++j) {
        if (k == i || k == j || i == j)
          continue;
        if (dis[i][j] > dis[i][k] + dis[k][j])
          dis[i][j] = dis[i][k] + dis[k][j];
      }
}

int main() {
  int u, v, i, j;
  while (scanf("%d%d%d", &n, &m, &l) && n) {
    for (u = 0; u < n; ++u)
      for (v = 0; v < n; ++v)
        dis[u][v] = dis[v][u] = INF;
    while (m--) {
      scanf("%d%d%d", &u, &v, &i);
      dis[u][v] = dis[v][u] = i;
    }
    for (i = 0; i < n; ++i)
      dis[i][i] = 0;
    Floyd();
    for (i = 0; i < l; ++i)
      scanf("%d%d", p + i, t + i);
    memset(head, 0, sizeof(head));
    cnt = 0;
    for (i = 0; i < l; ++i)
      for (j = 0; j < l; ++j)
        if (i != j && t[i] >= t[j] + dis[p[j]][p[i]]) {
          addedge(j, i + l);
          addedge(i + l, j);
        }
    printf("%d\\n", l - solve());
  }
  return 0;
}