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

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};

typedef long long int ll;

using namespace std;
int losen[101][101];
int d[3][101];

int n, m;

int main() {
  while (true) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    REP(i, 101) REP(j, 101) losen[i][j] = -1;
    REP(i, 3) REP(j, 101) d[i][j] = 1000000;
    REP(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      losen[a][b] = c;
      losen[b][a] = c;
    }
    queue<int> qx;
    queue<int> qd;
    queue<int> qv;
    qx.push(1);
    qd.push(2);
    d[2][1] = 0;
    while (!qx.empty()) {
      int x = qx.front();
      int dim = qd.front();
      // printf("x = %d dim = %d v = %d\\n",x,dim,v);
      qx.pop();
      qd.pop();
      FOR(i, 1, n + 1) {
        if (dim != 1 && losen[x][i] > 0 &&
            d[dim][x] + losen[x][i] < d[dim][i]) {
          d[dim][i] = d[dim][x] + losen[x][i];
          //		    printf("push: %d to %d dim = %d v =
          //%d\\n",x,i,dim,v+losen[x][i]);
          qx.push(i);
          qd.push(dim);
        }
        if (dim >= 1 && losen[x][i] > 0 && d[dim][x] < d[dim - 1][i]) {
          d[dim - 1][i] = d[dim][x];
          //		    printf("free ticket push: %d to %d dim = %d v =
          //%d\\n",x,i,dim,v);
          qx.push(i);
          qd.push(dim - 1);
        }
      }
    }
    int ans = 1000000;
    ans = min(d[0][n], d[2][n]);
    //	printf("d[0][n] = %d\\n",d[0][n]);
    //	printf("d[2][n] = %d\\n",d[2][n]);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};

typedef long long int ll;

using namespace std;
int losen[101][101];

int n, m;
int saiki(int p, vector<int> eki, vector<bool> &u) {
  int ans = 10000000;
  if (p == n) {
    ans = 0;
    FOR(i, 0, eki.size()) { ans += eki[i]; }
    if (eki.size() >= 2) {
      int temp = ans;
      REP(i, eki.size() - 1) { temp = min(temp, ans - (eki[i] + eki[i + 1])); }
      ans = temp;
    }
    return ans;
  }
  FOR(i, 1, n + 1) {
    if (losen[p][i] > 0 && !u[i]) {
      u[i] = true;
      vector<int> temp = eki;
      temp.PB(losen[p][i]);
      ans = min(ans, saiki(i, temp, u));
      u[i] = false;
    }
  }
  return ans;
}

int main() {
  while (true) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    memset(losen, -1, sizeof(losen));
    REP(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      losen[a][b] = c;
      losen[b][a] = c;
    }
    vector<bool> u(n, false);
    cout << saiki(1, vector<int>(0), u) << endl;
    ;
  }
  return 0;
}