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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define x first
#define y second
#define mod 1e9 + 7
#define f(i, a, b) for (ll i = a; i < b; i++)
#define rof(i, a, b) for (ll i = a; i > b; i--)

// int a[1001][1001] //aise global me hi

ll lcm(ll a, ll b) { return a / __gcd(a, b) * b; }
// memset can initialize to 0 or -1 ONLY
//  memset(a,0,sizeof(a[0][0])*n*n); //2D array
// memset(a, 0, sizeof(a));  //1D array
// memset(str, 't', sizeof(str)); // string
void solve() {
  int n, x, y;
  cin >> n >> x >> y;
  int a[n];
  memset(a, 0, sizeof(a));

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {

      int dist = min(abs(i - j), min(abs(i - x) + 1 + abs(y - j),
                                     abs(i - y) + 1 + abs(j - x)));

      a[dist]++;
    }
  }
  f(i, 1, n) cout << a[i] << endl;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll t = 1;
  //   cin>>t;
  while (t--) {

    solve();
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define x first
#define y second
#define mod 1e9 + 7
#define f(i, a, b) for (ll i = a; i < b; i++)
#define rof(i, a, b) for (ll i = a; i > b; i--)
//  int** a = new int*[2001];
/// for(int i = 0; i < 2001; ++i)
// a[i] = new int[2001];
int a[2001][2001];

ll lcm(ll a, ll b) { return a / __gcd(a, b) * b; }
// memset can initialize to 0 or -1 ONLY
//  memset(a,0,sizeof(a[0][0])*n*n); //2D array
// memset(a, 0, sizeof(a));  //1D array
// memset(str, 't', sizeof(str)); // string

// void dijkstra(int n,int e,int source,int *ans)
// {
// // int source;//cin>>source
// //     source=0;//0 se sabka distance?
//     int weight[n];
//     for(int i=0;i<n;i++) weight[i]=INT_MAX;
//     weight[source]=0;

//   bool *visited = new bool[n];
//     for(int i = 0; i < n; i++)
//         visited[i] = false;

//     for(int i=0;i<n;i++)
//     {
//         int minw=INT_MAX,minv=-1;
//         for(int j=0;j<n;j++)
//         {
//             if(!visited[j]&&weight[j]<=minw)
//             {
//                 minv=j;minw=weight[j];
//             }
//         }
//         visited[minv]=true;
//         for(int j=0;j<n;j++)
//         {
//             if(a[minv][j]>0&&!visited[j])
//             {
//                 if(weight[minv]+a[j][minv]<weight[j])
//                     weight[j]=weight[minv]+a[minv][j];
//             }
//         }

//     }
//     // for(int i=0;i<n;i++)
//     //     cout<<i<<" "<<weight[i]<<endl;

//     f(i,0,n)
//     ans[weight[i]]++;

// }

int minDistance(int dist[], bool sptSet[], int V) {
  // Initialize min value
  int min = INT_MAX, min_index;

  for (int v = 0; v < V; v++)
    if (sptSet[v] == false && dist[v] <= min)
      min = dist[v], min_index = v;

  return min_index;
}
void dijkstra(int n, int src, int *ans) {
  int V = n;
  int dist[V]; // The output array.  dist[i] will hold the shortest
  // distance from src to i

  bool sptSet[V]; // sptSet[i] will be true if vertex i is included in shortest
  // path tree or shortest distance from src to i is finalized

  // Initialize all distances as INFINITE and stpSet[] as false
  for (int i = 0; i < V; i++)
    dist[i] = INT_MAX, sptSet[i] = false;

  // Distance of source vertex from itself is always 0
  dist[src] = 0;

  // Find shortest path for all vertices
  for (int count = 0; count < V - 1; count++) {
    // Pick the minimum distance vertex from the set of vertices not
    // yet processed. u is always equal to src in the first iteration.
    int u = minDistance(dist, sptSet, V);

    // Mark the picked vertex as processed
    sptSet[u] = true;

    // Update dist value of the adjacent vertices of the picked vertex.
    for (int v = 0; v < V; v++)

      // Update dist[v] only if is not in sptSet, there is an edge from
      // u to v, and total weight of path from src to  v through u is
      // smaller than current value of dist[v]
      if (!sptSet[v] && a[u][v] && dist[u] != INT_MAX &&
          dist[u] + a[u][v] < dist[v])
        dist[v] = dist[u] + a[u][v];
  }

  // print the constructed distance array
  // printSolution(dist);
  f(i, 0, n) ans[dist[i]]++;
}
void solve() {
  int n, x, y;
  cin >> n >> x >> y;
  // int a[2000][2000];

  x--;
  y--;
  memset(a, 0, 2001 * 2001 * sizeof(a[0][0]));
  int ans[n];
  memset(ans, 0, sizeof(ans));
  f(i, 1, n) {
    a[i][i - 1] = 1;
    a[i - 1][i] = 1;
  }
  a[y][x] = 1;
  a[x][y] = 1;

  // cout<<"J";
  f(i, 0, n) { dijkstra(n, i, ans); }

  f(i, 1, n) cout << ans[i] / 2 << endl;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll t = 1;
  // cin>>t;
  while (t--) {
    // cout<<"hi";
    solve();
  }
}