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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define rep(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rREP(i, m, n) for (int(i) = (n)-1; (i) >= (m); --(i))
#define all(x) (x).begin(), (x).end()
#define out(y, x, h, w) (y) < 0 || (x) < 0 || (y) >= (h) || (x) >= (w)
constexpr int INF = 1000000001;
constexpr ll mod = 1000000007;
constexpr double PI = 3.1415926535897932;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
inline void init() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

int main() {
  init();

  int N, X, Y;
  cin >> N >> X >> Y;
  Graph G(N);
  rep(i, 0, N - 1) {
    G[i].push_back(i + 1);
    G[i + 1].push_back(i);
  }
  G[X - 1].push_back(Y - 1);
  G[Y - 1].push_back(X - 1);
  map<int, int> mp;
  rep(i, 0, N) {
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        pq;
    vector<int> vec(N, INF);
    vec[i] = 0;
    pq.push(make_pair(0, i));
    while (!pq.empty()) {
      auto p = pq.top();
      pq.pop();
      int v = p.second;
      if (vec[v] < p.first)
        continue;
      for (auto to : G[v]) {
        if (chmin(vec[to], vec[v] + 1)) {
          pq.push(make_pair(vec[to], to));
        }
      }
    }
    rep(i, 0, N) { mp[vec[i]]++; }
  }
  rep(i, 1, N) { cout << mp[i] / 2 << "\\n"; }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define rep(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rREP(i, m, n) for (int(i) = (n)-1; (i) >= (m); --(i))
#define all(x) (x).begin(), (x).end()
#define out(y, x, h, w) (y) < 0 || (x) < 0 || (y) >= (h) || (x) >= (w)
constexpr int INF = 1000000001;
constexpr ll mod = 1000000007;
constexpr double PI = 3.1415926535897932;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
inline void init() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

int main() {
  init();

  int N, X, Y;
  cin >> N >> X >> Y;
  Graph G(N, vector<int>(N, INF));
  rep(i, 0, N - 1) {
    G[i][i] = 0;
    G[i][i + 1] = 1;
    G[i + 1][i] = 1;
  }
  G[X - 1][Y - 1] = 1;
  G[Y - 1][X - 1] = 1;

  map<int, int> mp;
  rep(i, 0, N) {
    vector<int> d(N, INF);
    vector<bool> used(N, false);
    d[i] = 0;
    while (1) {
      int v = -1;
      rep(u, 0, N) {
        if (!used[u] && (v == -1 || d[u] < d[v]))
          v = u;
      }
      if (v == -1)
        break;
      used[v] = true;
      rep(u, 0, N) d[u] = min(d[u], d[v] + G[v][u]);
    }
    rep(j, 0, N) { mp[d[j]]++; }
  }

  rep(i, 1, N) { cout << mp[i] / 2 << "\\n"; }

  return 0;
}