alexjercan/bugnet huggingface dataset:
Problem p01133 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;

int n, dx, dy, hx, hy;
bool done[20];
int cx[20], cy[20];

double calc(int x1, int y1, int x2, int y2) {
  return sqrt((double)(x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

bool dfs(int x, int y, double time, int k) {
  if (time >= calc(x, y, dx, dy))
    return false;
  if (k == n)
    return true;

  for (int i = 0; i < n; i++) {
    if (done[i])
      continue;
    if (time + calc(x, y, cx[i], cy[i]) >= calc(cx[i], cy[i], dx, dy))
      return false;
  }

  for (int i = 0; i < n; i++) {
    if (done[i])
      continue;
    if (dfs(cx[i], cy[i], time + calc(cx[i], cy[i], x, y), k + 1))
      return true;
  }
  return false;
}

int main() {
  while (cin >> n >> hx >> hy >> dx >> dy && n != 0) {
    for (int i = 0; i < n; i++)
      cin >> cx[i] >> cy[i];
    memset(done, false, sizeof(done));
    if (dfs(hx, hy, 0.0, 0))
      cout << "YES\\n";
    else
      cout << "NO\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;

int n, dx, dy, hx, hy;
bool done[20];
int cx[20], cy[20];

double calc(int x1, int y1, int x2, int y2) {
  return sqrt((double)(x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

bool dfs(int x, int y, int now, double time, int k) {
  //	cout<<now<<" ";
  //	cout<<time<<endl;
  if (now >= 0 && calc(cx[now], cy[now], dx, dy) <= time)
    return false;
  if (k == n)
    return true;

  for (int i = 0; i < n; i++) {
    if (done[i])
      true;
    if (time + calc(cx[i], cy[i], x, y) + 0.000001 >=
        calc(cx[i], cy[i], dx, dy))
      return false;
  }

  for (int i = 0; i < n; i++) {
    if (done[i])
      continue;
    done[i] = true;
    if (dfs(cx[i], cy[i], i, time + calc(cx[i], cy[i], x, y), k + 1))
      return true;
    done[i] = false;
  }
  return false;
}

int main() {
  while (cin >> n >> hx >> hy >> dx >> dy && n != 0) {
    for (int i = 0; i < n; i++)
      cin >> cx[i] >> cy[i];
    memset(done, false, sizeof(done));
    if (dfs(hx, hy, -1, 0.0, 0))
      cout << "YES\\n";
    else
      cout << "NO\\n";
  }
  return 0;
}
Problem p01133 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long LL;
static const double EPS = 1e-9;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
bool used[21];
int n, hx, hy, dx, dy;
vector<int> cx(21), cy(21);
vector<double> dist(21);
double pointdistance(int x1, int y1, int x2, int y2) {
  return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int dfs(int x, int y, int t, double s) {
  if (t == n)
    return true;
  double nexts;
  REP(i, n) {
    if (!used[i]) {
      nexts = s + pointdistance(x, y, cx[i], cy[i]);
      if (nexts > dist[i] - EPS)
        return false;
    }
  }
  REP(i, n) {
    if (!used[i]) {
      nexts = s + pointdistance(x, y, cx[i], cy[i]);
      used[i] = true;
      if (dfs(cx[i], cy[i], t + 1, nexts))
        return true;
      used[i] = false;
    }
  }
  return false;
}

int main(void) {
  while (cin >> n >> hx >> hy >> dx >> dy, n) {
    REP(i, n) cin >> cx[i] >> cy[i];
    REP(i, n)
    dist[i] = sqrt((cx[i] - dx) * (cx[i] - dx) + (cy[i] - dy) * (cy[i] - dy));
    memset(used, 0, sizeof(used));
    if (dfs(hx, hy, 0, 0.0))
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long LL;
static const double EPS = 1e-9;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
bool used[21];
int n, hx, hy, dx, dy;
vector<int> cx(21), cy(21);
vector<double> dist(21);
double pointdistance(int x1, int y1, int x2, int y2) {
  return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int dfs(int x, int y, int t, double s) {
  if (t == n)
    return true;
  REP(i, n) {
    if (!used[i]) {
      double nexts = s + pointdistance(x, y, cx[i], cy[i]);
      if (nexts > dist[i] - EPS)
        return false;
      else {
        used[i] = true;
        if (dfs(cx[i], cy[i], t + 1, nexts))
          return true;
        used[i] = false;
      }
    }
  }
  return false;
}

int main(void) {
  while (cin >> n >> hx >> hy >> dx >> dy, n) {
    REP(i, n) cin >> cx[i] >> cy[i];
    REP(i, n)
    dist[i] = sqrt((cx[i] - dx) * (cx[i] - dx) + (cy[i] - dy) * (cy[i] - dy));
    memset(used, 0, sizeof(used));
    if (dfs(hx, hy, 0, 0.0))
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}
Problem p01133 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
 *  Author: mizo0203
 *  Problem: 2008
 *  Status:
 *  Submission Date:
 */
#include <algorithm>
#include <math.h>
#include <stdio.h>

using namespace std;

int n;
int hx, hy, dx, dy;
int cx[20], cy[20];
double cdis[20];

// 初期化
void init() {}

// 入力
bool input() {
  int i;

  scanf("%d %d %d %d %d", &n, &hx, &hy, &dx, &dy);
  if (!n && !hx && !hy && !dx && !dy)
    return false;
  for (i = 0; i < n; i++) {
    scanf("%d %d", &cx[i], &cy[i]);
    cdis[i] = hypot(dx - cx[i], dy - cy[i]);
  }
  return true;
}

// 再帰関数
bool recursive(long bit, double sum, int x, int y) {
  int i;
  double minD, maxH;

  for (i = 0; i < n; i++)
    if (~bit & 1 << i)
      break;
  if (i == n)
    return true;
  minD = 2000000.0;  // INF
  maxH = -2000000.0; // -INF
  for (i = 0; i < n; i++) {
    if (bit & 1 << i)
      continue;
    if (minD > cdis[i]) {
      minD = cdis[i];
      maxH = hypot(x - cx[i], y - cy[i]) + sum;
    }
  }
  // printf("%f >= %fn", maxH, minD);
  if (maxH >= minD)
    return false;

  for (i = 0; i < n; i++) {
    if (bit & 1 << i)
      continue;
    if (recursive(bit | 1 << i, sum + hypot(x - cx[i], y - cy[i]), cx[i],
                  cy[i]))
      return true;
  }
  // printf("sum:%fn", sum);
  return false;
}

int main() {

  init();           // 初期化
  while (input()) { // 入力
    if (recursive(0, 0.0, hx, hy)) {
      printf("YES\\n");
    } else {
      printf("NO\\n");
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

/*
 *  Author: mizo0203
 *  Problem: 2008
 *  Status: Time Limit Exceeded
 *  Submission Date: 2012/04/14 23:23:19
 *
 *  方策：再帰関数による幅優先探索
 */
#include <algorithm>
#include <math.h>
#include <stdio.h>

using namespace std;

int n; // (0 < n <= 20)
int hx, hy, dx, dy;
int cx[20], cy[20]; // (-1000 <= h, d, c <= 1000)

void init() {}

bool input() {
  int i;

  scanf("%d %d %d %d %d", &n, &hx, &hy, &dx, &dy);
  if (!n && !hx && !hy && !dx && !dy)
    return false;
  for (i = 0; i < n; i++)
    scanf("%d %d", &cx[i], &cy[i]);

  return true;
}

// sum: 勇者の総移動距離^2 == 瘴気の半径^2
bool recursive(long l, int x, int y, double sum) {
  int i;
  double dis;

  for (i = 0; i < n; i++)
    if (~l & 1 << i)
      break;
  if (i == n)
    return true;

  for (i = 0; i < n; i++) {
    if (l & 1 << i)
      continue;
    dis = sqrt(pow(cx[i] - x, 2) + pow(cy[i] - y, 2));
    // printf("%f %f\\n", sum + dis, sqrt(pow(cx[i] - dx, 2) + pow(cy[i] - dy,
    // 2)));
    if (sum + dis >= sqrt(pow(cx[i] - dx, 2) + pow(cy[i] - dy, 2)))
      continue;
    if (recursive(l | 1 << i, cx[i], cy[i], sum + dis))
      return true;
  }

  return false;
}

int main() {
  int i;

  init();
  while (input()) {
    int pmt[20];
    // [0, 1, 2, ....]というサイズnの配列を作成
    for (i = 0; i < n; i++)
      pmt[i] = i;
    // 全ての順列を出力
    if (recursive(0, hx, hy, 0))
      printf("YES\\n");
    else
      printf("NO\\n");
  }

  return 0;
}
Problem p01133 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
static const double EPS = 1e-8;

int n, hx, hy, dx, dy;
int x[20], y[20];
bool flag;

void dfs(int unvisit, int cx, int cy, double t) {
  if (flag)
    return;

  if (unvisit == 0)
    flag = true;

  else {
    // 枝刈り
    rep(i, n) {
      if (!((unvisit >> i) & 1))
        continue;
      if (hypot(dx - x[i], dy - y[i]) < t + hypot(cx - x[i], cy - y[i]) + EPS)
        return;
    }

    rep(i, n) {
      if (!((unvisit >> i) & 1))
        continue;
      dfs((unvisit & ~(1 << i)), x[i], y[i], t + hypot(cx - x[i], cy - y[i]));
    }
  }
}

string solve() {
  flag = false;
  dfs((1 << n) - 1, hx, hy, 0);
  return (flag) ? "YES" : "NO";
}

int main() {
  while (cin >> n >> hx >> hy >> dx >> dy) {
    if ((n | hx | hy | dx | dy) == 0)
      break;

    rep(i, n) { cin >> x[i] >> y[i]; }

    cout << solve() << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;

static const double EPS = 1e-8;

int n, hx, hy, dx, dy;
int x[20], y[20];
bool flag;

void dfs(int unvisit, int cx, int cy, double t) {
  if (flag)
    return;
  if (unvisit == 0)
    flag = true;
  else {
    for (int i = 0; i < n; i++) {
      if (!(unvisit >> i) & 1)
        continue;
      if (hypot(dx - x[i], dy - y[i]) < t + hypot(cx - x[i], cy - y[i]) + EPS)
        return;
    }

    for (int i = 0; i < n; i++) {
      if (!((unvisit >> i) & 1))
        continue;
      dfs((unvisit & ~(1 << i)), x[i], y[i], t + hypot(cx - x[i], cy - y[i]));
    }
  }
}

string solve() {
  flag = false;
  dfs((1 << n) - 1, hx, hy, 0);
  return (flag) ? "YES" : "NO";
}

int main() {
  while (cin >> n >> hx >> hy >> dx >> dy) {
    if ((n | hx | hy | dx | dy) == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i];
    }

    cout << solve() << endl;
  }
}
Problem p01133 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

int n;
double x[22];
double y[22];
double d[22][22];

bool dfs(int k, int x, double c) {
  if (x == (1 << n) - 1) {
    return true;
  }
  rep(i, n) {
    if (!(x & (1 << i)) && c + d[k][i] >= d[i][n + 1]) {
      return false;
    }
  }
  rep(i, n) {
    if (!(x & (1 << i)) && dfs(i, x | (1 << i), c + d[k][i])) {
      return true;
    }
  }
  return false;
}

int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    scanf("%lf%lf%lf%lf", x + n, y + n, x + n + 1, y + n + 1);
    rep(i, n) { scanf("%lf%lf", x + i, y + i); }
    rep(i, n + 2) {
      rep(j, n + 2) {
        double dx = x[i] - x[j];
        double dy = y[i] - y[j];
        d[i][j] = sqrt(dx * dx + dy * dy);
      }
    }
    printf("%s\\n", dfs(n, 0, 0) ? "YES" : "NO");
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

const double inf = 1e4;

int n;
int x[22];
int y[22];
double d[22][22];
double dp[1 << 20][20];

double dfs(int s, int t) {
  if (dp[s][t] != -1) {
    return dp[s][t];
  }
  int u = s - (1 << t);
  if (u == 0) {
    return dp[s][t] = d[n][t] < d[n + 1][t] ? d[n][t] : inf;
  }
  double p = inf;
  rep(i, n) {
    if (u & (1 << i)) {
      double q = dfs(u, i) + d[i][t];
      if (q < d[n + 1][t]) {
        p = min(q, p);
      }
    }
  }
  return dp[s][t] = p;
}

void init() {
  rep(i, n + 2) {
    rep(j, n + 2) {
      double dx = x[i] - x[j];
      double dy = y[i] - y[j];
      d[i][j] = sqrt(dx * dx + dy * dy);
    }
  }
}

int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    rep(i, 2) { scanf("%d%d", x + n + i, y + n + i); }
    rep(i, n) { scanf("%d%d", x + i, y + i); }
    init();
    rep(i, 1 << n) { fill_n(dp[i], n, -1); }
    bool ans = false;
    rep(i, n) {
      if (dfs((1 << n) - 1, i) != inf) {
        ans = true;
        break;
      }
    }
    printf("%s\\n", ans ? "YES" : "NO");
  }
  return 0;
}
Problem p01133 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
double eps = 1e-9;
int x[23];
int y[23];
double dist[23][23];
int n;
int solve(int a, int b, double s) {
  if (a == (1 << n) - 1)
    return 1;
  for (int i = 0; i < n; i++) {
    if (a & (1 << i))
      continue;
    if (s + dist[b][i] + eps > dist[n + 1][i])
      return 0;
  }
  for (int i = 0; i < n; i++) {
    if (a & (1 << i))
      continue;

    if (solve(a + (1 << i), i, s + dist[b][i]))
      return 1;
  }
  return 0;
}
int main() {
  int a, hx, hy, dx, dy;
  while (scanf("%d%d%d%d%d", &a, &hx, &hy, &dx, &dy), a) {
    n = a;
    for (int i = 0; i < a; i++)
      scanf("%d%d", x + i, y + i);
    x[a] = hx;
    x[a + 1] = dx;
    y[a] = hy;
    y[a + 1] = dy;
    for (int i = 0; i < a + 2; i++)
      for (int j = 0; j < a + 2; j++)
        dist[i][j] =
            sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
    if (solve(0, a, 0))
      printf("YES\\n");
    else
      printf("NO\\n");
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
double eps = 1e-9;
int x[23];
int y[23];
double dist[23][23];
int n;
int solve(int a, int b, double s) {
  if (a == (1 << n) - 1)
    return 1;
  for (int i = 0; i < n; i++) {
    if (a & (1 << i))
      continue;
    if (s + dist[b][i] + eps > dist[n + 1][i])
      return 0;
    if (solve(a + (1 << i), i, s + dist[b][i]))
      return 1;
  }
  return 0;
}
int main() {
  int a, hx, hy, dx, dy;
  while (scanf("%d%d%d%d%d", &a, &hx, &hy, &dx, &dy), a) {
    n = a;
    for (int i = 0; i < a; i++)
      scanf("%d%d", x + i, y + i);
    x[a] = hx;
    x[a + 1] = dx;
    y[a] = hy;
    y[a + 1] = dy;
    for (int i = 0; i < a + 2; i++)
      for (int j = 0; j < a + 2; j++)
        dist[i][j] =
            sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
    if (solve(0, a, 0))
      printf("YES\\n");
    else
      printf("NO\\n");
  }
}
Problem p01133 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;
#define EPS 1e-10
int n;
double sx, sy, tx, ty, px[20], py[20];

double dis(double a, double b, double c, double d) {
  return sqrt((a - c) * (a - c) + (b - d) * (b - d));
}
double dis(int a, int b) { return dis(px[a], py[a], px[b], py[b]); }

bool dfs(int S, int pos, double cost) {
  if (S == (1 << n) - 1)
    return true;
  for (int i = 0; i < n; i++) {
    if ((S >> i) & 1)
      continue;
    if (cost + dis(pos, i) >= dis(tx, ty, px[i], py[i]))
      return false;
  }
  for (int i = 0; i < n; i++) {
    if ((S >> i) & 1)
      continue;
    if (cost + dis(pos, i) >= dis(tx, ty, px[i], py[i]))
      continue;
    if (dfs(S | (1 << i), i, cost + dis(pos, i)))
      return true;
  }
  return false;
}

bool solve() {
  for (int i = 0; i < n; i++) {
    if (dis(sx, sy, px[i], py[i]) >= dis(tx, ty, px[i], py[i]))
      continue;
    if (dfs((1 << i), i, dis(sx, sy, px[i], py[i])))
      return true;
  }
  return false;
}

int main() {
  while (1) {
    cin >> n >> sx >> sy >> tx >> ty;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> px[i] >> py[i];
    cout << (solve() ? "YES" : "NO") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> P;
typedef pair<double, P> PP;
#define INF (1 << 24)
#define EPS 1e-10
int n;
double sx, sy, tx, ty, px[20], py[20];
double tab[(1 << 20)];

double dis(double a, double b, double c, double d) {
  return sqrt((a - c) * (a - c) + (b - d) * (b - d));
}
double dis(int a, int b) { return dis(px[a], py[a], px[b], py[b]); }

int main() {
  while (1) {
    cin >> n >> sx >> sy >> tx >> ty;
    if (n == 0 && sx == 0 && sy == 0 && tx == 0 && ty == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> px[i] >> py[i];
    for (int i = 0; i < (1 << n); i++)
      tab[i] = INF;
    priority_queue<PP> Q;
    for (int i = 0; i < n; i++) {
      Q.push(PP(dis(sx, sy, px[i], py[i]), P((1 << i), i)));
      tab[(1 << i)] = dis(sx, sy, px[i], py[i]);
    }

    while (!Q.empty()) {
      PP tmp = Q.top();
      Q.pop();
      double total = tmp.first;
      int S = tmp.second.first, pos = tmp.second.second;

      bool flg = false;
      for (int i = 0; i < n; i++)
        if (total + dis(pos, i) > dis(tx, ty, px[i], py[i]))
          flg = true;
      if (flg)
        continue;

      for (int i = 0; i < n; i++) {
        if ((S >> i) & 1)
          continue;
        if (total + dis(pos, i) > tab[S | (1 << i)])
          continue;
        if (total + dis(pos, i) >= dis(tx, ty, px[i], py[i]))
          continue;
        tab[S | (1 << i)] = total + dis(pos, i);
        Q.push(PP(total + dis(pos, i), P(S | (1 << i), i)));
      }
    }

    bool flag = false;
    if (tab[(1 << n) - 1] != INF)
      flag = true;
    cout << (flag ? "YES" : "NO") << endl;
  }
  return 0;
}
Problem p01133 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <math.h>
#include <stdio.h>
#define MAX_NUM 100000000

double dist(int x, int y) { return sqrt((double)(x * x + y * y)); }

double min(double a, double b) {
  if (a > b)
    return b;
  else
    return a;
}

int n, cx[20], cy[20], dx, dy;

double dfs(int now, double sum, int bit) {
  int i;
  double ret = MAX_NUM;
  for (i = 0; i < n; i++)
    if (~bit & (1 << i) && sum + dist(cx[now] - cx[i], cy[now] - cy[i]) >=
                               dist(dx - cx[i], dy - cy[i]))
      return ret;
  if (sum >= dist(dx - cx[now], dy - cy[now]))
    return ret;
  if (bit == (1 << n) - 1)
    return sum;
  for (i = 0; i < n; i++) {
    if (~bit & (1 << i)) {
      ret = min(
          dfs(i, sum + dist(cx[i] - cx[now], cy[i] - cy[now]), bit | (1 << i)),
          ret);
    }
  }
  return ret;
}

int main(void) {
  int hx, hy, i;
  double ans;
  while (1) {
    scanf("%d%d%d%d%d", &n, &hx, &hy, &dx, &dy);
    if (n == 0 && hx == 0 && hy == 0 && dx == 0 && dy == 0)
      break;
    for (i = 0; i < n; i++)
      scanf("%d%d", &cx[i], &cy[i]);
    ans = MAX_NUM;
    for (i = 0; i < n; i++)
      ans = min(ans, dfs(i, dist(cx[i] - hx, cy[i] - hy), 1 << i));
    if (ans != MAX_NUM)
      printf("YES\\n");
    else
      printf("NO\\n");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
#define MAX_NUM 100000000

double dist(int x, int y) { return sqrt((double)(x * x + y * y)); }

double min(double a, double b) {
  if (a > b)
    return b;
  else
    return a;
}

int n, cx[20], cy[20], dx, dy;

double dfs(int now, double sum, int bit) {
  int i;
  double ret = MAX_NUM;
  if (sum >= dist(dx - cx[now], dy - cy[now]))
    return ret;
  if (bit == (1 << n) - 1)
    return sum;
  for (i = 0; i < n; i++) {
    if (~bit & (1 << i)) {
      ret = min(
          dfs(i, sum + dist(cx[i] - cx[now], cy[i] - cy[now]), bit | (1 << i)),
          ret);
    }
  }
  return ret;
}

int main(void) {
  int hx, hy, i;
  double ans;
  while (1) {
    scanf("%d%d%d%d%d", &n, &hx, &hy, &dx, &dy);
    if (n == 0 && hx == 0 && hy == 0 && dx == 0 && dy == 0)
      break;
    for (i = 0; i < n; i++)
      scanf("%d%d", &cx[i], &cy[i]);
    ans = MAX_NUM;
    for (i = 0; i < n; i++)
      ans = min(ans, dfs(i, dist(cx[i] - hx, cy[i] - hy), 1 << i));
    if (ans != MAX_NUM)
      printf("YES\\n");
    else
      printf("NO\\n");
  }
  return 0;
}
Problem p01133 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;

template <class T> inline void chmin(T &a, const T &b) {
  if (b < a)
    a = b;
}

int n;
int dx, dy;
int cx[21], cy[21];
double dist[21][21];
double limit[20];

bool dfs(int pos, int visited, double sum) {
  if (visited == (1 << n) - 1)
    return true;

  for (int i = 0; i < n; ++i) {
    if (visited & (1 << i))
      continue;

    const double next = sum + dist[pos][i];
    for (int j = 0; j < n; ++j)
      if (!(visited & (1 << j)) && next + dist[i][j] >= limit[j])
        goto next;

    if (dfs(i, (visited | (1 << i)), next))
      return true;

  next:;
  }

  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  const int INF = 0xfffffff;

  while (cin >> n, cin >> cx[n] >> cy[n] >> dx >> dy, n) {
    for (int i = 0; i < n; ++i)
      cin >> cx[i] >> cy[i];

    for (int i = 0; i <= n; ++i)
      for (int j = i + 1; j <= n; ++j)
        dist[i][j] = dist[j][i] = hypot(cx[i] - cx[j], cy[i] - cy[j]);

    for (int i = 0; i < n; ++i)
      limit[i] = hypot(cx[i] - dx, cy[i] - dy);

    cout << (dfs(n, 0, 0.0) ? "YES" : "NO") << endl;
  }

  return EXIT_SUCCESS;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;

int n;
int hx, hy, dx, dy;
int cx[20], cy[20];
double dist[20][20];
double limit[20];

bool dfs(int pos, int visited, double sum) {
  for (int i = 0; i < n; ++i)
    if (!(visited & (1 << i)) && sum >= limit[i])
      return false;

  if (visited == (1 << n) - 1)
    return true;

  for (int i = 0; i < n; ++i) {
    if (visited & (1 << i))
      continue;

    const double d = dist[pos][i];
    if (sum + d < limit[i] && dfs(i, visited | (1 << i), sum + d))
      return true;
  }

  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  const int INF = 0xfffffff;

  while (cin >> n >> hx >> hy >> dx >> dy, n) {
    for (int i = 0; i < n; ++i)
      cin >> cx[i] >> cy[i];

    for (int i = 0; i < n; ++i)
      for (int j = i + 1; j < n; ++j)
        dist[i][j] = dist[j][i] = hypot(cx[i] - cx[j], cy[i] - cy[j]);

    for (int i = 0; i < n; ++i)
      limit[i] = hypot(cx[i] - dx, cy[i] - dy);

    for (int i = 0; i < n; ++i) {
      const double d = hypot(cx[i] - hx, cy[i] - hy);
      if (d < limit[i] && dfs(i, (1 << i), d)) {
        cout << "YES" << endl;
        goto next;
      }
    }

    cout << "NO" << endl;
  next:;
  }

  return EXIT_SUCCESS;
}