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

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int n;
int d[10][10];

bool bound(int k, long long *length, bool used[], int home[], long long c[],
           const int n) {
  bool res = false;
  long long lb; // ??????????????¢?´¢?????????
  int t = home[k - 1];
  lb = c[k - 1];

  int m = 0, mm = 99999;
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      m = max(m, d[t][i]);
      // mm = min(mm, d[t][i]);
    }
  }
  if (lb + max(m, n - k + 1) >= *length && *length > 0)
    res = true; // ????????????????????§??????????§£??????????????´???
  return res;
}

void dfs(int k, long long *length, bool used[], int home[], long long c[],
         const int n) {
  if (k == n) {
    // for (int i=0;i<n;i++) cout << home[i]; cout << ' ' << c[n-1] << endl;
    if (*length > c[n - 1] || *length == 0)
      *length = c[n - 1];
    return;
  } else if (k > 0 && *length > 0 && bound(k, length, used, home, c, n))
    return;
  else {
    for (int i = 0; i < n; i++) {
      if (!used[i]) {
        used[i] = true;
        home[k] = i;
        if (k == 0)
          c[k] = 0;
        else {
          c[k] = c[k - 1] + d[home[k]][home[k - 1]];
          for (int j = k - 2; j >= 0; j--)
            c[k] = max(c[k], c[j] + d[home[k]][home[j]]);
        }
        dfs(k + 1, length, used, home, c, n);
        used[i] = false;
      }
    }
  }
  return;
}

int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      return 0;

    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        cin >> d[i][j];
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        d[i][j] = max(d[i][j], d[j][i]);

    long long length = 0;
    bool used[10] = {false, false, false, false, false,
                     false, false, false, false, false};
    int home[10];
    long long c[10];

    dfs(0, &length, used, home, c, n);
    cout << length << endl;
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int n;
int d[10][10];

bool bound(int k, int *length, bool used[], int home[], int c[], const int n) {
  bool res = false;
  int len = c[k - 1];
  int t = home[k - 1];
  for (int i = 0; i < n; i++) {
    if (!used[i])
      t += d[t][i];
    t = i;
  }
  if (t >= *length && *length > 0)
    res = true;
  return res;
}

void dfs(int k, int *length, bool used[], int home[], int c[], const int n) {
  if (k == n) {
    // for (int i=0;i<n;i++) cout << home[i]; cout << endl;
    // cout << c[n-1] << endl;
    if (n == 1)
      *length = c[n - 1];
    else {
      if (*length > c[n - 1] || *length == 0)
        *length = c[n - 1];
    }
    return;
  } else if (bound(k, length, used, home, c, n))
    return;
  else {
    for (int i = 0; i < n; i++) {
      if (!used[i]) {
        used[i] = true;
        home[k] = i;
        if (k == 0)
          c[k] = 0;
        else {
          c[i] = c[i - 1] + d[home[i]][home[i - 1]];
          for (int j = i - 2; j >= 0; j--)
            c[i] = max(c[i], c[j] + d[home[i]][home[j]]);
        }
        dfs(k + 1, length, used, home, c, n);
        used[i] = false;
      }
    }
  }
  return;
}

int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      return 0;

    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        cin >> d[i][j];
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        d[i][j] = max(d[i][j], d[j][i]);

    int length = 0;
    bool used[10] = {false, false, false, false, false,
                     false, false, false, false, false};
    int home[10];
    int c[10];

    dfs(0, &length, used, home, c, n);
    cout << length << endl;
  }
}