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

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define repr(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x " = " << x << endl
#define print(x) cout << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<int, P> PIP;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;
typedef set<int> S;

#define INF INT_MAX / 4
#define MAX_N 1000000001

int main() {
  cin.sync_with_stdio(false);
  int n, m, r;
  while (cin >> n >> m, n | m) {
    int d[n][n][2];
    rep(i, n) rep(j, n) d[i][j][0] = d[i][j][1] = (i != j) ? INF : 0;

    map<int, int> sea;
    int cnt = 0;
    rep(i, m) {
      int x, y, t;
      char sl;
      cin >> x >> y >> t >> sl;
      x--, y--;
      if (t < d[x][y][sl == 'S'])
        d[x][y][sl == 'S'] = d[y][x][sl == 'S'] = t;
      if (sl == 'S') {
        if (!sea.count(x))
          sea[x] = cnt++;
        if (!sea.count(y))
          sea[y] = cnt++;
      }
    }

    cin >> r;
    vector<int> z(r);
    rep(i, r) cin >> z[i], z[i]--;

    rep(i, n) rep(j, n) rep(k, n) {
      minch(d[j][k][0], d[j][i][0] + d[i][k][0]);
      minch(d[j][k][1], d[j][i][1] + d[i][k][1]);
    }

    if (cnt > 0) {
      int dp[r][cnt];
      rep(i, r) rep(j, cnt) dp[i][j] = INF;
      dp[0][sea[z[0]]] = 0;
      repl(i, 1, r) {
        each(j, sea) {
          minch(dp[i][j.se], dp[i - 1][j.se] + d[z[i - 1]][z[i]][0]);
          each(k, sea) {
            minch(dp[i][j.se], dp[i - 1][k.se] + d[z[i - 1]][k.fi][0] +
                                   d[k.fi][j.fi][1] + d[j.fi][z[i]][0]);
          }
        }
      }
      int ans = INF;
      rep(i, cnt) minch(ans, dp[r - 1][i]);
      cout << ans << endl;
    } else {
      int ans = 0;
      rep(i, r - 1) { ans += d[z[i]][z[i + 1]][0]; }
      cout << ans << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define repr(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x " = " << x << endl
#define print(x) cout << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<int, P> PIP;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;
typedef set<int> S;

#define INF INT_MAX / 3
#define MAX_N 1000000001

struct state {
  int v, c, p;
};

bool operator<(const state &a, const state &b) { return a.c > b.c; }

int main() {
  cin.sync_with_stdio(false);
  int n, m, r;
  while (cin >> n >> m, n | m) {
    int d[n][n][2];
    rep(i, n) rep(j, n) d[i][j][0] = d[i][j][1] = INF;

    map<int, int> sea;
    int cnt = 0;
    rep(i, m) {
      int x, y, t;
      char sl;
      cin >> x >> y >> t >> sl;
      x--, y--;
      if (t < d[x][y][sl == 'S'])
        d[x][y][sl == 'S'] = d[y][x][sl == 'S'] = t;
      if (sl == 'S') {
        if (!sea.count(x))
          sea[x] = cnt++;
        if (!sea.count(y))
          sea[y] = cnt++;
      }
    }

    cin >> r;
    vector<int> z(r);
    rep(i, r) cin >> z[i], z[i]--;

    int dp[n][sea.size()];
    rep(i, n) rep(j, sea.size()) dp[i][j] = INF;
    dp[z[0]][sea[z[0]]] = 0;

    bool visited[n][sea.size()];
    priority_queue<state> q;
    repl(i, 1, r) {
      memset(visited, 0, sizeof(visited));
      rep(j, sea.size()) {
        if (dp[z[i - 1]][j] < INF)
          q.push(state{z[i - 1], dp[z[i - 1]][j], j});
      }
      rep(i, n) rep(j, sea.size()) dp[i][j] = INF;

      while (!q.empty()) {
        state s = q.top();
        q.pop();

        if (visited[s.v][s.p])
          continue;
        else
          visited[s.v][s.p] = true;

        dp[s.v][s.p] = s.c;
        if (s.v == z[i])
          continue;

        rep(i, n) {
          if (d[s.v][i][0] < INF && !visited[i][s.p]) {
            q.push(state{i, s.c + d[s.v][i][0], s.p});
          }
          if (d[s.v][i][1] < INF && sea[s.v] == s.p && !visited[i][sea[i]]) {
            q.push(state{i, s.c + d[s.v][i][1], sea[i]});
          }
        }
      }
    }

    int mini = INF;
    rep(i, sea.size()) minch(mini, dp[z[r - 1]][i]);
    cout << mini << endl;
  }

  return 0;
}