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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define ll long long
using namespace std;
// typedef vector<unsigned int>vec;
// typedef vector<ll>vec;
// typedef vector<vec> mat;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INF = 1000000000;
const ll LINF = 1000000000000000000; // 1e18
const ll MOD = 1000000007;
const double PI = acos(-1.0);
const double EPS = 1e-10;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
// template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};

template <class T> class Dijkstra {
public:
  int N;
  T inf;
  vector<T> cost;
  vector<vector<pair<T, int>>> edge;

  Dijkstra(const int N, T inf) : N(N), inf(inf), cost(N), edge(N) {}

  void make_edge(int from, int to, T w) { edge[from].push_back({w, to}); }

  void solve(int start) {
    for (int i = 0; i < N; ++i)
      cost[i] = inf;

    priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>>
        pq;
    cost[start] = 0;
    pq.push({0, start});

    while (!pq.empty()) {
      T v = pq.top().first;
      int from = pq.top().second;
      pq.pop();
      for (auto u : edge[from]) {
        T w = v + u.first;
        int to = u.second;
        if (w < cost[to]) {
          cost[to] = w;
          pq.push({w, to});
        }
      }
    }
    return;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, X, Y;
  cin >> N >> X >> Y;
  --X, --Y;
  Dijkstra<int> d(N, INF);
  d.make_edge(X, Y, 1);
  d.make_edge(Y, X, 1);
  REP(i, N - 1) {
    d.make_edge(i, i + 1, 1);
    d.make_edge(i + 1, i, 1);
  }
  vector<int> ans(N);
  REP(i, N - 1) {
    d.solve(i);
    for (int j = i + 1; j < N; j++)
      ans[d.cost[j]]++;
  }
  for (int i = 1; i < N; i++)
    cout << ans[i] << endl;
}


Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

#define INF 1e+9
#define MAX_V 2020

struct edge {
  int to;
  int cost;
};

// <最短距離, 頂点の番号>
using P = pair<int, int>;

int V;
vector<edge> G[MAX_V];
int d[MAX_V];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;

    for (int i = 0; i < G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int x, y;
  cin >> V >> x >> y;
  for (int i = 0; i < V - 1; ++i) {
    edge e = {i + 1, 1};
    G[i].push_back(e);
    e = {i, 1};
    G[i + 1].push_back(e);
  }
  edge e = {x - 1, 1};
  G[y - 1].push_back(e);
  e = {y - 1, 1};
  G[x - 1].push_back(e);

  vector<int> cnt(V);
  for (int i = 0; i < V; i++) {
    dijkstra(i);
    for (int j = i + 1; j < V; j++) {
      cnt[d[j]]++;
    }
  }

  for (int i = 1; i < V; i++) {
    cout << cnt[i] << endl;
  }
}