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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;
const int maxn = 4e3 + 10;
int cnt = 0, head[maxn];
int dst[maxn], vis[maxn] = {0};
int n;
struct Edge {
  int val, to, next;
} edge[maxn];
struct node {
  int dst, to;
  bool operator<(const node &b) const { return dst > b.dst; }
} p[maxn];
void add_edge(int from, int to, int val) {
  cnt++;
  edge[cnt].to = to;
  edge[cnt].val = val;
  edge[cnt].next = head[from];
  head[from] = cnt;
}
void dijkstra(int s) {
  //	memset(dst,inf,sizeof dst);
  //	memset(vis,0,sizeof vis);
  for (int i = 0; i <= n; i++) {
    vis[i] = 0;
    dst[i] = inf;
  }
  priority_queue<node> q;
  q.push((node){0, s});
  while (!q.empty()) {
    node on = q.top();
    q.pop();
    if (vis[on.to])
      continue;
    dst[on.to] = on.dst;
    vis[on.to] = 1;
    for (int i = head[on.to]; i; i = edge[i].next) {
      if (dst[edge[i].to] > dst[on.to] + edge[i].val) {
        dst[edge[i].to] = dst[on.to] + edge[i].val;
        if (vis[edge[i].to] == 0)
          q.push((node){dst[edge[i].to], edge[i].to});
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int m, s, x, y;
  memset(head, -1, sizeof head);
  cin >> n >> x >> y;
  for (int i = 1; i < n; i++) {
    add_edge(i, i + 1, 1);
    add_edge(i + 1, i, 1);
  }
  add_edge(x, y, 1);
  add_edge(y, x, 1);
  int len[2020][2020];
  for (int i = 1; i <= n; i++) {
    dijkstra(i);
    for (int j = 1; j <= n; j++) {
      // 		cout<<dst[j]<<' ';
      if (len[i][j] == 0)
        len[i][j] = dst[j];
      else
        len[i][j] = min(len[i][j], dst[j]);
    }
  }
  int ans[maxn];
  for (int i = 1; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      ans[len[i][j]]++;
    }
  }
  for (int i = 1; i < n; i++)
    cout << ans[i] << ' ';
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;
const int maxn = 4e3 + 10;
int cnt = 0, head[maxn];
int dst[maxn], vis[maxn] = {0};
int n;
struct Edge {
  int val, to, next;
} edge[maxn];
struct node {
  int dst, to;
  bool operator<(const node &b) const { return dst > b.dst; }
} p[maxn];
void add_edge(int from, int to, int val) {
  cnt++;
  edge[cnt].to = to;
  edge[cnt].val = val;
  edge[cnt].next = head[from];
  head[from] = cnt;
}
void dijkstra(int s) {
  //	memset(dst,inf,sizeof dst);
  //	memset(vis,0,sizeof vis);
  for (int i = 0; i <= n; i++) {
    vis[i] = 0;
    dst[i] = inf;
  }
  priority_queue<node> q;
  q.push((node){0, s});
  while (!q.empty()) {
    node on = q.top();
    q.pop();
    if (vis[on.to])
      continue;
    dst[on.to] = on.dst;
    vis[on.to] = 1;
    for (int i = head[on.to]; i; i = edge[i].next) {
      if (dst[edge[i].to] > dst[on.to] + edge[i].val) {
        dst[edge[i].to] = dst[on.to] + edge[i].val;
        if (vis[edge[i].to] == 0)
          q.push((node){dst[edge[i].to], edge[i].to});
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int m, s, x, y;
  memset(head, -1, sizeof head);
  cin >> n >> x >> y;
  for (int i = 1; i < n; i++) {
    add_edge(i, i + 1, 1);
    add_edge(i + 1, i, 1);
  }
  add_edge(x, y, 1);
  add_edge(y, x, 1);
  int len[2000][20000];
  for (int i = 1; i <= n; i++) {
    dijkstra(i);
    for (int j = 1; j <= n; j++) {
      // 		cout<<dst[j]<<' ';
      if (len[i][j] == 0)
        len[i][j] = dst[j];
      else
        len[i][j] = min(len[i][j], dst[j]);
    }
  }
  int ans[maxn];
  for (int i = 1; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      ans[len[i][j]]++;
    }
  }
  for (int i = 1; i < n; i++)
    cout << ans[i] << ' ';
}