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

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
const long double PI = 3.14159255358979323846264338327950288l;
struct Point {
  long double px, py;
};
long double theta(long double a, long double b, long double c) {
  long double s = (a + b + c) / 2.0l;
  long double S = sqrtl(s * (s - a) * (s - b) * (s - c));
  long double A = -1000, B = -1000, C = -1000, H;

  if (a > b && a > c) {
    H = 2.0l * S / a;
    B = asin(H / c) * 180.0l / PI;
    C = asin(H / b) * 180.0l / PI;
    A = 180.0l - B - C;
  } else if (b > a && b > c) {
    H = 2.0l * S / b;
    A = asin(H / c) * 180.0l / PI;
    C = asin(H / a) * 180.0l / PI;
    B = 180.0l - A - C;
  } else {
    H = 2.0l * S / c;
    A = asin(H / b) * 180.0l / PI;
    B = asin(H / a) * 180.0l / PI;
    C = 180.0l - A - B;
  }

  return A;
}
long double theta_(Point a, Point b, Point c) {
  // a->b->c
  long double A1 =
      sqrtl((a.px - c.px) * (a.px - c.px) + (a.py - c.py) * (a.py - c.py));
  long double B1 =
      sqrtl((a.px - b.px) * (a.px - b.px) + (a.py - b.py) * (a.py - b.py));
  long double C1 =
      sqrtl((b.px - c.px) * (b.px - c.px) + (b.py - c.py) * (b.py - c.py));
  return theta(A1, B1, C1);
}
long double dp[22][22], prev_[22][22], r, t, theta__[22][22][22], dist_[22][22];
Point x[30];
int n;
int main() {
  cin >> n >> r >> t;
  for (int i = 0; i < n; i++)
    cin >> x[i].px >> x[i].py;
  for (int i = 0; i < 22; i++) {
    for (int j = 0; j < 22; j++) {
      dp[i][j] = 999999999.9l;
      prev_[i][j] = 999999999.9l;
    }
  }
  for (int i = 0; i < n; i++) {
    if (i != 0) {
      prev_[0][i] = sqrtl((x[0].px - x[i].px) * (x[0].px - x[i].px) +
                          (x[0].py - x[i].py) * (x[0].py - x[i].py));
    }
  }
  for (int i = 0; i < 22; i++) {
    for (int j = 0; j < 22; j++) {
      for (int k = 0; k < 22; k++) {
        theta__[i][j][k] = 180.0l - theta_(x[i], x[j], x[k]);
      }
    }
  }
  for (int i = 0; i < 22; i++) {
    for (int j = 0; j < 22; j++) {
      dist_[i][j] = sqrtl((x[i].px - x[j].px) * (x[i].px - x[j].px) +
                          (x[i].py - x[j].py) * (x[i].py - x[j].py));
    }
  }
  int maxn = 0;
  for (int i = 1; i < 100000; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        for (int l = 0; l < n; l++) {
          if (j == k || k == l || theta__[j][k][l] > t)
            continue;
          dp[k][l] = min(dp[k][l], prev_[j][k] + dist_[k][l]);
        }
      }
    }
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        if (prev_[j][k] < r)
          maxn = i;
        prev_[j][k] = dp[j][k];
        dp[j][k] = 999999999.9l;
      }
    }
    if (maxn != i)
      break;
  }
  cout << maxn << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
const long double PI = 3.14159255358979323846264338327950288l;
struct Point {
  long double px, py;
};
long double theta(long double a, long double b, long double c) {
  long double s = (a + b + c) / 2.0l;
  long double S = sqrtl(s * (s - a) * (s - b) * (s - c));
  long double A = -1000, B = -1000, C = -1000, H;

  if (a > b && a > c) {
    H = 2.0l * S / a;
    B = asin(H / c) * 180.0l / PI;
    C = asin(H / b) * 180.0l / PI;
    A = 180.0l - B - C;
  } else if (b > a && b > c) {
    H = 2.0l * S / b;
    A = asin(H / c) * 180.0l / PI;
    C = asin(H / a) * 180.0l / PI;
    B = 180.0l - A - C;
  } else {
    H = 2.0l * S / c;
    A = asin(H / b) * 180.0l / PI;
    B = asin(H / a) * 180.0l / PI;
    C = 180.0l - A - B;
  }

  return A;
}
long double theta_(Point a, Point b, Point c) {
  // a->b->c
  long double A1 =
      sqrtl((a.px - c.px) * (a.px - c.px) + (a.py - c.py) * (a.py - c.py));
  long double B1 =
      sqrtl((a.px - b.px) * (a.px - b.px) + (a.py - b.py) * (a.py - b.py));
  long double C1 =
      sqrtl((b.px - c.px) * (b.px - c.px) + (b.py - c.py) * (b.py - c.py));
  return theta(A1, B1, C1);
}
long double dp[22][22], prev_[22][22], r, t;
Point x[30];
int n;
int main() {
  cin >> n >> r >> t;
  for (int i = 0; i < n; i++)
    cin >> x[i].px >> x[i].py;
  for (int i = 0; i < 22; i++) {
    for (int j = 0; j < 22; j++) {
      dp[i][j] = 999999999.9l;
      prev_[i][j] = 999999999.9l;
    }
  }
  for (int i = 0; i < n; i++) {
    if (i != 0) {
      prev_[0][i] = sqrtl((x[0].px - x[i].px) * (x[0].px - x[i].px) +
                          (x[0].py - x[i].py) * (x[0].py - x[i].py));
    }
  }
  int maxn = 0;
  for (int i = 1; i < 100000; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        for (int l = 0; l < n; l++) {
          if (j == k || k == l)
            continue;
          long double T = theta_(x[j], x[k], x[l]);
          T = 180.0l - T;
          long double dst = sqrtl((x[k].px - x[l].px) * (x[k].px - x[l].px) +
                                  (x[k].py - x[l].py) * (x[k].py - x[l].py));
          if (T >= t)
            continue;
          dp[k][l] = min(dp[k][l], prev_[j][k] + dst);
        }
      }
    }
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        if (prev_[j][k] < r)
          maxn = i;
        prev_[j][k] = dp[j][k];
        dp[j][k] = 999999999.9l;
      }
    }
    if (maxn != i)
      break;
  }
  cout << maxn << endl;
  return 0;
}