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

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define show(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define show(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
template <typename T> using vec = std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 60;
const double eps = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve(int n, int m) {
  ll dl[256][256], ds[256][256];
  fill_n((ll *)dl, 256 * 256, infll);
  fill_n((ll *)ds, 256 * 256, infll);
  rep(i, 0, 256) dl[i][i] = ds[i][i] = 0;
  rep(i, 0, m) {
    int x, y;
    ll t;
    char sl;
    cin >> x >> y >> t >> sl;
    --x;
    --y;
    if (sl == 'L') {
      dl[x][y] = min(dl[x][y], t);
      dl[y][x] = min(dl[y][x], t);
    } else {
      ds[x][y] = min(ds[x][y], t);
      ds[y][x] = min(ds[y][x], t);
    }
  }
  int r, z[1000];
  cin >> r;
  rep(i, 0, r) {
    cin >> z[i];
    --z[i];
  }

  rep(k, 0, n) rep(i, 0, n) rep(j, 0, n) dl[i][j] =
      min(dl[i][j], dl[i][k] + dl[k][j]);
  rep(k, 0, n) rep(i, 0, n) rep(j, 0, n) ds[i][j] =
      min(ds[i][j], ds[i][k] + ds[k][j]);

  ll dp[1000][256];
  fill_n((ll *)dp, 1000 * 256, infll);
  rep(i, 0, n) dp[0][i] = ds[z[0]][i] + dl[i][z[0]];
  rep(i, 1, r) rep(j, 0, n) rep(k, 0, n) {
    if (k != j)
      dp[i][j] = min(dp[i][j],
                     dp[i - 1][k] + dl[z[i - 1]][k] + ds[k][j] + dl[j][z[i]]);
    else
      dp[i][j] = min(dp[i][j], dp[i - 1][k] + dl[z[i - 1]][z[i]]);
  }

  ll ans = infll;
  rep(i, 0, n) ans = min(ans, dp[r - 1][i]);
  cout << ans << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 and m == 0)
      break;
    solve(n, m);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define show(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define show(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
template <typename T> using vec = std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 62;
const double eps = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve(int n, int m) {
  vector<pii> graph[256][2];
  rep(i, 0, m) {
    int x, y, t;
    char sl;
    cin >> x >> y >> t >> sl;
    --x;
    --y;
    int a = (sl == 'L' ? 0 : 1);
    graph[x][a].push_back(make_pair(y, t));
    graph[y][a].push_back(make_pair(x, t));
  }

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

  typedef tuple<ll, int, int> tpl;
  ll dist[200][200];
  fill_n((ll *)dist, 200 * 200, infll);
  dist[z[0]][z[0]] = 0;
  rep(i, 1, r) {
    priority_queue<tpl, vector<tpl>, greater<tpl>> que;
    rep(j, 0, n) {
      rep(k, 0, n) {
        if (dist[j][k] == inf)
          continue;
        if (j == z[i - 1])
          que.push(make_tuple(dist[j][k], j, k));
        else
          dist[j][k] = inf;
      }
    }
    while (!que.empty()) {
      auto t = que.top();
      que.pop();
      ll d = get<0>(t);
      int p1 = get<1>(t), p2 = get<2>(t);
      if (d > dist[p1][p2])
        continue;
      for (auto &e : graph[p1][0]) {
        ll d_ = d + e.second;
        int p_ = e.first;
        if (d_ >= dist[p_][p2])
          continue;
        dist[p_][p2] = d_;
        que.push(make_tuple(d_, p_, p2));
      }
      if (p1 != p2)
        continue;
      for (auto &e : graph[p1][1]) {
        ll d_ = d + e.second;
        int p_ = e.first;
        if (d_ >= dist[p_][p_])
          continue;
        dist[p_][p_] = d_;
        que.push(make_tuple(d_, p_, p_));
      }
    }
  }
  ll ans = infll;
  rep(i, 0, n) ans = min(ans, dist[z.back()][i]);
  cout << ans << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 and m == 0)
      break;
    solve(n, m);
  }
  return 0;
}