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

// 56
#include <algorithm>
#include <cmath>
#include <iostream>

using namespace std;

#define S(X) ((X) * (X))

int n, hx, hy, dx, dy;
double d[21][21];
double l[21];

bool dfs(int s, double t, int g) {
  if (g == (1 << n + 1) - 1)
    return true;
  for (int i = 1; i <= n; i++) {
    if (!(g & 1 << i)) {
      if (l[i] < t + d[s][i])
        return false;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!(g & 1 << i) && t + d[s][i] < l[i]) {
      if (dfs(i, t + d[s][i], g | 1 << i))
        return true;
    }
  }
  return false;
}

int main() {
  while (cin >> n >> hx >> hy >> dx >> dy, n | hx | hy | dx | dy) {
    int cx[21] = {hx}, cy[21] = {hy};
    for (int i = 1; i <= n; i++) {
      cin >> cx[i] >> cy[i];
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        d[i][j] = sqrt(S(cx[i] - cx[j]) + S(cy[i] - cy[j]));
      }
    }
    for (int i = 1; i <= n; i++) {
      l[i] = sqrt(S(dx - cx[i]) + S(dy - cy[i]));
    }
    cout << (dfs(0, 0, 1) ? "YES" : "NO") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 56
#include <algorithm>
#include <cmath>
#include <iostream>

using namespace std;

#define S(X) ((X) * (X))

int n, hx, hy, dx, dy;
double d[21][21];
double l[21];

bool dfs(int s, double t, int g) {
  if (g == (1 << n + 1) - 1)
    return true;
  double ll = 1 << 30;
  for (int i = 1; i <= n; i++) {
    if (!(g & 1 << i) && ll > l[i]) {
      ll = l[i];
    }
  }
  double lb = 1 << 30;
  for (int i = 0; i <= n; i++) {
    if (!(g & 1 << i) && lb > d[s][i] + t) {
      lb = d[s][i] + t;
    }
  }
  if (ll < lb)
    return false;
  for (int i = 1; i <= n; i++) {
    if (!(g & 1 << i) && t + d[s][i] < l[i]) {
      if (dfs(i, t + d[s][i], g | 1 << i))
        return true;
    }
  }
  return false;
}

int main() {
  while (cin >> n >> hx >> hy >> dx >> dy, n | hx | hy | dx | dy) {
    int cx[21] = {hx}, cy[21] = {hy};
    for (int i = 1; i <= n; i++) {
      cin >> cx[i] >> cy[i];
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        d[i][j] = sqrt(S(cx[i] - cx[j]) + S(cy[i] - cy[j]));
      }
    }
    for (int i = 1; i <= n; i++) {
      l[i] = sqrt(S(dx - cx[i]) + S(dy - cy[i]));
    }
    cout << (dfs(0, 0, 1) ? "YES" : "NO") << endl;
  }
  return 0;
}
Problem p01133 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int n, dx, dy, hx, hy;
int cx[21], cy[21];
bool used[21];
bool flag;
void dfs(int v, int cnt, double cost) {
  if (cnt == n)
    flag = true;
  if (flag)
    return;
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      double move = sqrt(pow(cx[v] - cx[i], 2) + pow(cy[v] - cy[i], 2));
      double ng = sqrt(pow(dx - cx[i], 2) + pow(dy - cy[i], 2));
      if (cost + move >= ng)
        return;
    }
  }
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      double move = sqrt(pow(cx[v] - cx[i], 2) + pow(cy[v] - cy[i], 2));
      used[i] = true;
      dfs(i, cnt + 1, cost + move);
      used[i] = false;
    }
  }
}

int main(void) {
  while (1) {
    scanf("%d %d %d %d %d", &n, &hx, &hy, &dx, &dy);
    if (n + hx + hy + dx + dy == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d %d", &cx[i], &cy[i]);
    }
    cx[n] = hx, cy[n] = hy;
    flag = false;
    dfs(n, 0, 0);
    printf("%s\\n", flag ? "YES" : "NO");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <vector>

using namespace std;
int n, dx, dy, hx, hy;
int cx[21], cy[21];
bool used[21];
bool flag;
void dfs(int v, int cnt, int cost) {
  if (cnt == n)
    flag = true;
  if (flag)
    return;
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      int move = abs(cx[v] - cx[i]) + abs(cy[v] - cy[i]);
      int ng = abs(dx - cx[i]) + abs(dy - cy[i]);
      if (cost + move >= ng)
        return;
    }
  }
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      int move = abs(cx[v] - cx[i]) + abs(cy[v] - cy[i]);
      used[i] = true;
      dfs(i, cnt + 1, cost + move);
      used[i] = false;
    }
  }
}

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#define INF (1 << 30)
#define EPS (double)(1e-5)
using namespace std;
int n, hx, hy, dx, dy;
int cx[22], cy[22];

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

bool solve(int m, int bit, double l) {
  if (bit == (1 << n) - 1)
    return true;
  for (int i = 1; i < n; i++) {
    if (bit & (1 << i))
      continue;
    if (L(cx[m] - cx[i], cy[m] - cy[i]) + l + EPS > L(cx[i] - dx, cy[i] - dy))
      return false;
  }
  for (int i = 1; i < n; i++) {
    if (bit & (1 << i))
      continue;
    if (solve(i, bit | (1 << i), l + L(cx[m] - cx[i], cy[m] - cy[i])))
      return true;
  }
  return false;
}

int main(void) {
  while (cin >> n >> cx[0] >> cy[0] >> dx >> dy && n) {
    n++;
    for (int i = 1; i < n; i++) {
      cin >> cx[i] >> cy[i];
    }
    if (solve(0, 1, 0))
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#define INF (1 << 30)
#define EPS (double)(1e-5)
using namespace std;
int n, hx, hy, dx, dy;
int cx[22], cy[22];

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

bool solve(int m, int bit, double l) {
  if (bit == (1 << n) - 1)
    return true;
  for (int i = 1; i < n; i++) {
    if (bit & (1 << i))
      continue;
    if (L(cx[m] - cx[i], cy[m] - cy[i]) + l + EPS > L(cx[i] - dx, cy[i] - dy))
      return false;
    if (solve(i, bit | (1 << i), l + L(cx[m] - cx[i], cy[m] - cy[i])))
      return true;
  }
  return false;
}

int main(void) {
  while (cin >> n >> cx[0] >> cy[0] >> dx >> dy && n) {
    n++;
    for (int i = 1; i < n; i++) {
      cin >> cx[i] >> cy[i];
    }
    if (solve(0, 1, 0))
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
}
Problem p01133 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <map>
#include <math.h>
#include <stdio.h>
#include <utility>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mp make_pair
inline double sq(double a) { return a * a; }
#define INF (1e100)
#define EPS (1e-9)

int n;
double hx, hy, mx, my, xs[32], ys[32], ds[32], ws[32][32];
pair<double, int> ns[32];

bool rec(int b, int at, double t) {
  if (b == (1 << n) - 1)
    return true;
  rep(i, n) {
    const int ix = ns[i].second;
    if (b & (1 << ix))
      continue;
    double nt = t + ws[at][ix];
    if (nt > ds[ix] - EPS)
      return false;
    if (rec(b | (1 << ix), ix, nt))
      return true;
  }
  return false;
}

int main() {
  for (;;) {
    scanf("%d%lf%lf%lf%lf", &n, &hx, &hy, &mx, &my);
    if (n == 0)
      return 0;
    rep(i, n) scanf("%lf%lf", xs + i, ys + i);
    rep(i, n) ds[i] = sqrt(sq(xs[i] - mx) + sq(ys[i] - my));
    rep(i, n) ns[i] = mp(ds[i], i);
    sort(ns, ns + n);
    rep(i, n) rep(j, n) {
      ws[i][j] = sqrt(sq(xs[i] - xs[j]) + sq(ys[i] - ys[j]));
    }
    bool ans = false;
    rep(i, n) {
      double t = sqrt(sq(xs[i] - hx) + sq(ys[i] - hy));
      if (t < ds[i] - EPS && rec(1 << i, i, t)) {
        ans = true;
        break;
      }
    }
    puts(ans ? "YES" : "NO");
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
inline double sq(double a) { return a * a; }
#define INF (1e100)
#define EPS (1e-9)

int n;
double hx, hy, mx, my, xs[32], ys[32], ds[32], ws[32][32];
double dp[1 << 20][20];

int main() {
  for (;;) {
    scanf("%d%lf%lf%lf%lf", &n, &hx, &hy, &mx, &my);
    if (n == 0)
      return 0;
    rep(i, n) scanf("%lf%lf", xs + i, ys + i);
    rep(i, n) ds[i] = sqrt(sq(xs[i] - mx) + sq(ys[i] - my));
    rep(i, n) rep(j, n) {
      ws[i][j] = sqrt(sq(xs[i] - xs[j]) + sq(ys[i] - ys[j]));
    }
    const int nn = 1 << n;
    rep(i, nn) rep(j, n) dp[i][j] = INF;
    rep(i, n) {
      double t = sqrt(sq(xs[i] - hx) + sq(ys[i] - hy));
      if (t < ds[i] - EPS)
        dp[1 << i][i] = t;
    }
    rep(b, nn) rep(k, n) if (dp[b][k] < INF) {
      rep(i, n) if ((b & (1 << i)) == 0) {
        double t = dp[b][k] + ws[k][i];
        if (t < ds[i] - EPS)
          dp[b | 1 << i][i] = min(dp[b | 1 << i][i], t);
      }
    }
    bool ans = false;
    rep(k, n) if (dp[nn - 1][k] < INF) ans = true;
    puts(ans ? "YES" : "NO");
  }
}
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 <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF 1145141919
#define EPS 1e-9
#define Pi acos(-1)

using namespace std;

typedef long long ll;

#define MAX_N 20

int N;
int hx, hy, dx, dy;
int cx[MAX_N], cy[MAX_N];

int dir[30][30]; // N:勇者 N+1:魔王

vector<string> ans_list;

void init() {

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dir[i][j] = abs(cx[i] - cx[j]) + abs(cy[i] - cy[j]);
    }
  }

  for (int i = 0; i < N; i++) {
    dir[i][N] = dir[N][i] = abs(cx[i] - hx) + abs(cy[i] - hy);
    dir[i][N + 1] = dir[N + 1][i] = abs(cx[i] - dx) + abs(cy[i] - dy);
  }
}

bool dfs(int S, int now, int t) {

  if (S == (1 << N) - 1)
    return true;

  // 枝狩り
  for (int i = 0; i < N; i++) {
    if (!(S >> i & 1) && dir[now][i] >= dir[N + 1][i] - t) {
      return false;
    }
  }

  // 探索
  for (int i = 0; i < N; i++) {
    if (!(S >> i & 1)) {
      if (dfs(S | (1 << i), i, t + dir[now][i]))
        return true;
    }
  }

  return false;
}

int main() {

  while (cin >> N >> hx >> hy >> dx >> dy, N) {

    for (int i = 0; i < N; i++) {
      cin >> cx[i] >> cy[i];
    }

    init();

    if (dfs(0, N, 0))
      ans_list.push_back("YES");
    else
      ans_list.push_back("NO");
  }

  for (int i = 0; i < ans_list.size(); i++) {
    cout << ans_list[i] << endl;
  }

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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF 1145141919
#define EPS 1e-9
#define Pi acos(-1)

using namespace std;

typedef long long ll;

#define MAX_N 20

int N;
int hx, hy, dx, dy;
int cx[MAX_N], cy[MAX_N];

vector<string> ans_list;

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

bool dfs(int S, int nowx, int nowy, double t) {

  if (S == (1 << N) - 1)
    return true;

  // 枝狩り
  for (int i = 0; i < N; i++) {
    if (!(S >> i & 1) &&
        dist(dx, dy, cx[i], cy[i]) - t <= dist(nowx, nowy, cx[i], cy[i]))
      return false;
  }

  // 探索
  for (int i = 0; i < N; i++) {
    if (!(S >> i & 1)) {
      if (dfs(S | (1 << i), cx[i], cy[i], t + dist(nowx, nowy, cx[i], cy[i])))
        return true;
    }
  }

  return false;
}

int main() {

  while (cin >> N >> hx >> hy >> dx >> dy, N) {

    for (int i = 0; i < N; i++) {
      cin >> cx[i] >> cy[i];
    }

    if (dfs(0, hx, hy, 0))
      ans_list.push_back("YES");
    else
      ans_list.push_back("NO");
  }

  for (int i = 0; i < ans_list.size(); i++) {
    cout << ans_list[i] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF 1145141919
#define EPS 1e-9
#define Pi acos(-1)

using namespace std;

typedef long long ll;

#define MAX_N 20

int N;
int hx, hy, dx, dy;
int cx[MAX_N], cy[MAX_N];

vector<string> ans_list;

int dist(int x1, int y1, int x2, int y2) { return abs(x1 - x2) + abs(y1 - y2); }

bool dfs(int S, int nowx, int nowy, int t) {

  if (S == (1 << N) - 1)
    return true;

  // 枝狩り
  for (int i = 0; i < N; i++) {
    if (!(S >> i & 1) &&
        dist(dx, dy, cx[i], cy[i]) - t <= dist(nowx, nowy, cx[i], cy[i]))
      return false;
  }

  // 探索
  for (int i = 0; i < N; i++) {
    if (!(S >> i & 1)) {
      if (dfs(S | (1 << i), cx[i], cy[i], t + dist(nowx, nowy, cx[i], cy[i])))
        return true;
    }
  }

  return false;
}

int main() {

  while (cin >> N >> hx >> hy >> dx >> dy, N) {

    for (int i = 0; i < N; i++) {
      cin >> cx[i] >> cy[i];
    }

    if (dfs(0, hx, hy, 0))
      ans_list.push_back("YES");
    else
      ans_list.push_back("NO");
  }

  for (int i = 0; i < ans_list.size(); i++) {
    cout << ans_list[i] << endl;
  }

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

#include <cmath>
#include <iostream>

#define EPS (1e-10)

using namespace std;

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

double Dist(double xx, double yy) { return sqrt(xx * xx + yy * yy); }

bool search(int count, double t) {
  if (count == n)
    return true;
  for (int i = 0; i < n; ++i) {
    if (got[i])
      continue;
    double dist = (Dist(hx - cx[i], hy - cy[i]) + t);
    if ((Dist(dx - cx[i], dy - cy[i]) <= dist + EPS))
      return false;
  }
  bool hit = false;
  double tx = hx, ty = hy;
  for (int i = 0; i < n && !hit; ++i) {
    if (got[i])
      continue;
    got[i] = true;
    hx = cx[i];
    hy = cy[i];
    hit |= search(count + 1, (Dist(tx - cx[i], ty - cy[i]) + t));
    got[i] = false;
  }
  return hit;
}

int main(void) {
  while (cin >> n >> hx >> hy >> dx >> dy) {
    if ((int)(n + hx + hy + dx + dy) == 0)
      break;
    for (int i = 0; i < n; ++i) {
      cin >> cx[i] >> cy[i];
      got[i] = false;
    }
    if (search(0, 0))
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

#define EPS (1e-10)

using namespace std;

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

double Dist(double xx, double yy) { return sqrt(xx * xx + yy * yy); }

bool search(int count, double t) {
  if (count == n)
    return true;
  bool hit = true;
  for (int i = 0; i < n; ++i) {
    if (got[i])
      continue;
    double dist = (Dist(hx - cx[i], hy - cy[i]) + t);
    hit &= (Dist(dx - cx[i], dy - cy[i]) > dist + EPS);
  }
  if (!hit)
    return false;
  double tx = hx, ty = hy;
  for (int i = 0; i < n; ++i) {
    if (got[i])
      continue;
    got[i] = true;
    hx = cx[i];
    hy = cy[i];
    hit |= search(count + 1, (Dist(tx - cx[i], ty - cy[i]) + t));
    got[i] = false;
  }
  return hit;
}

int main(void) {
  while (cin >> n >> hx >> hy >> dx >> dy) {
    if ((int)(n + hx + hy + dx + dy) == 0)
      break;
    for (int i = 0; i < n; ++i) {
      cin >> cx[i] >> cy[i];
      got[i] = false;
    }
    if (search(0, 0))
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}
Problem p01133 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>

using namespace std;

int n, sx, sy, mx, my;
int xi[22], yi[22];
bool used[22];
const double eps = 1e-8;
inline double sc(double a) { return a * a; }
#define R(a, b) (sqrt(sc(a) + sc(b)))

bool solve(int a, int p, double t, double x, double y) {
  // printf("%d %f %f %f\\n",a,t,x,y);
  if (a == n)
    return true;
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      if (R(xi[i] - mx, yi[i] - my) < t + R(x - xi[i], y - yi[i]) + eps) {
        return false;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      used[i] = true;
      if (solve(a + 1, i, t + R(x - xi[i], y - yi[i]), xi[i], yi[i]))
        return true;
      used[i] = false;
    }
  }
  return false;
}

int main(void) {
  for (;;) {
    scanf("%d%d%d%d%d", &n, &sx, &sy, &mx, &my);
    if (!n)
      break;
    for (int i = 0; i < n; i++) {
      used[i] = false;
      scanf("%d%d", xi + i, yi + i);
    }
    bool res = solve(0, -1, 0, sx, sy);
    puts(res ? "YES" : "NO");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>

using namespace std;

int n, sx, sy, mx, my;
int xi[22], yi[22];
bool used[22];
const double eps = 1e-8;

bool solve(int a, double t, double x, double y) {
  // printf("%d %f %f %f\\n",a,t,x,y);
  if (a == n)
    return true;
  for (int i = 0; i < n; i++) {
    if (hypot(xi[i] - mx, yi[i] - my) < t + hypot(x - xi[i], y - yi[i]) + eps)
      return false;
  }
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      used[i] = true;
      if (solve(a + 1, t + hypot(x - xi[i], y - yi[i]), xi[i], yi[i]))
        return true;
      used[i] = false;
    }
  }
  return false;
}

int main(void) {
  for (;;) {
    scanf("%d%d%d%d%d", &n, &sx, &sy, &mx, &my);
    if (!n)
      break;
    for (int i = 0; i < n; i++) {
      used[i] = false;
      scanf("%d%d", xi + i, yi + i);
    }
    bool res = solve(0, 0, sx, sy);
    puts(res ? "YES" : "NO");
  }
  return 0;
}
Problem p01133 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

using namespace std;

const double EPS = 1e-9;

int n;
double d[21][21];

bool dfs(double t, int i, bool *vis) {
  if (d[i][n] < t + EPS)
    return false;
  if (count(vis, vis + n, true) == n)
    return true;

  rep(j, n) if (!vis[j] && d[i][j] + EPS > d[j][n] - t) return false;

  rep(j, n) if (!vis[j]) {
    vis[j] = true;
    if (dfs(t + d[i][j], j, vis))
      return true;
    vis[j] = false;
  }
  return false;
}

int main() {
  for (int hx, hy, mx, my; scanf("%d%d%d%d%d", &n, &hx, &hy, &mx, &my), n;) {
    int cx[20], cy[20];
    rep(i, n) scanf("%d%d", cx + i, cy + i);
    rep(i, n) {
      rep(j, n) d[i][j] = hypot(cx[i] - cx[j], cy[i] - cy[j]);
      d[i][n] = hypot(cx[i] - mx, cy[i] - my);
    }

    bool ok = false, vis[20] = {};
    rep(i, n) {
      vis[i] = true;
      if (dfs(hypot(hx - cx[i], hy - cy[i]), i, vis)) {
        ok = true;
        break;
      }
      vis[i] = false;
    }
    puts(ok ? "YES" : "NO");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>

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

using namespace std;

int n, hx, hy, mx, my, cx[20], cy[20];

bool dfs(int t, int x, int y, bool *vis) {
  if (abs(x - mx) + abs(y - my) <= t)
    return false;
  if (count(vis, vis + n, true) == n)
    return true;

  rep(i, n) if (!vis[i]) {
    if (abs(x - cx[i]) + abs(y - cy[i]) > abs(mx - cx[i]) + abs(my - cy[i]) - t)
      return false;
  }

  rep(i, n) if (!vis[i]) {
    vis[i] = true;
    if (dfs(t + abs(x - cx[i]) + abs(y - cy[i]), cx[i], cy[i], vis))
      return true;
    vis[i] = false;
  }
  return false;
}

int main() {
  for (; scanf("%d%d%d%d%d", &n, &hx, &hy, &mx, &my), n;) {
    rep(i, n) scanf("%d%d", cx + i, cy + i);

    bool ok = false, vis[20] = {};
    rep(i, n) if (dfs(0, hx, hy, vis)) {
      ok = true;
      break;
    }
    puts(ok ? "YES" : "NO");
  }

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

#include <bits/stdc++.h>
using namespace std;
int N;
double mx, my;
double x[21], y[21];
bool endFlag;

const double EPS = 1e-8;
bool input() {
  scanf("%d", &N);
  scanf("%lf%lf%lf%lf", &x[N], &y[N], &mx, &my);
  if (!(N || x[N] || y[N] || mx || my))
    return false;
  for (int i = 0; i < N; i++)
    scanf("%lf%lf", &x[i], &y[i]);
  return true;
}

void dfs(int S, int pos, double total) {
  if (S == (1 << N) - 1) {
    endFlag = true;
    return;
  }
  if (endFlag)
    return;

  for (int i = 0; i < N; i++) {
    if ((S >> i) & 1)
      continue;
    double mao = hypot(mx - x[i], my - y[i]);
    double yu = hypot(x[pos] - x[i], y[pos] - y[i]);
    if (yu + total + EPS > mao)
      return;
  }

  for (int i = 0; i < N; i++) {
    if ((S >> i) & 1)
      continue;
    double yu = hypot(x[pos] - x[i], y[pos] - y[i]);
    dfs(S | (1 << i), i, total + yu);
  }
}
void solve() {
  endFlag = false;
  dfs(0, N, 0);
  puts(endFlag ? "YES" : "NO");
}
int main() {
  while (input())
    solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int N;
double mx, my;
double x[21], y[21];
bool endFlag;
bool input() {
  scanf("%d", &N);
  scanf("%lf%lf%lf%lf", &x[N], &y[N], &mx, &my);
  if (!(N || x[N] || y[N] || mx || my))
    return false;
  for (int i = 0; i < N; i++)
    scanf("%lf%lf", &x[i], &y[i]);
  return true;
}

void dfs(int S, int pos, double total) {
  if (S == (1 << N) - 1) {
    endFlag = true;
    return;
  }
  if (endFlag)
    return;

  for (int i = 0; i < N; i++) {
    if ((S >> i) & 1)
      continue;
    double mao = hypot(mx - x[i], my - y[i]);
    double yu = hypot(x[pos] - x[i], y[pos] - y[i]);
    if (yu + total >= mao)
      return;
    dfs(S | (1 << i), i, total + yu);
  }

  for (int i = 0; i < N; i++) {
    if ((S >> i) & 1)
      continue;
    double yu = hypot(x[pos] - x[i], y[pos] - y[i]);
    dfs(S | (1 << i), i, total + yu);
  }
}
void solve() {
  endFlag = false;
  dfs(0, N, 0);
  puts(endFlag ? "YES" : "NO");
}
int main() {
  while (input())
    solve();
  return 0;
}