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

#include "bits/stdc++.h"

using namespace std;

int N, M, K, L, R, H, W;

const long long int MOD = 1000000007;
const float EPS = 1e-8;

float dis(float a, float c, float b, float d) {
  return sqrt((a - b) * (a - b) + (c - d) * (c - d));
}

float dp[1 << 20][20];

int main() {

  int N;
  float sx, sy, gx, gy;
  while (cin >> N >> sx >> sy >> gx >> gy, N) {
    vector<float> y(N);
    vector<float> x(N);
    vector<float> devil(N);
    bool f = true;
    for (int i = 0; i < N; i++) {
      cin >> x[i] >> y[i];
      devil[i] = dis(gx, gy, x[i], y[i]);
      if (devil[i] < dis(sx, sy, x[i], y[i])) {
        f = false;
      }
    }
    if (!f) {
      cout << "NO\\n";
      continue;
    }
    vector<vector<float>> dist(N, vector<float>(N));
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        dist[i][j] = dis(x[i], y[i], x[j], y[j]);
      }
    }
    for (int i = 0; i < 1 << N; i++) {
      for (int j = 0; j < N; j++) {
        dp[i][j] = MOD;
      }
    }
    for (int i = 0; i < N; i++) {
      if (devil[i] - EPS > dis(sx, sy, x[i], y[i])) {
        dp[1 << i][i] = dis(sx, sy, x[i], y[i]);
      }
    }
    for (int i = 1; i < 1 << N; i++) {
      for (int j = 0; j < N; j++) {
        if (!((i >> j) & 1))
          continue;
        if (dp[i][j] >= 1000000)
          continue;
        bool hoge = false;
        for (int k = 0; k < N; k++) {
          if ((i >> k) & 1)
            continue;
          if (dp[i][j] + dist[j][k] > devil[k] + EPS)
            hoge = true;
        }

        if (hoge)
          continue;
        for (int k = 0; k < N; k++) {
          if ((i >> k) & 1)
            continue;
          float nx = dp[i][j] + dist[j][k];
          if (nx < devil[k] - EPS) {
            dp[i | (1 << k)][k] = min(dp[i | (1 << k)][k], nx);
          }
        }
      }
    }

    float ans = MOD;
    for (int i = 0; i < N; i++) {
      ans = min(ans, dp[(1 << N) - 1][i]);
    }
    if (ans >= 10000000) {
      cout << "NO\\n";
    } else {
      cout << "YES\\n";
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

int N, M, K, L, R, H, W;

const long long int MOD = 1000000007;
const float EPS = 1e-8;

float dis(float a, float c, float b, float d) {
  return sqrt((a - b) * (a - b) + (c - d) * (c - d));
}

float dp[1 << 20][20];

int main() {

  int N;
  float sx, sy, gx, gy;
  while (cin >> N >> sx >> sy >> gx >> gy, N) {
    vector<float> y(N);
    vector<float> x(N);
    vector<float> devil(N);
    bool f = true;
    for (int i = 0; i < N; i++) {
      cin >> x[i] >> y[i];
      devil[i] = dis(gx, gy, x[i], y[i]);
      if (devil[i] < dis(sx, sy, x[i], y[i])) {
        f = false;
      }
    }
    if (!f) {
      cout << "NO\\n";
      continue;
    }
    vector<vector<float>> dist(N, vector<float>(N));
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        dist[i][j] = dis(x[i], y[i], x[j], y[j]);
      }
    }
    for (int i = 0; i < 1 << N; i++) {
      for (int j = 0; j < N; j++) {
        dp[i][j] = MOD;
      }
    }
    for (int i = 0; i < N; i++) {
      if (devil[i] - EPS > dis(sx, sy, x[i], y[i])) {
        dp[1 << i][i] = dis(sx, sy, x[i], y[i]);
      }
    }
    for (int i = 1; i < 1 << N; i++) {
      for (int j = 0; j < N; j++) {
        if (!((i >> j) & 1))
          continue;
        if (dp[i][j] >= 1000000)
          continue;
        for (int k = 0; k < N; k++) {
          if ((i >> k) & 1)
            continue;
          float nx = dp[i][j] + dist[j][k];
          if (nx < devil[k] - EPS) {
            dp[i | (1 << k)][k] = min(dp[i | (1 << k)][k], nx);
          }
        }
      }
    }

    float ans = MOD;
    for (int i = 0; i < N; i++) {
      ans = min(ans, dp[(1 << N) - 1][i]);
    }
    if (ans >= 10000000) {
      cout << "NO\\n";
    } else {
      cout << "YES\\n";
    }
  }

  return 0;
}