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



#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define USE_MATH_DEFINES
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 29;
const double EPS = 1E-9;

typedef pair<xy_t, bool> dat;

namespace std {
bool operator<(const xy_t &a, const xy_t &b) {
  return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
} // namespace std

int n, hx, hy, dx, dy;
xy_t points[21];
double dis[1 << 21];
double adj[21][21];
double adjd[21];

bool dfs(double t, int v, int st) {

  for (int i = 1; i <= n; i++) {
    if (!(st & (1 << i)) && (adjd[i] <= t || adjd[i] <= t + adj[v][i]))
      return false;
  }
  if (st == (1 << (n + 1)) - 1)
    return true;
  for (int i = 1; i <= n; i++) {
    if (st & (1 << i))
      continue;
    if (dfs(t + adj[v][i], i, st | (1 << i)))
      return true;
  }
  return false;
}

int main() {
  while (cin >> n >> hx >> hy >> dx >> dy && (n || hx || hy || dx || dy)) {

    points[0] = xy_t(hx, hy);
    for (int i = 1; i <= n; i++) {
      int x, y;
      cin >> x >> y;
      points[i] = xy_t(x, y);
    }

    for (int i = 0; i < n + 1; i++) {
      adjd[i] = abs(points[i] - xy_t(dx, dy));

      for (int j = 0; j < n + 1; j++)
        adj[i][j] = abs(points[i] - points[j]);
    }

    if (dfs(0.0, 0, 1))
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define USE_MATH_DEFINES
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 29;
const double EPS = 1E-9;

typedef pair<xy_t, bool> dat;

namespace std {
bool operator<(const xy_t &a, const xy_t &b) {
  return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
} // namespace std

int n, hx, hy, dx, dy;
xy_t points[21];
double dis[1 << 21];
double adj[21][21];
double adjd[21];
map<P, bool> bad;
map<P, int> mp;

bool dfs(double t, int v, int st) {
  if (st == (1 << (n + 1)) - 1)
    return true;
  if (bad[P(v, st)])
    return false;
  if (mp[P(v, st)] != 0 && (int)t > mp[P(v, st)])
    return false;
  mp[P(v, st)] = (int)t;

  bool res = false;
  for (int i = 1; i < n + 1; i++) {
    if (!(st & (1 << i))) {
      double nt = t + adj[v][i];
      if (nt < adjd[i] && nt < dis[st | (1 << i)])
        res |= dfs(nt, i, st | (1 << i));
    }
    if (res)
      return true;
  }
  bad[P(v, st)] = true;
  return false;
}

int main() {
  while (cin >> n >> hx >> hy >> dx >> dy && (n || hx || hy || dx || dy)) {
    bad.clear();
    mp.clear();
    points[0] = xy_t(hx, hy);
    for (int i = 1; i <= n; i++) {
      int x, y;
      cin >> x >> y;
      points[i] = xy_t(x, y);
    }
    for (int i = 0; i < n + 1; i++) {
      adjd[i] = abs(points[i] - xy_t(dx, dy));
      for (int j = 0; j < n + 1; j++)
        adj[i][j] = abs(points[i] - points[j]);
    }

    for (int i = 1; i < (1 << (n + 1)); i++) {
      double d = 1e10;
      for (int j = 0; j < n + 1; j++) {
        if (!(i & (1 << j)))
          d = min(d, abs(points[j] - xy_t(dx, dy)));
      }
      dis[i] = d;
    }
    if (dfs(0.0, 0, 1))
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}