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

#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <string.h>

using namespace std;

const int maxn = 1010;
const int inf = 0x3f3f3f3f;

int dis[maxn][maxn];
int n, m, L;

struct node {
  int s, t;
  node(int _s, int _t) : s(_s), t(_t) {}
  node(){};
  bool operator<(const node &o) const { return t < o.t; }
} q[maxn];

int nn;
bool e[maxn][maxn];
void build() {
  memset(e, 0, sizeof(e));
  nn = L;
  for (int i = 1; i <= nn; i++)
    for (int j = i + 1; j <= nn; j++) {
      int d1 = q[j].t - q[i].t;
      int d2 = dis[q[j].s][q[i].s];
      if (d2 <= d1) {
        e[i][j] = 1;
        // printf("%d %d\\n",i,j);
      }
    }
  for (int k = 1; k <= nn; k++)
    for (int i = 1; i < k; i++)
      for (int j = k + 1; j <= nn; j++)
        e[i][j] = (e[i][j] | (e[i][k] & e[k][j]));
  return;
}

int vis[maxn];
int cx[maxn];
int cy[maxn];
int find(int u) {
  for (int v = u; v <= nn; v++) {
    if (e[u][v] && !vis[v]) {
      vis[v] = 1;
      if (cy[v] == -1 || find(cy[v])) {
        cx[u] = v;
        cy[v] = u;
        return true;
      }
    }
  }
  return false;
}
int solve() {
  memset(vis, 0, sizeof(vis));
  memset(cx, -1, sizeof(cx));
  memset(cy, -1, sizeof(cy));
  int res = 0;
  for (int i = 1; i <= nn; i++) {
    if (cx[i] == -1) {
      memset(vis, 0, sizeof(vis));
      if (find(i))
        res++;
    }
  }
  return res;
}
int main() {
  while (scanf("%d%d%d", &n, &m, &L) == 3) {
    if (n == 0 && m == 0 && L == 0)
      break;
    memset(dis, inf, sizeof(dis));
    for (int i = 1; i <= m; i++) {
      int u, v, d;
      scanf("%d%d%d", &u, &v, &d);
      u++;
      v++;
      if (d < dis[u][v]) {
        dis[u][v] = d;
        dis[v][u] = d;
      }
    }

    for (int k = 1; k <= n; k++)
      for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
          dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
    for (int i = 1; i <= n; i++)
      dis[i][i] = 0;

    for (int i = 1; i <= L; i++) {
      int s, t;
      scanf("%d%d", &s, &t);
      s++;
      q[i] = node(s, t);
    }
    sort(q + 1, q + 1 + L);
    build();
    int res = solve();
    int ans = L - res;

    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>

const int maxn = 105;
const int maxm = 1005;
const int inf = 0x7fffffff;

struct Query {
  int v, time;
  bool operator<(const Query p) const { return time < p.time; }
  void input() { scanf("%d %d", &v, &time); }
};

struct Match {
  bool vis[maxn];
  int net[maxn], match[maxn], size, n;
  struct EDGE {
    int v, next;
    EDGE() {}
    EDGE(int a, int b) : v(a), next(b) {}
  } E[maxm << 4];
  void init(int _n) {
    n = _n, size = 0;
    memset(net, -1, sizeof(net));
  }
  void add_edge(int u, int v) {
    E[size] = EDGE(v, net[u]);
    net[u] = size++;
  }
  int dfs(int u) {
    int v;
    for (int i = net[u]; v = E[i].v, i != -1; i = E[i].next)
      if (!vis[v]) {
        vis[v] = true;
        if (match[v] == -1 || dfs(match[v])) {
          match[v] = u;
          return 1;
        }
      }
    return 0;
  }
  int hungary() {
    int ans = 0;
    memset(match, -1, sizeof(match));
    for (int i = 0; i < n; i++) {
      memset(vis, false, sizeof(vis));
      ans += dfs(i);
    }
    return ans;
  }
};

Match G;
Query query[maxm];
int map[maxn][maxn], n;

void floyd() {
  for (int k = 0; k < n; k++)
    for (int i = 0; i < n; i++) {
      if (map[i][k] == inf)
        continue;
      for (int j = 0; j < n; j++) {
        if (map[k][j] == inf)
          continue;
        map[i][j] = std::min(map[i][j], map[i][k] + map[k][j]);
      }
    }
}

int main() {
  int m, l, a, b, c;

  while (scanf("%d %d %d", &n, &m, &l) == 3) {
    if (!n && !m && !l)
      break;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        map[i][j] = (i == j) ? 0 : inf;
    for (int i = 0; i < m; i++) {
      scanf("%d %d %d", &a, &b, &c);
      if (a != b)
        map[a][b] = map[b][a] = c;
    }
    floyd();
    for (int i = 0; i < l; i++)
      query[i].input();
    std::sort(query, query + l);
    G.init(l);
    for (int i = 0; i < l; i++)
      for (int j = i + 1; j < l; j++)
        if (map[query[i].v][query[j].v] <= query[j].time - query[i].time)
          G.add_edge(i, j);
    printf("%d\\n", l - G.hungary());
  }
  return 0;
}