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

#include <climits>
#include <cstdio>
#include <cstring>
#include <set>
#include <utility>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pii;

set<pii> S;

const int maxn = 128;
int f[maxn][maxn];

int N;

int curi[maxn], curn;
int curv[16];
int best;

inline bool in(int i, const pii &p) {
  if (i < 60)
    return ((1ll << i) & p.first) != 0;
  return ((1ll << (i - 60)) & p.second) != 0;
}

pii add(int i, const pii &p) {
  pii q = p;
  if (i < 60)
    q.first |= (1ll << i);
  else
    q.second |= (1ll << (i - 60));
  return q;
}

void search(pii p, int k) {
  if (S.count(p))
    return;
  S.insert(p);
  // printf("-- %lld\\n",p.first);
  for (int i = k; i < N; ++i) {
    if (in(i, p))
      continue;
    int j;
    curv[curn] = INT_MAX;
    for (j = 0; j < curn; ++j) {
      curv[curn] = min(curv[curn], f[i][curi[j]]);
      if (curv[curn] == 0)
        break;
    }
    if (j != curn)
      continue;
    int d[16];
    memcpy(d, curv, sizeof(d));
    int now = curv[curn];
    for (j = 0; j < curn; ++j) {
      curv[j] = min(curv[j], f[curi[j]][i]);
      now += curv[j];
    }
    curi[curn++] = i;
    if (curn > 1) {
      if (now > best)
        best = now;
    }
    /*if(curn==2){
            for(int i=0;i<curn;++i)
                    printf("%d %d\\n",curi[i],curv[i]);
    }*/
    pii q = add(i, p);
    search(q, i + 1);
    --curn;
    memcpy(curv, d, sizeof(d));
  }
}

int main() {
  int M;
  while (2 == scanf("%d%d", &N, &M)) {
    memset(f, 0, sizeof(f));
    best = 0;
    S.clear();
    while (M--) {
      int a, b, x;
      scanf("%d%d%d", &a, &b, &x);
      --a, --b;
      f[a][b] = f[b][a] = x;
    }
    curn = 0;
    search(pii(0, 0), 0);
    printf("%d\\n", best);
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

const int N = 101, INF = 1000000000;

int adj[N][N];

int n, m, opt;

int subG[N], sub_cnt;

int read() {
  if (scanf("%d%d", &n, &m) == -1)
    return 0;
  memset(adj, 0, sizeof(adj));
  for (int i = 0; i < m; i++) {
    int s, t, w;
    scanf("%d%d%d", &s, &t, &w);
    adj[s][t] = adj[t][s] = w;
  }
  return 1;
}

int check(int u) {
  for (int i = 1; i <= sub_cnt; i++) {
    if (adj[u][subG[i]] == 0)
      return 0;
  }
  return 1;
}

void update() {
  int res = 0;
  for (int i = 1; i <= sub_cnt; i++) {
    int mn = INF;
    for (int j = 1; j <= sub_cnt; j++)
      if (i ^ j) {
        if (adj[subG[i]][subG[j]] < mn)
          mn = adj[subG[i]][subG[j]];
      }
    res += mn;
  }
  if (res > opt)
    opt = res;
}

void dfs(int u) {
  if (sub_cnt > 1)
    update();
  for (int i = 1; i <= n; i++) {
    if (check(i)) {
      subG[++sub_cnt] = i;
      dfs(i);
      --sub_cnt;
    }
  }
}

void solve() {
  opt = 0;
  sub_cnt = 0;
  for (int i = 1; i <= n; i++) {
    subG[++sub_cnt] = i;
    dfs(i);
    --sub_cnt;
  }
  printf("%d\\n", opt);
}

int main() {

  //    freopen("std.in" , "r" , stdin);
  while (read()) {
    solve();
  }

  return 0;
}