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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = 123456789;

int N, M, E, S, T, R;
int ea[10], eb[10], ec[10];
int dp[1 << 8][8][110];
int dist[1 << 8][100][100];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  cin >> N >> M >> E >> S >> T >> R;
  fill((int *)dp, (int *)dp + (1 << 8) * 8 * 110, INF);
  fill((int *)dist, (int *)dist + (1 << 8) * 100 * 100, INF);
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    dist[0][a][b] = dist[0][b][a] = 1;
  }
  REP(i, N)
  dist[0][i][i] = 0;
  REP(i, E) {
    cin >> ea[i] >> eb[i] >> ec[i];
    if (ec[i] == S)
      dist[0][ea[i]][eb[i]] = dist[0][eb[i]][ea[i]] = 1;
  }
  REP(k, N)
  REP(i, N) REP(j, N) dist[0][i][j] = dist[0][j][i] =
      min(dist[0][i][j], dist[0][i][k] + dist[0][k][j]);

  const int MAX = 1 << E;
  REP(bi, MAX) REP(i, N) REP(j, N) dist[bi][i][j] = dist[0][i][j];

  REP(bi, MAX) {
    REP(k, E) {
      if (bi >> k & 1)
        continue;
      int a = ea[k], b = eb[k];
      dist[bi][a][b] = dist[bi][b][a] = 1;
      REP(i, N) REP(j, N) {
        int t = min(dist[bi][i][a] + 1 + dist[bi][b][j],
                    dist[bi][i][b] + 1 + dist[bi][a][j]);
        if (t < dist[bi][i][j])
          dist[bi][i][j] = dist[bi][j][i] = t;
      }
    }
  }

  int ans = INF;
  if (dist[MAX - 1][S][T] <= R)
    ans = dist[MAX - 1][S][T];
  dp[MAX - 1][0][0] = 0;
  REP(x, E) {
    int bit = (MAX - 1) ^ (1 << x);
    int r = dist[MAX - 1][S][ec[x]];
    if (r <= R)
      dp[bit][x][r] = r;
  }

  for (int bit = MAX - 1; bit >= 0; --bit) {
    REP(x, E) {
      if (bit >> x & 1)
        continue;
      int now = ec[x];
      REP(r, R + 1) {
        if (dp[bit][x][r] >= INF)
          continue;
        REP(i, E) {
          if (!(bit >> i & 1))
            continue;
          int nxt_bit = bit ^ (1 << i);
          int to = ec[i];
          int nxt_r = dist[bit][now][to] + r;
          if (nxt_r <= R) {
            dp[nxt_bit][i][nxt_r] =
                min(dp[nxt_bit][i][nxt_r], dp[bit][x][r] + dist[bit][now][to]);
          }
          nxt_r = dist[bit][S][to];
          if (nxt_r <= R) {
            dp[nxt_bit][i][nxt_r] =
                min(dp[nxt_bit][i][nxt_r], dp[bit][x][r] + 1 + nxt_r);
          }
        }
        if (r + dist[bit][now][T] <= R)
          ans = min(ans, dp[bit][x][r] + dist[bit][now][T]);
        if (dist[bit][S][T] <= R)
          ans = min(ans, dp[bit][x][r] + 1 + dist[bit][S][T]);
      }
    }
  }

  cout << (ans >= INF ? -1 : ans) << endl;

  return 0;
}

Error: TLE

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = 123456789;

int N, M, E, S, T, R;
int dist[101][101];
int ea[10], eb[10], ec[10];

int dfs(queue<int> &q, int n, int r) {
  int res = (dist[n][T] + r > R ? INF : dist[n][T]);
  if (q.empty()) {
    if (n != S)
      res = min(res, 1 + dfs(q, S, 0));
    return res;
  }

  vector<bool> used(E, false);
  while (!used[q.front()]) {
    used[q.front()] = true;
    int idx = q.front();
    q.pop();
    int a = ea[idx], b = eb[idx], c = ec[idx];
    int nc = dist[n][c];
    if (nc + r > R) {
      q.push(idx);
      if (n != S)
        res = min(res, 1 + dfs(q, S, 0));
      continue;
    }
    VVI prv(N, VI(N));
    REP(i, N) REP(j, N) prv[i][j] = dist[i][j];

    dist[a][b] = dist[b][a] = 1;
    REP(i, N) REP(j, N) {
      int t = min(dist[i][a] + 1 + dist[b][j], dist[i][b] + 1 + dist[a][j]);
      if (t < dist[i][j])
        dist[i][j] = dist[j][i] = t;
    }

    int ST = min(INF, nc + dfs(q, c, nc + r));
    q.push(idx);
    REP(i, N) REP(j, N) dist[i][j] = prv[i][j];
    res = min(res, ST);
  }
  return res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  cin >> N >> M >> E >> S >> T >> R;
  REP(i, N) REP(j, N) dist[i][j] = INF;
  REP(i, N)
  dist[i][i] = 0;
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    dist[a][b] = dist[b][a] = 1;
  }

  queue<int> q;
  REP(i, E) {
    cin >> ea[i] >> eb[i] >> ec[i];
    if (ec[i] == S)
      dist[ea[i]][eb[i]] = dist[eb[i]][ea[i]] = 1;
    else if (ec[i] != T)
      q.push(i);
  }
  REP(k, N)
  REP(i, N) REP(j, N) dist[i][j] = dist[j][i] =
      min(dist[i][j], dist[i][k] + dist[k][j]);

  int ans = dfs(q, S, 0);
  cout << (ans >= INF ? -1 : ans) << endl;

  return 0;
}