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

#include <algorithm>
#include <climits>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <sstream>

using namespace std;

#define INF 99999

int **getDistCount(int **graph, const int V) {
  /* dist[][] will be the output matrix
  that will finally have the shortest
  distances between every pair of vertices */

  int **dist, i, j, k;
  dist = new int *[V];
  // map<int, int> res;
  for (int i = 0; i < V; i++) {
    dist[i] = new int[V];
  }

  /* Initialize the solution matrix same
  as input graph matrix. Or we can say
  the initial values of shortest distances
  are based on shortest paths considering
  no intermediate vertex. */
  for (i = 0; i < V; i++)
    for (j = 0; j < V; j++)
      dist[i][j] = graph[i][j];

  /* Add all vertices one by one to
  the set of intermediate vertices.
  ---> Before start of an iteration,
  we have shortest distances between all
  pairs of vertices such that the
  shortest distances consider only the
  vertices in set {0, 1, 2, .. k-1} as
  intermediate vertices.
  ----> After the end of an iteration,
  vertex no. k is added to the set of
  intermediate vertices and the set becomes {0, 1, 2, .. k} */
  for (k = 0; k < V; k++) {
    // Pick all vertices as source one by one
    for (i = 0; i < V; i++) {
      // Pick all vertices as destination for the
      // above picked source
      for (j = 0; j < V; 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];
        }
      }
    }
  }
  return dist;
}

int main() {

  int n = 7, a, b;
  string inp; // = "7 3 7";
  getline(cin, inp);

  stringstream ss(inp);
  ss >> n;
  ss.ignore(1);
  ss >> a;
  ss.ignore(1);
  ss >> b;
  map<int, int> out;
  int dist = 0;
  int _min = min(a, b);
  int _max = max(a, b);

  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      dist = j - i;
      dist = min(dist, abs(j - _max) + abs(_min - i) + 1);
      dist = min(dist, abs(j - _min) + abs(_max - i) + 1);
      if (out.find(dist) != out.end()) {
        out[dist]++;
      } else {
        out[dist] = 1;
      }
    }
  }

  /*for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
          cout << arr[i][j] << " ";
      }
      cout << endl;
  }*/

  for (int i = 1; i < n; i++) {
    if (out.find(i) != out.end()) {
      cout << out[i] << endl;
    } else {
      cout << 0 << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <climits>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <sstream>

using namespace std;

#define INF 99999

int **getDistCount(int **graph, const int V) {
  /* dist[][] will be the output matrix
  that will finally have the shortest
  distances between every pair of vertices */

  int **dist, i, j, k;
  dist = new int *[V];
  // map<int, int> res;
  for (int i = 0; i < V; i++) {
    dist[i] = new int[V];
  }

  /* Initialize the solution matrix same
  as input graph matrix. Or we can say
  the initial values of shortest distances
  are based on shortest paths considering
  no intermediate vertex. */
  for (i = 0; i < V; i++)
    for (j = 0; j < V; j++)
      dist[i][j] = graph[i][j];

  /* Add all vertices one by one to
  the set of intermediate vertices.
  ---> Before start of an iteration,
  we have shortest distances between all
  pairs of vertices such that the
  shortest distances consider only the
  vertices in set {0, 1, 2, .. k-1} as
  intermediate vertices.
  ----> After the end of an iteration,
  vertex no. k is added to the set of
  intermediate vertices and the set becomes {0, 1, 2, .. k} */
  for (k = 0; k < V; k++) {
    // Pick all vertices as source one by one
    for (i = 0; i < V; i++) {
      // Pick all vertices as destination for the
      // above picked source
      for (j = 0; j < V; 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];
      }
    }
  }
  return dist;
}

int main() {

  int n = 7, a, b;
  string inp;
  getline(cin, inp);

  stringstream ss(inp);
  ss >> n;
  ss.ignore(1);
  ss >> a;
  ss.ignore(1);
  ss >> b;

  int **arr;
  arr = new int *[n];
  for (int i = 0; i < n; i++) {
    arr[i] = new int[n];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j) {
        arr[i][j] = 0;
      } else if (abs(i - j) == 1) {
        arr[i][j] = 1;
        arr[j][i] = 1;
      } else {
        arr[i][j] = INF;
      }
    }
  }

  /*for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
          cout << arr[i][j] << " ";
      }
      cout << endl;
  }*/

  arr[a - 1][b - 1] = 1;
  arr[b - 1][a - 1] = 1;
  int **res = getDistCount(arr, n);

  map<int, int> out;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      // cout << res[i][j] << " ";
      if (out.find(res[i][j]) != out.end()) {
        out[res[i][j]]++;
      } else {
        out[res[i][j]] = 1;
      }
    }
    // cout << endl;
  }

  for (int i = 1; i < n; i++) {
    if (out.find(i) != out.end()) {
      cout << out[i] << endl;
    } else {
      cout << 0 << endl;
    }
  }
}