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

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

#define INF (int)1e09

using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::list;
using std::make_pair;
using std::map;
using std::max;
using std::min;
using std::pair;
using std::priority_queue;
using std::queue;
using std::set; // insert erase find count
using std::setprecision;
using std::stack;
using std::string;
using std::vector;

template <typename T> using Vec = vector<T>; // c++11
typedef pair<int, int> PII;
typedef pair<int, pair<int, int>> PIII;

int n;
int m;

struct Coord {
  double l;
  double a;
  double b;
};

double d_abs(double target) {
  if (target < 0) {
    target *= (-1);
  }
  return target;
}

double act(int temp, Vec<Vec<double>> &dist, Vec<double> &dp, Vec<int> &edge) {
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    dp[1 << i] = 0;
    edge[1 << i] = 1;
  }

  for (int i = 0; i < pow(2, n); i++) {
    dp[i] = 0;
    for (int j = 0; j < n; j++) {
      if ((i & (1 << j)) != 0) {
        edge[i] = edge[i - (1 << j)] + 1;
      }
    }
    for (int j = 0; j < n; j++) {
      if ((i & (1 << j)) != 0) {
        double add_dist = 0;
        for (int k = 0; k < n; k++) {
          if ((i & (1 << k)) != 0) {
            add_dist += dist[j][k];
          }
        }
        dp[i] = max(dp[i], dp[i - (1 << j)] + add_dist);
      }
    }
  }

  double max_ans = 0;
  for (int i = 0; i < pow(2, n); i++) {
    if (edge[i] == m) {
      max_ans = max(max_ans, dp[i]);
    }
  }
  return max_ans;
}

int main(void) {
  cin >> n >> m;

  Vec<Coord> list(n);
  for (int i = 0; i < n; i++) {
    cin >> list[i].l >> list[i].a >> list[i].b;
  }
  Vec<Vec<double>> dist(n, Vec<double>(n));
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      double dl = d_abs(list[i].l - list[j].l);
      double da = d_abs(list[i].a - list[j].a);
      double db = d_abs(list[i].b - list[j].b);
      dist[i][j] = dl * dl + da * da + db * db;
      dist[j][i] = dist[i][j];
    }
  }

  Vec<double> dp((pow(2, n)));
  Vec<int> edge_num((pow(2, n)));
  cout << fixed << setprecision(6) << act(n, dist, dp, edge_num) << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

#define INF (int)1e09

using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::list;
using std::make_pair;
using std::map;
using std::max;
using std::min;
using std::pair;
using std::priority_queue;
using std::queue;
using std::set; // insert erase find count
using std::setprecision;
using std::stack;
using std::string;
using std::vector;

template <typename T> using Vec = vector<T>; // c++11
typedef pair<int, int> PII;
typedef pair<int, pair<int, int>> PIII;

int n;
int m;

struct Coord {
  double l;
  double a;
  double b;
};

double d_abs(double target) {
  if (target < 0) {
    target *= (-1);
  }
  return target;
}

double recur(int temp, Vec<Vec<double>> &dist, Vec<bool> &check) {
  double dist_max = 0;
  double now_dist;
  if (temp == 1) {
    return 0;
  } else {
    for (int i = 0; i < n; i++) {
      if (check[i] == true) {
        check[i] = false;
        if (temp > m) {
          now_dist = recur(temp - 1, dist, check);
        } else {
          double dist_add = 0;
          for (int j = 0; j < n; j++) {
            if (check[j] == true) {
              dist_add += dist[i][j];
            }
          }

          now_dist = dist_add + recur(temp - 1, dist, check);
        }
        check[i] = true;
        dist_max = max(dist_max, now_dist);
      }
    }
    return dist_max;
  }
  cout << "failed" << endl;
  return -1;
}

int main(void) {
  cin >> n >> m;

  Vec<Coord> list(n);
  for (int i = 0; i < n; i++) {
    cin >> list[i].l >> list[i].a >> list[i].b;
  }
  Vec<Vec<double>> dist(n, Vec<double>(n));
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      double dl = d_abs(list[i].l - list[j].l);
      double da = d_abs(list[i].a - list[j].a);
      double db = d_abs(list[i].b - list[j].b);
      dist[i][j] = dl * dl + da * da + db * db;
      dist[j][i] = dist[i][j];
    }
  }
  Vec<bool> check(n, true);
  cout << fixed << setprecision(6) << recur(n, dist, check) << endl;
  return 0;
}