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

#include <deque>
#include <iostream>
#include <vector>

class graph_list {
private:
  int origin;
  int size;
  int initial_point;
  std::deque<std::pair<int, int>> deq;

public:
  std::vector<std::vector<std::pair<int, int>>> list;
  std::vector<std::vector<int>> dist;

  graph_list(int origin_in, int N) {
    origin = origin_in;
    size = N;
    list.resize(size);
    dist.assign(size, std::vector<int>(size, 0x7FFFFFFF));
    for (int i = 0; i < size; i++) {
      dist[i][i] = 0;
    }
  }

  void connect(int a, int b, int cost) {
    std::pair<int, int> tmp(b - origin, cost);
    list[a - origin].push_back(tmp);
    std::pair<int, int> tmp2(a - origin, cost);
    list[b - origin].push_back(tmp2);
  }

  void dist_calc(int point) {
    initial_point = point;
    std::pair<int, int> tmp(initial_point, 0);
    deq.push_back(tmp);
    while (!deq.empty()) {
      std::pair<int, int> tmp = deq[0];
      sub_dist_calc(tmp.first, tmp.second);
      deq.pop_front();
    }
  }

  void sub_dist_calc(int point, int cost) {
    for (int i = 0; i < list[point - origin].size(); i++) {

      int move_cost = cost + list[point - origin][i].second;
      if (dist[initial_point - origin][list[point - origin][i].first] <
          move_cost) {
        continue;
      } else {
        dist[initial_point - origin][list[point - origin][i].first] = move_cost;
        dist[list[point - origin][i].first][initial_point - origin] = move_cost;
        std::pair<int, int> tmp(list[point - origin][i].first + origin,
                                move_cost);
        deq.push_back(tmp);
      }
    }
  }
};

int main() {
  int N, X, Y;
  std::cin >> N >> X >> Y;

  graph_list G(1, N);
  for (int i = 1; i <= N - 1; i++) {
    G.connect(i, i + 1, 1);
  }
  G.connect(X, Y, 1);

  for (int i = 1; i <= N; i++) {
    G.dist_calc(i);
  }

  std::vector<int> ans(N - 1, 0);
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      if (G.dist[i][j] > 0) {
        ans[G.dist[i][j] - 1]++;
      }
    }
  }
  for (int i = 0; i < N - 1; i++) {
    std::cout << ans[i] << std::endl;
  }
}


Error: TLE

Fixed submission:

#include <deque>
#include <iostream>
#include <vector>

class graph_matrix {
private:
  int origin;
  int size;
  int initial_point;
  std::deque<std::pair<int, int>> deq;

public:
  std::vector<std::vector<int>> graph;
  std::vector<std::vector<int>> dist;

  graph_matrix(int origin_in, int N) {
    origin = origin_in;
    size = N;
    graph.assign(size, std::vector<int>(size, -1));
    dist.assign(size, std::vector<int>(size, 0x7FFFFFFF));
    for (int i = 0; i < size; i++) {
      graph[i][i] = 0;
      dist[i][i] = 0;
    }
  }

  void connect(int a, int b, int cost) {
    graph[a - origin][b - origin] = cost;
    graph[b - origin][a - origin] = cost;
  }

  void dist_calc(int point) {
    initial_point = point;
    std::pair<int, int> tmp(initial_point, 0);
    deq.push_back(tmp);
    while (!deq.empty()) {
      std::pair<int, int> tmp = deq[0];
      sub_dist_calc(tmp.first, tmp.second);
      deq.pop_front();
    }
  }

  void sub_dist_calc(int point, int cost) {
    for (int i = 0; i < size; i++) {
      if (point - origin == i) {
        continue;
      }
      if (graph[point - origin][i] == -1) {
        continue;
      }

      int move_cost = cost + graph[point - origin][i];
      if (dist[initial_point - origin][i] < move_cost) {
        continue;
      } else {
        dist[initial_point - origin][i] = move_cost;
        dist[i][initial_point - origin] = move_cost;
        std::pair<int, int> tmp(i + origin, move_cost);
        deq.push_back(tmp);
      }
    }
  }
};

int main() {
  int N, X, Y;
  std::cin >> N >> X >> Y;

  graph_matrix G(1, N);
  for (int i = 1; i <= N - 1; i++) {
    G.connect(i, i + 1, 1);
  }
  G.connect(X, Y, 1);

  for (int i = 1; i <= N; i++) {
    G.dist_calc(i);
  }

  std::vector<int> ans(N - 1, 0);
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      if (G.dist[i][j] > 0) {
        ans[G.dist[i][j] - 1]++;
      }
    }
  }
  for (int i = 0; i < N - 1; i++) {
    std::cout << ans[i] << std::endl;
  }
}