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

#include <iostream>
#include <vector>

using namespace std;

#define INF 100000000

struct edge {
  int to, cost;
};

int main() {
  int c, n, m, s, d;
  while (cin >> c >> n >> m >> s >> d) {
    if (c == 0 && n == 0 && m == 0 && s == 0 && d == 0) {
      break;
    }
    vector<edge> ff[101];
    vector<vector<int>> ddd(n + 1, vector<int>(n + 1, INF));
    int a, b, f;
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> f;
      edge e;
      e.to = b;
      e.cost = f;
      ff[a].push_back(e);
      e.to = a;
      ff[b].push_back(e);
      ddd[a][b] = ddd[b][a] = f;
    }
    for (int i = 1; i <= n; i++) {
      ddd[i][i] = 0;
    }
    for (int k = 1; k <= n; k++) {
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
          ddd[i][j] = min(ddd[i][j], ddd[i][k] + ddd[k][j]);
        }
      }
    }
    vector<int> isd[101];
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (ddd[i][j] != INF) {
          isd[i].push_back(j);
        }
      }
    }
    vector<vector<vector<int>>> dp(
        n + 1, vector<vector<int>>(n + 1, vector<int>(c + 1, INF)));
    for (int i = 1; i <= n; i++) {
      dp[s][i][0] = ddd[s][i];
    }
    for (int k = 1; k <= c; k++) {
      for (int p = 0; p < isd[s].size(); p++) {
        int pp = isd[s][p];
        dp[s][pp][k] = dp[s][pp][k - 1];
        for (int i = 0; i < isd[s].size(); i++) {
          int ii = isd[s][i];

          for (int q = 0; q < ff[ii].size(); q++) {
            int qq = ff[ii][q].to;
            if (ddd[qq][pp] == INF)
              continue;
            if (dp[s][pp][k] >
                dp[s][ii][k - 1] + (ff[ii][q].cost / 2) + ddd[qq][pp]) {
              dp[s][pp][k] =
                  dp[s][ii][k - 1] + (ff[ii][q].cost / 2) + ddd[qq][pp];
            }
          }
        }
      }
    }
    cout << dp[s][d][c] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

#define INF 100000000

struct edge {
  int to, cost;
};

int main() {
  int c, n, m, s, d;
  while (cin >> c >> n >> m >> s >> d) {
    if (c == 0 && n == 0 && m == 0 && s == 0 && d == 0) {
      break;
    }
    vector<edge> ff[101];
    vector<vector<int>> ddd(n + 1, vector<int>(n + 1, INF));
    int a, b, f;
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> f;
      edge e;
      e.to = b;
      e.cost = f;
      ff[a].push_back(e);
      e.to = a;
      ff[b].push_back(e);
      ddd[a][b] = ddd[b][a] = f;
    }
    for (int i = 1; i <= n; i++) {
      ddd[i][i] = 0;
    }
    for (int k = 1; k <= n; k++) {
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
          ddd[i][j] = min(ddd[i][j], ddd[i][k] + ddd[k][j]);
        }
      }
    }
    vector<int> isd[101];
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (ddd[i][j] != INF) {
          isd[i].push_back(j);
        }
      }
    }
    vector<vector<vector<int>>> dp(
        n + 1, vector<vector<int>>(n + 1, vector<int>(c + 1, INF)));
    for (int i = 1; i <= n; i++) {
      dp[s][i][0] = ddd[s][i];
    }
    for (int k = 1; k <= c; k++) {
      for (int p = 0; p < isd[s].size(); p++) {
        int pp = isd[s][p];
        dp[s][pp][k] = dp[s][pp][k - 1];
        for (int i = 0; i < isd[s].size(); i++) {
          int ii = isd[s][i];
          for (int j = 0; j < isd[ii].size(); j++) {
            int jj = isd[ii][j];
            for (int q = 0; q < ff[jj].size(); q++) {
              int qq = ff[jj][q].to;
              if (ddd[qq][pp] == INF)
                continue;
              if (dp[s][pp][k] > dp[s][ii][k - 1] + ddd[ii][jj] +
                                     (ff[jj][q].cost / 2) + ddd[qq][pp]) {
                dp[s][pp][k] = dp[s][ii][k - 1] + ddd[ii][jj] +
                               (ff[jj][q].cost / 2) + ddd[qq][pp];
              }
            }
          }
        }
      }
    }
    cout << dp[s][d][c] << endl;
  }
  return 0;
}