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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

#define INF (1 << 24)

int d(int n) { return n * n; }

typedef pair<double, pair<int, int>> P;

int v_to_int(vector<bool> v) {
  int ret = 0;
  for (int i = 0; i < v.size(); ++i) {
    ret <<= 1;
    ret |= v[i];
  }
  return ret;
}

double EPS = 1e-9;

int main() {
  int L;
  while (scanf("%d", &L) != EOF) {
    vector<int> v;

    char c;
    while (c = getchar(), c != '\\n' && c != '\\r' && c != EOF) {
      int a;
      scanf("%d", &a);
      v.push_back(a);
    }

    /*
                    sort( v.begin(), v.end() );

                    int n = v.size();
                    vector<int> v1, v2;
                    for (int i = 0; i < n/2; ++i)
                            v1.push_back(v[i]);

                    for (int i = n/2; i < n; ++i)
                            v2.push_back(v[i]);
    */

    int n = v.size();
    vector<vector<double>> len(n, vector<double>(n, INF));
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        len[i][j] = sqrt(d(v[i] + v[j]) - d(abs(v[i] - v[j])));

    priority_queue<P, vector<P>, greater<P>> Q;
    for (int i = 0, k = 1; i < n; ++i, k *= 2) {
      Q.push(P(v[i], make_pair(i, k)));
    }

    double ans = INF;
    double cost[1 << 12][12];
    fill(&cost[0][0], &cost[0][0] + (1 << 12) * 12, INF);
    while (!Q.empty()) {
      P p = Q.top();
      Q.pop();

      double length = p.first;
      int head = p.second.first;
      int prev = p.second.second;

      int sz = 0;
      for (int i = 0, k = 1; i < n; ++i, k *= 2)
        sz += !!(prev & k);

      if (sz >= n) {
        if (length + v[head] < ans)
          ans = length + v[head];

        continue;
      }

      for (int i = 0, k = 1; i < n; ++i, k *= 2) {
        if (prev & k)
          continue;

        int new_prev = prev | k;

        if (length + len[head][i] + v[i] > cost[new_prev][head] - EPS)
          continue;
        cost[new_prev][head] = length + len[head][i] + v[i];

        Q.push(P(length + len[head][i], make_pair(i, new_prev)));
      }
    }

    //		cout << ans << endl;
    if (ans <= L)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

#define INF 1 << 24

int d(int n) { return n * n; }

typedef pair<vector<bool>, pair<int, double>> P;

int v_to_int(vector<bool> v) {
  int ret = 0;
  for (int i = 0; i < v.size(); ++i) {
    ret <<= 1;
    ret |= v[i];
  }
  return ret;
}

int main() {
  int L;
  while (scanf("%d", &L) != EOF) {
    vector<int> v;

    char c;
    while (c = getchar(), c != '\\n' && c != '\\r' && c != EOF) {
      int a;
      scanf("%d", &a);
      v.push_back(a);
    }

    /*
                    sort( v.begin(), v.end() );

                    int n = v.size();
                    vector<int> v1, v2;
                    for (int i = 0; i < n/2; ++i)
                            v1.push_back(v[i]);

                    for (int i = n/2; i < n; ++i)
                            v2.push_back(v[i]);
    */

    int n = v.size();
    vector<vector<double>> len(n, vector<double>(n, INF));
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        len[i][j] = sqrt(d(v[i] + v[j]) - d(abs(v[i] - v[j])));

    queue<P> Q;
    for (int i = 0; i < n; ++i) {
      vector<bool> V(n);
      V[i] = true;
      Q.push(P(V, make_pair(i, v[i])));
    }

    double ans = INF;
    double cost[1 << 12][12];
    fill(&cost[0][0], &cost[0][0] + (1 << 12) * 12, INF);
    while (!Q.empty()) {
      P p = Q.front();
      Q.pop();
      int head = p.second.first;
      double length = p.second.second;

      int sz = 0;
      for (int i = 0; i < n; ++i)
        sz += p.first[i];

      if (length + v[head] > L)
        continue;
      if (sz >= n) {
        if (length + v[head] < ans)
          ans = length + v[head];

        continue;
      }
      if (length > cost[v_to_int(p.first)][head])
        continue;
      cost[v_to_int(p.first)][head] = length;

      for (int i = 0; i < n; ++i) {
        if (p.first[i])
          continue;

        vector<bool> v2 = p.first;
        v2[i] = true;
        Q.push(P(v2, make_pair(i, length + len[head][i])));
      }
    }

    cout << ans << endl;
    if (ans <= L)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }
}