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

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

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;

typedef vector<bool> VB;
typedef vector<VB> VVB;

#define fst first;
#define snd second;
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(x, y, w, h) (0 <= (x) && 0 <= (y) && (x) < (w) && (y) < (h))
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, init, n) for (int i = (init); i < (n); i++)

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

int N, M;
int R;
VI rs;

VVI seas;
VVI loads;

int solve() {
  // ????????????
  rep(k, N) {
    rep(i, N) {
      rep(j, N) {
        if (i == j || i == k || k == j)
          continue;
        seas[i][j] = min(seas[i][j], seas[i][k] + seas[k][j]);
        loads[i][j] = min(loads[i][j], loads[i][k] + loads[k][j]);
      }
    }
  }
  // 8,000,000

  //    cout << "????????????ok!" << endl;

  // dp[ri][where]
  VVI dp(R, VI(N, INF));
  dp[0][rs[0]] = 0;

  rep(ri, R - 1) { // 1000
    rep(w, N) {    // 200
      if (dp[ri][w] == INF)
        continue;

      int s = rs[ri];
      int g = rs[ri + 1];

      dp[ri + 1][w] = min(dp[ri + 1][w], dp[ri][w] + loads[s][g]);

      int cost = dp[ri][w] + loads[s][w];
      rep(w2, N) { // 200
        int cost2 = cost;
        cost2 += seas[w][w2];
        cost2 += loads[w2][g];
        dp[ri + 1][w2] = min(dp[ri + 1][w2], cost2);
      }
    }
  }
  // 40,000,000

  return *min_element(ALL(dp[R - 1]));
}

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

  while (cin >> N >> M, N) {
    seas = VVI(N, VI(N, INF));
    loads = VVI(N, VI(N, INF));
    rep(i, N) rep(j, N) {
      if (i != j)
        continue;
      seas[i][j] = 0;
      loads[i][j] = 0;
    }

    rep(i, M) {
      int x, y, t;
      char sl;
      cin >> x >> y >> t >> sl;
      --x;
      --y;

      bool is_sea = sl == 'S';

      (is_sea ? seas : loads)[x][y] = t;
      (is_sea ? seas : loads)[y][x] = t;
    }

    cin >> R;
    rs = VI(R);
    for (auto &r : rs) {
      cin >> r;
      --r;
    }

    cout << solve() << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;

typedef vector<bool> VB;
typedef vector<VB> VVB;

#define fst first;
#define snd second;
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(x, y, w, h) (0 <= (x) && 0 <= (y) && (x) < (w) && (y) < (h))
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, init, n) for (int i = (init); i < (n); i++)

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

int N, M;

struct Edge {
  Edge(){};
  Edge(int t, int c, bool s) : to(t), cost(c), is_sea(s){};
  int to, cost;
  bool is_sea;
};

vector<vector<Edge>> es;

int R;
VI rs;

struct State {
  int v, where, ri, cost;
  bool operator>(const State &s) const { return cost < s.cost; }
};

int solve() {
  // costs[v][where][ri] = min_cost
  vector<VVI> costs(N, VVI(N, VI(R + 1, INF)));

  priority_queue<State, vector<State>, greater<State>> q;
  {
    State s;
    s.v = rs[0];
    s.cost = 0;
    s.where = rs[0];
    s.ri = 1;
    q.push(s);
    costs[s.v][s.where][s.ri] = 0;
  }

  while (q.size()) {
    State cur = q.top();
    q.pop();

    if (cur.ri == R)
      continue;
    if (cur.cost > costs[cur.v][cur.where][cur.ri])
      continue;

    for (auto &e : es[cur.v]) {
      State n;
      n.cost = cur.cost + e.cost;
      n.ri = cur.ri + (rs[cur.ri] == e.to);
      n.v = e.to;

      if (e.is_sea) {
        if (cur.where == cur.v) {
          n.where = n.v;
        } else {
          continue;
        }
      } else {
        n.where = cur.where;
      }

      if (costs[n.v][n.where][n.ri] > n.cost) {
        costs[n.v][n.where][n.ri] = n.cost;
        q.push(n);
      }
    }
  }

  // costs[rs[R - 1]][*][R]
  int ans = INF;
  rep(v, N) { ans = min(ans, costs[rs[R - 1]][v][R]); }

  return ans;
}

int main(void) {
  while (cin >> N >> M, N) {
    es.clear();
    es.resize(N);

    rep(i, M) {
      int x, y, t;
      char sl;
      cin >> x >> y >> t >> sl;
      --x;
      --y;
      es[x].EB(y, t, sl == 'S');
      es[y].EB(x, t, sl == 'S');
    }

    cin >> R;
    rs = VI(R);
    for (auto &r : rs) {
      cin >> r;
      --r;
    }

    cout << solve() << endl;
  }

  return 0;
}