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

#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int beg[5005];
int ans[5005];
int number;
int t, n;
bool vis[5003];
int dis[5003];
int x, y;
struct cmp {
  template <typename T> bool operator()(T const &left, T const &right) {
    if (left.second > right.second)
      return true;
    else
      return false;
  }
};
struct edge {
  int to;
  int Next;
  int w;
} ljb[5005];
void add(int from, int to, int w) {
  number++;
  ljb[number].to = to;
  ljb[number].w = w;
  ljb[number].Next = beg[from];
  beg[from] = number;
}
void dij(int x) {
  memset(dis, 0x7f, sizeof(dis));
  memset(vis, 0, sizeof(vis));
  int res, pos;
  dis[x] = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> q;
  q.push(make_pair(x, 0));
  while (!q.empty()) {
    pair<int, int> res;
    res = q.top();
    q.pop();
    int from = res.first;
    vis[from] = 1;
    for (int j = beg[from]; j != -1; j = ljb[j].Next) {
      int to = ljb[j].to;
      if (!vis[to] && dis[to] > dis[from] + ljb[j].w) {
        dis[to] = dis[from] + ljb[j].w;
        q.push(make_pair(to, dis[to]));
      }
    }
  }
}
int main() {
  memset(beg, -1, sizeof(beg));
  cin >> n >> x >> y;
  for (int i = 2; i <= n; i++) {
    add(i, i - 1, 1);
    add(i - 1, i, 1);
  }
  add(x, y, 1);
  add(y, x, 1);
  for (int i = 1; i <= n; i++) {
    dij(i);
    for (int j = 1; j <= n; j++) {
      if (i == j)
        continue;
      else
        ans[dis[j]]++;
    }
  }
  for (int i = 1; i <= n - 1; i++)
    cout << ans[i] / 2 << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int beg[2005];
int ans[2005];
int number;
int t, n;
bool vis[2003];
int dis[2003];
int x, y;
struct cmp {
  template <typename T> bool operator()(T const &left, T const &right) {
    if (left.second > right.second)
      return true;
    else
      return false;
  }
};
struct edge {
  int to;
  int Next;
  int w;
} ljb[2005];
void add(int from, int to, int w) {
  number++;
  ljb[number].to = to;
  ljb[number].w = w;
  ljb[number].Next = beg[from];
  beg[from] = number;
}
void dij(int x) {
  memset(dis, 0x7f, sizeof(dis));
  memset(vis, 0, sizeof(vis));
  int res, pos;
  dis[x] = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> q;
  q.push(make_pair(x, 0));
  while (!q.empty()) {
    pair<int, int> res;
    res = q.top();
    q.pop();
    int from = res.first;
    vis[from] = 1;
    for (int j = beg[from]; j != -1; j = ljb[j].Next) {
      int to = ljb[j].to;
      if (!vis[to] && dis[to] > dis[from] + ljb[j].w) {
        dis[to] = dis[from] + ljb[j].w;
        q.push(make_pair(to, dis[to]));
      }
    }
  }
}
int main() {
  memset(beg, -1, sizeof(beg));
  cin >> n >> x >> y;
  for (int i = 2; i <= n; i++) {
    add(i, i - 1, 1);
    add(i - 1, i, 1);
  }
  add(x, y, 1);
  add(y, x, 1);
  for (int i = 1; i <= n; i++) {
    dij(i);
    for (int j = 1; j <= n; j++) {
      if (i == j)
        continue;
      else
        ans[dis[j]]++;
    }
  }
  for (int i = 1; i <= n - 1; i++)
    cout << ans[i] / 2 << endl;
  return 0;
}