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

#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstring>
using namespace std;

int s[100][100];

int main() {
  int n, m, x, y, z, ans, mind, mini;
  int d[100], a[100];
  bool b[100];

  while (1) {
    scanf("%d", &n);
    if (!n)
      break;
    scanf("%d", &m);
    memset(s, 0, sizeof(int) * n * 100);
    for (int i = 0; i < m; i++) {
      scanf("%d,%d,%d", &x, &y, &z);
      s[x][y] = s[y][x] = z / 100 - 1;
    }

    for (int i = 0; i < n; i++)
      d[i] = INT_MAX;
    memset(b, 0, sizeof(bool) * n);
    d[0] = 0;
    a[0] = 0;
    ans = 0;
    while (1) {
      mind = INT_MAX;
      for (int i = 0; i < n; i++) {
        if (!b[i] && d[i] < mind) {
          mind = d[i];
          mini = i;
        }
      }
      if (mind == INT_MAX)
        break;
      ans += s[a[mini]][mini];
      b[mini] = true;
      for (int i = 0; i < n; i++) {
        if (!b[i] && s[mini][i]) {
          if (d[i] > s[mini][i]) {
            d[i] = s[mini][i];
            a[i] = mini;
          }
        }
      }
    }
    printf("%d\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstring>
using namespace std;

int n;
int s[100][100];
bool b[100];
bool c[100];
int ans;
int cc;

void dfs(int x, int y, int sum) {
  /*for (int i=0; i<n; i++) {
    printf("%d ", b[i]);
  }
  printf("\\n");*/
  // printf("%d %d %d %d [%d %d %d %d]\\n", x, y, sum, cc, c[0], c[1], c[2],
  // c[3]);
  if (s[x][y] && !b[y] && !c[y]) {
    // printf("use %d-%d\\n", x, y);
    if (cc == n - 1) {
      // printf("%d\\n", sum+s[x][y]);
      ans = min(ans, sum + s[x][y]);
    } else {
      b[y] = true;
      ++cc;
      if (y < n - 1)
        dfs(x, y + 1, sum + s[x][y]);
      else {
        bool ct[n];
        // printf("save %d %d %d %d\\n", c[0], c[1], c[2], c[3]);
        memcpy(ct, c, sizeof(bool) * n);
        for (int i = 0; i < n; i++) {
          if (!c[i] && b[i]) {
            c[i] = true;
            // printf("check %d start\\n", i);
            dfs(i, 0, sum + s[x][y]);
            // printf("check %d end\\n", i);
          }
        }
        // printf("revert %d %d %d %d\\n", ct[0], ct[1], ct[2], ct[3]);
        memcpy(c, ct, sizeof(bool) * n);
      }
      b[y] = false;
      --cc;
    }
    // printf("unuse %d-%d\\n", x, y);
  }
  if (y < n - 1)
    dfs(x, y + 1, sum);
  else {
    bool ct[n];
    // printf("save %d %d %d %d\\n", c[0], c[1], c[2], c[3]);
    memcpy(ct, c, sizeof(bool) * n);
    for (int i = 0; i < n; i++) {
      if (!c[i] && b[i]) {
        c[i] = true;
        // printf("check %d start\\n", i);
        dfs(i, 0, sum);
        // printf("check %d end\\n", i);
      }
    }
    // printf("revert %d %d %d %d\\n", ct[0], ct[1], ct[2], ct[3]);
    memcpy(c, ct, sizeof(bool) * n);
  }
}

int main() {
  int m, x, y, z;

  while (1) {
    scanf("%d", &n);
    if (!n)
      break;
    memset(b, 0, sizeof(bool) * n);
    memset(c, 0, sizeof(bool) * n);
    memset(s, 0, sizeof(int) * n * 100);
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
      scanf("%d,%d,%d", &x, &y, &z);
      s[x][y] = s[y][x] = z / 100 - 1;
    }
    b[0] = true;
    c[0] = true;
    ans = INT_MAX;
    cc = 1;
    dfs(0, 0, 0);
    printf("%d\\n", ans);
  }
}