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

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

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
bool used[10];
int parm[10];
int N;
int a[10][10];
int dist[10];
int ans;
void dfs(int idx) {
  if (idx == N) {
    ans = dist[idx - 1];
    return;
  }
  for (int i = 0; i < N; i++)
    if (!used[i]) {
      parm[idx] = i;
      used[i] = true;
      dist[idx] = 0;
      for (int j = 0; j < idx; j++) {
        dist[idx] = max(dist[idx], dist[j] + a[parm[j]][parm[idx]]);
      }
      if (dist[idx] < ans)
        dfs(idx + 1);
      used[i] = false;
    }
}

int main() {
  while (cin >> N && N) {
    REP(i, N) {
      REP(j, N) { cin >> a[i][j]; }
    }
    REP(i, N) REP(j, N) a[i][j] = max(a[i][j], a[j][i]);
    memset(used, 0, sizeof(used));
    ans = INF;
    dfs(0);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
  int N;
  while (cin >> N && N) {
    int a[10][10] = {};
    REP(i, N) {
      REP(j, N) { cin >> a[i][j]; }
    }
    REP(i, N) REP(j, N) a[i][j] = max(a[i][j], a[j][i]);
    /*
    do{
      //debug(parm.begin(), parm.end());
      vector<int> dist(N);
      for(int i = 1; i < N; i++){
        int u = parm[i];
        for(int j = 0; j < i; j++){
          int v = parm[j];
          dist[u] = max(dist[u], dist[v] + a[u][v]);
        }
        if(dist[u] >= ans){
          //printf("wrong %d ", i);
          sort(parm.begin() + i + 1, parm.end(), greater<int>());
          dist[parm[N - 1]] = INF;
          break;
        }
      }
      //cout << ": " << dist[parm[N - 1]] << endl;
      ans = min(ans, dist[parm[N - 1]]);
    }while(next_permutation(parm.begin(), parm.end()));
    */
    int ans = INF;
    vector<int> parm(N);
    REP(i, N) parm[i] = i;
    REP(iter, 10000000 / N / N) {
      random_shuffle(parm.begin(), parm.end());
      vector<int> dist(N);
      for (int i = 1; i < N; i++) {
        int u = parm[i];
        for (int j = 0; j < i; j++) {
          int v = parm[j];
          dist[u] = max(dist[u], dist[v] + a[u][v]);
        }
        if (dist[u] >= ans) {
          // printf("wrong %d ", i);
          dist[parm[N - 1]] = INF;
          break;
        }
      }
      ans = min(ans, dist[parm[N - 1]]);
    }
    cout << ans << endl;
  }
  return 0;
}