Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, X, Y;
  cin >> N >> X >> Y;
  int dp[N + 1][N + 1];
  int ans[N + 1];
  for (int i = 0; i < N + 1; i++) {
    ans[i] = 0;
    for (int j = 0; j < N + 1; j++) {
      dp[i][j] = INT_MAX / 2;
    }
  }

  for (int i = 1; i < N; i++) {
    dp[i][i + 1] = 1;
    dp[i + 1][i] = 1;
  }
  for (int i = 1; i < N + 1; i++) {
    dp[i][i] = 0;
    if (i < X) {
      dp[i][X] = X - i;
      dp[X][i] = X - i;
    }
    if (Y < i) {
      dp[i][Y] = i - Y;
      dp[Y][i] = i - Y;
    }
    if ((X < i) & (i < Y)) {
      if (i - X < Y - i) {
        dp[i][X] = i - X;
        dp[X][i] = i - X;
      } else {
        dp[i][Y] = Y - i;
        dp[Y][i] = Y - i;
      }
    }
  }
  dp[X][Y] = 1;
  dp[Y][X] = 1;
  for (int i = 1; i < N + 1; i++) {
    for (int j = 1; j < N + 1; j++) {
      if (dp[i][j] >= INT_MAX / 2) {
        dp[i][j] = min({dp[i][j - 1] + 1, dp[i][X] + dp[Y][j] + 1,
                        dp[i - 1][j] + 1, dp[j][i]});
      }
    }
  }
  for (int i = 1; i < N + 1; i++) {
    for (int j = 1; j < N + 1; j++) {
      ans[dp[i][j]]++;
    }
  }
  for (int i = 1; i < N; i++)
    cout << ans[i] / 2 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <queue>
using namespace std;
int N, X, Y;
vector<int> dijkstra(int s, vector<vector<int>> graph, int N, int X, int Y);
int main() {
  cin >> N >> X >> Y;
  vector<vector<int>> time(N + 1, vector<int>(N + 1));
  vector<int> ans(N + 1);
  for (int i = 0; i < N + 1; i++)
    ans[i] = 0;
  for (int i = 0; i < N + 1; i++) {
    for (int j = 0; j < N + 1; j++) {
      time[i][j] = 0;
    }
  }
  for (int i = 0; i < N + 1; i++)
    time[i][i] = 0;
  for (int i = 1; i < N; i++) {
    time[i][i + 1] = 1;
    time[i + 1][i] = 1;
  }
  time[X][Y] = 1;
  time[Y][X] = 1;
  vector<int> d;
  for (int i = 1; i < N + 1; i++) {
    d = dijkstra(i, time, N, X, Y);
    for (int j = 1; j < N + 1; j++)
      ans[d[j]]++;
  }
  for (int i = 1; i < N; i++)
    cout << ans[i] / 2 << endl;
  return 0;
}

vector<int> dijkstra(int s, vector<vector<int>> graph, int N, int X, int Y) {
  vector<int> d(N + 1);
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que;
  for (int i = 1; i < N + 1; i++) {
    if (i == s)
      d[i] = 0;
    else
      d[i] = 100000;
    que.push(make_pair(d[i], i));
  }

  while (!que.empty()) {
    pair<int, int> pa;
    pa = que.top();
    que.pop();
    int alt;
    if (pa.second <= N - 1) {
      if (graph[pa.second][pa.second + 1] == 1) {
        alt = pa.first + 1;
        if (d[pa.second + 1] > alt) {
          d[pa.second + 1] = alt;
          que.push(make_pair(d[pa.second + 1], pa.second + 1));
        }
      }
    }
    if (pa.second >= 2) {
      if (graph[pa.second][pa.second - 1] == 1) {
        alt = pa.first + 1;
        if (d[pa.second - 1] > alt) {
          d[pa.second - 1] = alt;
          que.push(make_pair(d[pa.second - 1], pa.second - 1));
        }
      }
    }
    if (pa.second == X) {
      alt = pa.first + 1;
      if (d[Y] > alt) {
        d[Y] = alt;
        que.push(make_pair(d[Y], Y));
      }
    }
    if (pa.second == Y) {

      alt = pa.first + 1;
      if (d[X] > alt) {
        d[X] = alt;
        que.push(make_pair(d[X], X));
      }
    }
  }
  return d;
}