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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define stoi stoll
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
#define ALL(c) begin(c), end(c)
#define RALL(c) rbegin(c), rend(c)
#define ITR(i, b, e) for (auto i = (b); i != (e); ++i)
#define FORE(x, c) for (auto &x : c)
#define REPF(i, a, n) for (int i = a, i##len = (int)(n); i < i##len; ++i)
#define REP(i, n) REPF(i, 0, n)
#define REPR(i, n) for (int i = (int)(n); i >= 0; --i)
#define SZ(c) ((int)c.size())
#define CONTAIN(c, x) (c.find(x) != end(c))
#define OUTOFRANGE(y, x, h, w) ((y) < 0 || (x) < 0 || (y) >= (h) || (x) >= (w))
#define dump(...)
const signed INF_ = 1001001001;
const long long INF = 1001001001001001001LL;
const int DX[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0},
          DY[9] = {-1, 0, 1, 0, -1, 1, 1, -1, 0};
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  ITR(i, begin(v), end(v)) os << *i << (i == end(v) - 1 ? "" : "\\n");
  return os;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  ITR(i, begin(v), end(v)) is >> *i;
  return is;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T, class U> bool chmax(T &a, const U &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T, class U> bool chmin(T &a, const U &b) {
  return a > b ? a = b, 1 : 0;
}
template <class T> using heap = priority_queue<T, vector<T>, greater<T>>;
struct {
  template <class T> operator T() {
    T x;
    cin >> x;
    return x;
  }
} IN;
struct before_main_function {
  before_main_function() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(15) << fixed;
    // #define endl "\\n"
  }
} before_main_function;
//------------------8<------------------------------------8<--------------------

void solve(int N, int M) {
  vector<vector<int>> gl(N, vector<int>(N, INF));
  vector<vector<int>> gs(N, vector<int>(N, INF));
  REP(i, M) {
    int x, y, t;
    char ls;
    cin >> x >> y >> t >> ls;
    x--, y--;
    if (ls == 'L') {
      gl[x][y] = gl[y][x] = t;
    } else {
      gs[x][y] = gs[y][x] = t;
    }
  }

  REP(i, N) gl[i][i] = gs[i][i] = 0;
  REP(i, N) REP(j, N) REP(k, N) chmin(gl[j][k], gl[j][i] + gl[i][k]);
  REP(i, N) REP(j, N) REP(k, N) chmin(gs[j][k], gs[j][i] + gs[i][k]);

  int R;
  cin >> R;
  vector<int> z(R);
  REP(i, R) {
    cin >> z[i];
    z[i]--;
  }

  vector<vector<int>> dp(R, vector<int>(N, INF));
  function<int(int, int)> rec = [&](int i, int s) {
    auto &DP = dp[i][s];
    if (DP != INF)
      return DP;
    if (i == R - 1)
      return DP = 0LL;

    int ret = INF;
    int land = gl[z[i]][z[i + 1]] + rec(i + 1, s);
    chmin(ret, land);
    REP(v, N) {
      int sea = gl[z[i]][s] + gs[s][v] + gl[v][z[i + 1]];
      if (sea >= INF)
        continue;
      sea += rec(i + 1, v);
      chmin(ret, sea);
    }
    return DP = ret;
  };
  cout << rec(0, z[0]) << endl;
}
signed main() {
  while (1) {
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    solve(N, M);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define stoi stoll
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
#define ALL(c) begin(c), end(c)
#define RALL(c) rbegin(c), rend(c)
#define ITR(i, b, e) for (auto i = (b); i != (e); ++i)
#define FORE(x, c) for (auto &x : c)
#define REPF(i, a, n) for (int i = a, i##len = (int)(n); i < i##len; ++i)
#define REP(i, n) REPF(i, 0, n)
#define REPR(i, n) for (int i = (int)(n); i >= 0; --i)
#define SZ(c) ((int)c.size())
#define CONTAIN(c, x) (c.find(x) != end(c))
#define OUTOFRANGE(y, x, h, w) ((y) < 0 || (x) < 0 || (y) >= (h) || (x) >= (w))
#define dump(...)
const signed INF_ = 1001001001;
const long long INF = 1001001001001001001LL;
const int DX[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0},
          DY[9] = {-1, 0, 1, 0, -1, 1, 1, -1, 0};
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  ITR(i, begin(v), end(v)) os << *i << (i == end(v) - 1 ? "" : "\\n");
  return os;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  ITR(i, begin(v), end(v)) is >> *i;
  return is;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T, class U> bool chmax(T &a, const U &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T, class U> bool chmin(T &a, const U &b) {
  return a > b ? a = b, 1 : 0;
}
template <class T> using heap = priority_queue<T, vector<T>, greater<T>>;
struct {
  template <class T> operator T() {
    T x;
    cin >> x;
    return x;
  }
} IN;
struct before_main_function {
  before_main_function() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(15) << fixed;
#define endl "\\n"
  }
} before_main_function;
//------------------8<------------------------------------8<--------------------

void solve(int N, int M) {
  vector<vector<int>> gl(N, vector<int>(N, INF));
  vector<vector<int>> gs(N, vector<int>(N, INF));
  REP(i, M) {
    int x, y, t;
    char ls;
    cin >> x >> y >> t >> ls;
    x--, y--;
    if (ls == 'L') {
      gl[x][y] = gl[y][x] = t;
    } else {
      gs[x][y] = gs[y][x] = t;
    }
  }

  REP(i, N) gl[i][i] = gs[i][i] = 0;
  REP(i, N) REP(j, N) REP(k, N) chmin(gl[j][k], gl[j][i] + gl[i][k]);
  REP(i, N) REP(j, N) REP(k, N) chmin(gs[j][k], gs[j][i] + gs[i][k]);

  int R;
  cin >> R;
  vector<int> z(R);
  REP(i, R) {
    cin >> z[i];
    z[i]--;
  }

  vector<vector<int>> dp(R, vector<int>(N, INF));
  function<int(int, int)> rec = [&](int i, int s) {
    auto &DP = dp[i][s];
    if (DP != INF)
      return DP;
    if (i == R - 1)
      return 0LL;

    int land = gl[z[i]][z[i + 1]] + rec(i + 1, s);
    chmin(DP, land);
    REP(v, N) {
      int sea = gl[z[i]][s] + gs[s][v] + gl[v][z[i + 1]] + rec(i + 1, v);
      chmin(DP, sea);
    }
    return DP;
  };
  cout << rec(0, z[0]) << endl;
}
signed main() {
  while (1) {
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    solve(N, M);
  }
  return 0;
}