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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const double PI = acos(-1.0);

class Point {
public:
  double y, x;
  Point() { y = x = 0.0; }
  Point(double y0, double x0) {
    y = y0;
    x = x0;
  }
  Point operator+(const Point &p) const { return Point(y + p.y, x + p.x); }
  Point operator-(const Point &p) const { return Point(y - p.y, x - p.x); }
  Point operator*(double a) const { return Point(y * a, x * a); }
  Point operator/(double a) const { return Point(y / a, x / a); }
  double length() const { return sqrt(y * y + x * x); }
  double dist(const Point &p) const {
    return sqrt(pow(y - p.y, 2) + pow(x - p.x, 2));
  }
  double dot(const Point &p) const {
    return y * p.y + x * p.x; // |a|*|b|*cos慮
  }
  double cross(const Point &p) const {
    return x * p.y - y * p.x; // |a|*|b|*sin慮
  }
};

int main() {
  int n;
  double r, theta;
  cin >> n >> r >> theta;
  theta *= PI / 180;

  vector<Point> p(n);
  for (int i = 0; i < n; ++i)
    cin >> p[i].x >> p[i].y;

  vector<vector<double>> dist(n, vector<double>(n));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      dist[i][j] = p[i].dist(p[j]);
    }
  }

  vector<vector<vector<bool>>> access(
      n, vector<vector<bool>>(n, vector<bool>(n, false)));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < n; ++k) {
        if (i == k)
          continue;
        double angle = acos((p[i] - p[j]).dot(p[k] - p[i]) /
                            (p[i] - p[j]).length() / (p[k] - p[i]).length());
        if (angle < theta)
          access[i][j][k] = true;
      }
    }
  }

  vector<vector<double>> dp(n, vector<double>(n, -1.0));
  for (int i = 1; i < n; ++i)
    dp[i][0] = r - dist[0][i];

  int ret = 0;
  for (;;) {
    double maxRest = -1.0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        maxRest = max(maxRest, dp[i][j]);
      }
    }
    if (maxRest < 0) {
      cout << ret << endl;
      return 0;
    }

    vector<vector<double>> next(n, vector<double>(n, -1.0));
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        for (int k = 0; k < n; ++k) {
          if (access[i][j][k])
            next[k][i] = max(next[k][i], dp[i][j] - dist[i][k]);
        }
      }
    }
    dp.swap(next);

    ++ret;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const double PI = acos(-1.0);

class Point {
public:
  double y, x;
  Point() { y = x = 0.0; }
  Point(double y0, double x0) {
    y = y0;
    x = x0;
  }
  Point operator+(const Point &p) const { return Point(y + p.y, x + p.x); }
  Point operator-(const Point &p) const { return Point(y - p.y, x - p.x); }
  Point operator*(double a) const { return Point(y * a, x * a); }
  Point operator/(double a) const { return Point(y / a, x / a); }
  double length() const { return sqrt(y * y + x * x); }
  double dist(const Point &p) const {
    return sqrt(pow(y - p.y, 2) + pow(x - p.x, 2));
  }
  double dot(const Point &p) const {
    return y * p.y + x * p.x; // |a|*|b|*cos慮
  }
  double cross(const Point &p) const {
    return x * p.y - y * p.x; // |a|*|b|*sin慮
  }
};

int main() {
  int n;
  double r, theta;
  cin >> n >> r >> theta;
  theta *= PI / 180;

  vector<Point> p(n);
  for (int i = 0; i < n; ++i)
    cin >> p[i].x >> p[i].y;

  vector<vector<double>> dp(n, vector<double>(n, -1.0));
  for (int i = 1; i < n; ++i)
    dp[i][0] = r - p[0].dist(p[i]);

  int ret = 0;
  for (;;) {
    double maxRest = -1.0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        maxRest = max(maxRest, dp[i][j]);
      }
    }
    if (maxRest < 0) {
      cout << ret << endl;
      return 0;
    }

    vector<vector<double>> next(n, vector<double>(n, -1.0));
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        for (int k = 0; k < n; ++k) {
          if (i == k)
            continue;

          double angle = acos((p[i] - p[j]).dot(p[k] - p[i]) /
                              (p[i] - p[j]).length() / (p[k] - p[i]).length());
          if (angle > theta)
            continue;

          next[k][i] = max(next[k][i], dp[i][j] - p[i].dist(p[k]));
        }
      }
    }
    dp.swap(next);

    ++ret;
  }

  return 0;
}