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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int main() {
  unsigned i, j;
  unsigned a[100][100], b[100][11], c[100][11], m, n, o, u, v;
  pair<pair<unsigned, unsigned>, unsigned> mn;
  while (cin >> m >> n >> o >> u >> v && m + n + o + u + v) {
    u--;
    v--;
    memset(a, -1, sizeof(a));
    for (i = 0; i < o; i++) {
      int p, q, r;
      cin >> p >> q >> r;
      p--;
      q--;
      a[p][q] = a[q][p] = r;
    }
    memset(b, 0, sizeof(b));
    memset(c, -1, sizeof(c));
    c[u][0] = 0;
    mn.first.first = u;
    mn.first.second = 0;
    mn.second = 0;
    while (mn.first.first != v) {
      b[mn.first.first][mn.first.second] = 1;
      for (i = 0; i < n; i++) {
        if ((int)a[mn.first.first][i] != -1) {
          c[i][mn.first.second] =
              min(c[i][mn.first.second], mn.second + a[mn.first.first][i]);
          if (mn.first.second < m)
            c[i][mn.first.second + 1] =
                min(c[i][mn.first.second + 1],
                    mn.second + a[mn.first.first][i] / 2);
        }
      }
      mn.second = -1;
      for (i = 0; i < m + 1; i++) {
        for (j = 0; j < n; j++) {
          if (b[j][i] == 0 && mn.second > c[j][i]) {
            mn.first.first = j;
            mn.first.second = i;
            mn.second = c[j][i];
          }
        }
      }
    }
    cout << mn.second << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
int f(int a[][101], int b[], int c[], int d, int n, int p, int e, int w) {
  int h, i;
  int z[101];
  int sm, mn = -1;
  if (p == e) {
    do {
      h = 0;
      for (i = 1; i < d; i++) {
        if (c[i - 1] < c[i]) {
          swap(c[i - 1], c[i]);
          h = 1;
        }
      }
    } while (h);
    sm = 0;
    for (i = 0; i < d; i++)
      sm += i < w ? c[i] / 2 : c[i];
    return sm;
  }
  if (b[p])
    return -1;
  memcpy(z, b, sizeof(z));
  z[p] = 1;
  for (i = 1; i < n + 1; i++) {
    if (a[p][i] != -1) {
      int tmp;
      c[d] = a[p][i];
      if ((tmp = f(a, z, c, d + 1, n, i, e, w)) != -1) {
        if (mn == -1 || mn > tmp)
          mn = tmp;
      }
    }
  }
  return mn;
}
int main() {
  int h, i, j, k;
  int a[101][101], b[101], c[99], g[101], w, m, n, s, e;
  memset(b, 0, sizeof(b));
  while (cin >> w >> n >> m >> s >> e && w + n + m + s + e != 0) {
    memset(a, -1, sizeof(a));
    memset(g, 0, sizeof(g));
    for (i = 0; i < m; i++) {
      int p, q, r;
      cin >> p >> q >> r;
      a[p][q] = r;
      a[q][p] = r;
      g[p]++;
      g[q]++;
    }
    do {
      h = 0;
      for (i = 1; i < n + 1; i++) {
        if (i != s && i != e) {
          if (g[i] == 1) {
            for (j = 1; a[i][j] == -1; j++)
              ;
            a[i][j] = -1;
            a[j][i] = -1;
            g[i]--;
            g[j]--;
            h = 1;
          } else if (g[i] == 2) {
            for (j = 1; a[i][j] == -1; j++)
              ;
            for (k = j + 1; a[i][k] == -1; k++)
              ;
            if (a[j][k] != -1 && a[j][k] <= a[i][j] + a[i][k]) {
              a[i][j] = -1;
              a[j][j] = -1;
              a[i][k] = -1;
              a[k][i] = -1;
              g[i] = 0;
              g[j]--;
              g[k]--;
              h = 1;
            }
          }
        }
      }
    } while (h);
    cout << f(a, b, c, 0, n, s, e, w) << endl;
  }
  return 0;
}