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

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef float fl;
#define input                                                                  \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define fast() ios_base::sync_with_stdio(false), cin.tie(NULL);
#define mod 1000000007
#define pb push_back

ll n;

void dijkstra(vector<pair<ll, ll>> adj[], ll source, ll len[]) {
  ll dist[n + 1];
  ll i, x, wei, e, w;
  bool vis[n + 1] = {false};
  for (i = 0; i <= n; ++i)
    dist[i] = INT_MAX;
  dist[source] = 0;
  multiset<pair<ll, ll>> s;
  s.insert({0, source});
  pair<ll, ll> p;
  while (!s.empty()) {
    p = *s.begin();
    s.erase(s.begin());
    x = p.second;
    wei = p.first;
    if (vis[x])
      continue;
    vis[x] = true;
    for (i = 0; i < adj[x].size(); ++i) {
      e = adj[x][i].second;
      w = adj[x][i].first;
      if (dist[x] + w < dist[e]) {
        dist[e] = dist[x] + w;
        s.insert({dist[e], e});
      }
    }
  }
  // cout<<"This is from dijkstra -> ";
  // for(i=1;i<=n;++i) cout<<dist[i]<<" ";
  // cout<<endl;
  for (i = 1; i <= n; ++i) {
    len[dist[i]]++;
  }
  // cout<<"This is len vector -> ";
  // for(i=1;i<=n;++i) cout<<len[i]<<" ";
  // cout<<endl;
}

int main(void) {
  fast();
  /*ll n,k,i,x;
  cin>>k>>n;
  vector<ll> v;
  for(i=0;i<n;++i) {
      cin>>x;
      v.pb(x);
  }*/
  ll x, i, j, k, y;
  cin >> n >> x >> y;
  vector<pair<ll, ll>> adj[n + 1];
  for (i = 1; i < n; ++i) {
    adj[i].pb(make_pair(1, i + 1));
    adj[i + 1].pb(make_pair(1, i));
  }
  adj[x].pb(make_pair(1, y));
  adj[y].pb(make_pair(1, x));
  ll len[n + 1];
  for (i = 0; i <= n; ++i)
    len[i] = 0;
  for (i = 1; i <= n; ++i) {
    dijkstra(adj, i, len);
  }
  for (i = 1; i < n; ++i) {
    cout << len[i] / 2 << endl;
  }
  /*ll dist[n+1][n+1];
  ll graph[n+1][n+1];
  for(i=0;i<=n;++i) {
      for(j=0;j<=n;++j) {
          if(i==j) graph[i][j]=0;
          else if(i+1==j||i==j+1) graph[i][j]=1;
          else if((i==x&&j==y)||(i==y&&j==x)) graph[i][j]=1;
          else graph[i][j]=INT_MAX;
      }
  }
  for(i=0;i<=n;++i) {
      for(j=0;j<=n;++j)
          dist[i][j]=graph[i][j];
  }
  for (k = 1; k <=n; k++)
  {
      for (i = 1; i <=n; i++)
      {
          for (j = 1; j <=n; j++)
          {
              if (dist[i][k] + dist[k][j] < dist[i][j])
                  dist[i][j] = dist[i][k] + dist[k][j];
          }
      }
  }
  vector<ll> len;
  for(i=0;i<=n;++i) len.pb(0);
  for(i=1;i<=n;++i) {
      for(j=1;j<=n;++j) {
          len[dist[i][j]]++;
      }
  }
  for(i=1;i<n;++i) {
      cout<<len[i]/2<<endl;
  }*/
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef float fl;
#define input                                                                  \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define fast() ios_base::sync_with_stdio(false), cin.tie(NULL);
#define mod 1000000007
#define pb push_back

int main(void) {
  fast();
  /*ll n,k,i,x;
  cin>>k>>n;
  vector<ll> v;
  for(i=0;i<n;++i) {
      cin>>x;
      v.pb(x);
  }*/
  ll n, x, i, j, k, y;
  cin >> n >> x >> y;
  ll dist[n + 1][n + 1];
  ll graph[n + 1][n + 1];
  for (i = 0; i <= n; ++i) {
    for (j = 0; j <= n; ++j) {
      if (i == j)
        graph[i][j] = 0;
      else if (i + 1 == j || i == j + 1)
        graph[i][j] = 1;
      else if ((i == x && j == y) || (i == y && j == x))
        graph[i][j] = 1;
      else
        graph[i][j] = INT_MAX;
    }
  }
  for (i = 0; i <= n; ++i) {
    for (j = 0; j <= n; ++j)
      dist[i][j] = graph[i][j];
  }
  for (k = 1; k <= n; k++) {
    // Pick all vertices as source one by one
    for (i = 1; i <= n; i++) {
      // Pick all vertices as destination for the
      // above picked source
      for (j = 1; j <= n; j++) {
        // If vertex k is on the shortest path from
        // i to j, then update the value of dist[i][j]
        if (dist[i][k] + dist[k][j] < dist[i][j])
          dist[i][j] = dist[i][k] + dist[k][j];
      }
    }
  }
  vector<ll> len;
  for (i = 0; i <= n; ++i)
    len.pb(0);
  for (i = 1; i <= n; ++i) {
    for (j = 1; j <= n; ++j) {
      len[dist[i][j]]++;
    }
  }
  for (i = 1; i < n; ++i) {
    cout << len[i] / 2 << endl;
  }
  return 0;
}