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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.00000001
using namespace std;

struct Info {
  double r;
};

double ***dp;

void func(char buf[300]) {

  double length;
  int tmp;

  int index;

  tmp = 0;
  for (index = 0; buf[index] != ' ' && buf[index] != '\\0'; index++) {
    tmp = 10 * tmp + buf[index] - '0';
  }

  length = (double)tmp;

  Info info[15];
  int info_index = 0;

  while (buf[index] != '\\0') {

    tmp = 0;
    for (++index; buf[index] != ' ' && buf[index] != '\\0'; index++) {
      tmp = 10 * tmp + buf[index] - '0';
    }
    info[info_index].r = (double)tmp;
    info_index++;
  }

  double sum = 0;

  for (int i = 0; i < info_index; i++) {
    sum += info[i].r;
  }

  if (length > 2 * sum + EPS) {
    printf("OK\\n");
    return;
  }

  double distance[info_index][info_index];

  for (int i = 0; i < info_index; i++) {
    for (int k = 0; k < info_index; k++) {
      if (i != k) {
        distance[i][k] = sqrt(
            (info[i].r + info[k].r) * (info[i].r + info[k].r) -
            (info[i].r - info[k].r) * (info[i].r - info[k].r)); // i??????k???
      }
    }
  }

  int limit = pow(2, info_index);

  double minimum = 4000.0;

  for (int start_town = 0; start_town < info_index; start_town++) {

    for (int i = 0; i <= info_index; i++) {
      for (int k = 0; k < info_index; k++) {
        for (int p = 0; p < limit; p++)
          dp[i][k][p] = 3000.0;
      }
    }

    int next_state;

    next_state = pow(2, start_town);

    dp[0][start_town][next_state] = info[start_town].r;

    for (int i = 1; i <= info_index; i++) {           //?????°????????????
      for (int k = 0; k < info_index; k++) {          //???????????????
        for (int state = 0; state < limit; state++) { //??¶???????????????
          if (dp[i - 1][k][state] < 2000.0) {
            for (int a = 0; a < info_index; a++) {
              if (state & (1 << a)) { //???a?????¢????????£???????????´???
                if (i == info_index && a == start_town && state == limit - 1) {
                  dp[i][start_town][limit - 1] =
                      min(dp[i][start_town][limit - 1],
                          dp[i - 1][k][state] + info[k].r);
                }
              } else {
                next_state = state + pow(2, a);
                dp[i][a][next_state] = min(
                    dp[i][a][next_state], dp[i - 1][k][state] + distance[k][a]);
              }
            }
          }
        }
      }
    }

    minimum = min(minimum, dp[info_index][start_town][limit - 1]);
  }

  if (minimum < length || fabs(minimum - length) < EPS) {
    printf("OK\\n");
  } else {
    printf("NA\\n");
  }
}

int main() {

  dp = new double **[13];

  for (int i = 0; i < 13; i++) {
    dp[i] = new double *[12];
    for (int k = 0; k < 12; k++) {
      dp[i][k] = new double[4096];
    }
  }

  char buf[300];
  int index;
  char ch;

  while (true) {
    index = 0;
    ch = getchar();

    if (ch == '\\n' || ch == '\\0' || ch == EOF)
      break;
    // if(ch == EOF)break;

    while (ch != '\\n' && ch != '\\0' && ch != EOF) {
      buf[index++] = ch;
      ch = getchar();
    }
    buf[index] = '\\0';
    func(buf);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
using namespace std;

struct Info {
  Info() { r = -1.0; }

  double r;
};

double ***dp;

void func(char buf[100]) {

  printf("DEBUG\\n");
  return;

  double length;
  int tmp;

  int index;

  tmp = 0;
  for (index = 0; buf[index] != ' ' && buf[index] != '\\0'; index++) {
    tmp = 10 * tmp + buf[index] - '0';
  }

  length = (double)tmp;

  Info info[15];
  int info_index = 0;

  while (buf[index] != '\\0') {

    tmp = 0;
    for (++index; buf[index] != ' ' && buf[index] != '\\0'; index++) {
      tmp = 10 * tmp + buf[index] - '0';
    }
    info[info_index].r = (double)tmp;
    info_index++;
  }

  double sum = 0;

  for (int i = 0; i < info_index; i++) {
    sum += info[i].r;
  }

  if (length > 2 * sum) {
    printf("OK\\n");
    return;
  }

  double div, left, right, m, calcDist;

  double distance[info_index][info_index];

  for (int i = 0; i < info_index; i++) {
    for (int k = 0; k < info_index; k++) {
      if (i != k) {

        left = info[i].r;
        right = 2.0 * info[i].r + info[k].r;
        m = (left + right) / 2.0;

        while (left <= right) {
          calcDist = sqrt((m - info[i].r) * (m - info[i].r) +
                          (info[i].r - info[k].r) * (info[i].r - info[k].r));

          if (fabs(calcDist - (info[i].r + info[k].r)) < 0.00001) {
            div = m + info[k].r - info[i].r * 2.0;
            break;
          } else {
            if (calcDist + 0.00001 < info[i].r + info[k].r) {
              left = m + 0.00001;
            } else {
              right = m - 0.00001;
            }
          }
          m = (left + right) / 2.0;
        }
        distance[i][k] = div;
      }
    }
  }

  int limit = pow(2, info_index);

  for (int i = 0; i <= info_index; i++) {
    for (int k = 0; k < info_index; k++) {
      for (int p = 0; p < limit; p++)
        dp[i][k][p] = (double)BIG_NUM;
    }
  }

  dp[0][0][1] = info[0].r * 2.0;

  int next_state;

  for (int i = 1; i <= info_index; i++) {
    for (int k = 0; k < info_index; k++) {
      for (int state = 0; state < limit; state++) {
        if (dp[i - 1][k][state] != (double)BIG_NUM) {
          for (int a = 0; a < info_index; a++) {
            if (state & (1 << a)) {
              if (i == info_index) {
                dp[info_index][0][limit - 1] =
                    min(dp[info_index][0][limit - 1], dp[i - 1][k][state]);
              }
            } else {
              next_state = state + pow(2, a);
              dp[i][a][next_state] = min(dp[i][a][next_state],
                                         dp[i - 1][k][state] + distance[k][a]);
            }
          }
        }
      }
    }
  }

  if (dp[info_index][0][limit - 1] <= length) {
    printf("OK\\n");
  } else {
    printf("NA\\n");
  }
}

int main() {

  dp = new double **[13];

  for (int i = 0; i < 13; i++) {
    dp[i] = new double *[12];
    for (int k = 0; k < 12; k++) {
      dp[i][k] = new double[4096];
    }
  }

  char buf[100];
  int index;
  char ch;

  while (true) {
    index = 0;
    ch = getchar();

    if (ch == '\\n' || ch == '\\0')
      break;

    while (ch != '\\n' && ch != '\\0') {
      buf[index++] = ch;
      ch = getchar();
    }
    buf[index] = '\\0';
    func(buf);
  }

  return 0;
}