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

#include <iostream>
#include <queue>
#include <vector>

const int NOTPARENT = -1;
const int NOTSEARHCH = 0;
const int SEARCHING = 1;
const int SEARCHED = 2;
const int INF = static_cast<int>(1e6);

struct Edge {
  int ToNode;
  int weight;
  Edge(int t, int w) : ToNode(t), weight(w) {}
};

bool operator>(const Edge &A, const Edge &B) { return A.weight > B.weight; }

// this function calculates shortest path spanning tree by fast dijkstra's
// algorithm that uses priority queue
std::vector<int> shortestPath(int s, std::vector<std::vector<Edge>> &graph) {
  std::size_t n = graph.size();
  std::vector<int> shortestPathWeight(n, INF);
  std::vector<int> shortestPathParent(n);
  std::vector<int> nodeState(n, NOTSEARHCH);
  std::priority_queue<Edge, std::vector<Edge>, std::greater<Edge>> pq;
  shortestPathWeight[s] = 0;
  pq.push(Edge{s, 0});
  shortestPathParent[s] = NOTPARENT;

  int minWeight;
  int nextNode;
  while (!pq.empty()) {
    nextNode = pq.top().ToNode;
    minWeight = pq.top().weight;
    pq.pop();

    nodeState[nextNode] = SEARCHED;
    if (shortestPathWeight[nextNode] < minWeight)
      continue;

    for (int i = 0; i < graph[nextNode].size(); i++) {
      int adjacentNode = graph[nextNode][i].ToNode;
      int adjacentWeight = graph[nextNode][i].weight;
      if (nodeState[adjacentNode] != SEARCHED) {
        if (shortestPathWeight[nextNode] + adjacentWeight <
            shortestPathWeight[adjacentNode]) {
          shortestPathWeight[adjacentNode] =
              shortestPathWeight[nextNode] + adjacentWeight;
          nodeState[adjacentNode] = SEARCHING;
          shortestPathParent[adjacentNode] = nextNode;
          pq.push(Edge{adjacentNode, shortestPathWeight[adjacentNode]});
        }
      }
    }
  }

  return shortestPathWeight;
}

int main() {
  int n, X, Y;
  std::cin >> n >> X >> Y;
  X--;
  Y--; // to 0 origin
  // 重み付き隣接リスト
  std::vector<std::vector<Edge>> graph(n);
  for (int i = 0; i < n - 1; i++) {
    graph[i].emplace_back(i + 1, 1);
    graph[i + 1].emplace_back(i, 1);
  }
  graph[X].emplace_back(Y, 1);
  graph[Y].emplace_back(X, 1);

  std::vector<int> out(n, 0);
  for (int s = 0; s < n; s++) {
    std::vector<int> d = shortestPath(s, graph);
    for (int i = 0; i < n; i++) {
      out[d[i]]++;
    }
  }

  for (int k = 1; k < n; k++) {
    std::cout << out[k] / 2 << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using graph = std::vector<std::vector<int>>;
int n, X, Y;
const int INF = 1000000;

graph floyd(graph &g) {
  graph d(g);
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (d[i][k] == INF || d[k][j] == INF)
          continue;
        d[i][j] = std::min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }

  return d;
}

int main() {
  std::cin >> n >> X >> Y;
  X--;
  Y--; // to 0 origin
  graph g(n, std::vector<int>(n, INF));
  g[X][Y] = 1;
  g[Y][X] = 1;
  for (int i = 0; i < n - 1; i++) {
    g[i][i + 1] = 1;
    g[i + 1][i] = 1;
  }

  graph d = floyd(g);
  std::vector<int> out(n, 0);
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      out[d[i][j]]++;
    }
  }

  for (int k = 1; k < n; k++) {
    std::cout << out[k] << std::endl;
  }

  return 0;
}