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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#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;
typedef long long ll;
typedef ll li;
typedef pair<int, int> PI;
#define EPS (1e-6)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP(i, n) rep(i, n)
#define F first
#define S second
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
#define FLL(a, b) memset((a), b, sizeof(a))
#define CLR(a) memset((a), 0, sizeof(a))
#define FOR(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
template <typename T, typename U>
ostream &operator<<(ostream &out, const pair<T, U> &val) {
  return out << "(" << val.F << ", " << val.S << ")";
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &val) {
  out << "{";
  rep(i, SZ(val)) out << (i ? ", " : "") << val[i];
  return out << "}";
}
typedef double FP;
typedef complex<FP> pt;
typedef pt P;
typedef pair<pt, pt> line;
FP dot(P a, P b) { return (conj(a) * b).real(); }
FP crs(P a, P b) { return (conj(a) * b).imag(); }
P crspt(P a, P b, P c, P d) {
  b -= a, d -= c;
  return a + b * crs(d, c - a) / crs(d, b);
}
bool onl(P a1, P a2, P b) {
  return abs(b - a1) + abs(b - a2) < abs(a1 - a2) + EPS;
}
void pkuassert(bool t) { t = 1 / t; };
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};
/*
enum{TOP,BTM,LFT,RGT,FRT,BCK};
int dxdy2ce[]={RGT,FRT,LFT,BCK};
template<class T> T shift(T a,int b,int c,int d,int e){
  __typeof(a[0])t=a[b];
  a[b]=a[c];a[c]=a[d];a[d]=a[e];a[e]=t;return a;}
template<class T> T rgtin(T a){return shift(a,TOP,LFT,BTM,RGT);}
template<class T> T lftin(T a){return shift(a,TOP,RGT,BTM,LFT);}
template<class T> T frtin(T a){return shift(a,TOP,BCK,BTM,FRT);}
template<class T> T bckin(T a){return shift(a,TOP,FRT,BTM,BCK);}
*/

int n;
int hx, hy, mx, my;
int cx[20], cy[20];
map<PI, double> cost;

bool dfs(int cv, int vis, double cc) {
  if (cost.count(mp(cv, vis)) && cost[mp(cv, vis)] <= cc + EPS)
    return false;
  if (vis == (1 << n) - 1)
    return true;
  // cout << cv << ' ' << vis << ' ' << cc << endl;
  cost[mp(cv, vis)] = cc;
  rep(i, n) {
    if ((vis >> i) & 1)
      continue;
    if (cc + abs(pt(cx[i], cy[i]) - pt(cx[cv], cy[cv])) + EPS >
        abs(pt(cx[i], cy[i]) - pt(mx, my)))
      return false;
  }

  rep(i, n) {
    if ((vis >> i) & 1)
      continue;
    if (dfs(i, vis | 1 << i, cc + abs(P(cx[cv], cy[cv]) - P(cx[i], cy[i]))))
      return true;
  }

  return false;
}

void solve() {
  cin >> hx >> hy >> mx >> my;
  rep(i, n) cin >> cx[i] >> cy[i];
  // rep(i,n)rep(j,(1<<n)) cost[i][j] = 1<<20;
  cost.clear();
  rep(i, n) {
    if (abs(P(mx, my) - P(cx[i], cy[i])) >
            abs(P(cx[i], cy[i]) - P(hx, hy)) + EPS &&
        dfs(i, 1 << i, abs(P(cx[i], cy[i]) - P(hx, hy)))) {
      cout << "YES" << endl;
      return;
    }
  }
  cout << "NO" << endl;
}

int main(int argc, char *argv[]) {
  while (cin >> n && n)
    solve();
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#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;
typedef long long ll;
typedef ll li;
typedef pair<int, int> PI;
#define EPS (1e-6)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP(i, n) rep(i, n)
#define F first
#define S second
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
#define FLL(a, b) memset((a), b, sizeof(a))
#define CLR(a) memset((a), 0, sizeof(a))
#define FOR(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
template <typename T, typename U>
ostream &operator<<(ostream &out, const pair<T, U> &val) {
  return out << "(" << val.F << ", " << val.S << ")";
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &val) {
  out << "{";
  rep(i, SZ(val)) out << (i ? ", " : "") << val[i];
  return out << "}";
}
typedef double FP;
typedef complex<FP> pt;
typedef pt P;
typedef pair<pt, pt> line;
FP dot(P a, P b) { return (conj(a) * b).real(); }
FP crs(P a, P b) { return (conj(a) * b).imag(); }
P crspt(P a, P b, P c, P d) {
  b -= a, d -= c;
  return a + b * crs(d, c - a) / crs(d, b);
}
bool onl(P a1, P a2, P b) {
  return abs(b - a1) + abs(b - a2) < abs(a1 - a2) + EPS;
}
void pkuassert(bool t) { t = 1 / t; };
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};
enum { TOP, BTM, LFT, RGT, FRT, BCK };
int dxdy2ce[] = {RGT, FRT, LFT, BCK};
template <class T> T shift(T a, int b, int c, int d, int e) {
  __typeof(a[0]) t = a[b];
  a[b] = a[c];
  a[c] = a[d];
  a[d] = a[e];
  a[e] = t;
  return a;
}
template <class T> T rgtin(T a) { return shift(a, TOP, LFT, BTM, RGT); }
template <class T> T lftin(T a) { return shift(a, TOP, RGT, BTM, LFT); }
template <class T> T frtin(T a) { return shift(a, TOP, BCK, BTM, FRT); }
template <class T> T bckin(T a) { return shift(a, TOP, FRT, BTM, BCK); }

int n;
int hx, hy, mx, my;
int cx[20], cy[20];
double cost[20][1 << 20];

bool dfs(int cv, int vis, double cc) {
  if (cost[cv][vis] <= cc + EPS)
    return false;
  if (vis == (1 << n) - 1)
    return true;
  // cout << cv << ' ' << vis << ' ' << cc << endl;
  cost[cv][vis] = cc;
  rep(i, n) {
    if ((vis >> i) & 1)
      continue;
    if (cc + abs(pt(cx[i], cy[i]) - pt(cx[cv], cy[cv])) + EPS >
        abs(pt(cx[i], cy[i]) - pt(mx, my)))
      return false;
  }

  rep(i, n) {
    if ((vis >> i) & 1)
      continue;
    if (dfs(i, vis | 1 << i, cc + abs(P(cx[cv], cy[cv]) - P(cx[i], cy[i]))))
      return true;
  }

  return false;
}

void solve() {
  cin >> hx >> hy >> mx >> my;
  rep(i, n) cin >> cx[i] >> cy[i];
  rep(i, n) rep(j, (1 << n)) cost[i][j] = 1 << 20;
  rep(i, n) {
    if (abs(P(mx, my) - P(cx[i], cy[i])) >
            abs(P(cx[i], cy[i]) - P(hx, hy)) + EPS &&
        dfs(i, 1 << i, abs(P(cx[i], cy[i]) - P(hx, hy)))) {
      cout << "YES" << endl;
      return;
    }
  }
  cout << "NO" << endl;
}

int main(int argc, char *argv[]) {
  while (cin >> n && n)
    solve();
}