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

#include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

#define GET_ARG(a, b, c, F, ...) F
#define REP3(i, s, e) for (i = s; i <= e; i++)
#define REP2(i, n) REP3(i, 0, (int)(n)-1)
#define REP(...) GET_ARG(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define RREP3(i, s, e) for (i = s; i >= e; i--)
#define RREP2(i, n) RREP3(i, (int)(n)-1, 0)
#define RREP(...) GET_ARG(__VA_ARGS__, RREP3, RREP2)(__VA_ARGS__)
#define DEBUG(x) cerr << #x ": " << x << endl

typedef long long ll;

constexpr ll LL_INF = 1e15;

ll dist1[200][200], dist2[200][200];
ll dp[1000][200];

/*
 * 2 <= N <= 200
 * 1 <= M <= 10000
 * 1 <= xi, yi <= N
 * 1 <= ti <= 1000
 * 1 <= R <= 1000
 * 1 <= zi <= N
 */

int main(void) {
  while (true) {
    int i, j, k, n, m;
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    REP(i, n) REP(j, n) {
      dist1[i][j] = i == j ? 0 : LL_INF;
      dist2[i][j] = i == j ? 0 : LL_INF;
    }
    REP(i, m) {
      int x, y, t;
      char sl;
      scanf("%d %d %d %c", &x, &y, &t, &sl);
      x--;
      y--;
      if (sl == 'S') {
        dist1[x][y] = min<ll>(dist1[x][y], t);
        dist1[y][x] = dist1[x][y];
      } else {
        dist2[x][y] = min<ll>(dist2[x][y], t);
        dist2[y][x] = dist2[x][y];
      }
    }
    REP(k, n) REP(i, n) REP(j, n) {
      dist1[i][j] = min(dist1[i][j], dist1[i][k] + dist1[k][j]);
      dist2[i][j] = min(dist2[i][j], dist2[i][k] + dist2[k][j]);
    }
    int r;
    scanf("%d", &r);
    int pre;
    scanf("%d", &pre);
    pre--;
    REP(i, r) REP(j, n) dp[i][j] = LL_INF;
    dp[0][pre] = 0;
    REP(i, r - 1) {
      int x;
      scanf("%d", &x);
      x--;
      REP(j, n) {
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + dist2[pre][x]);
        REP(k, n)
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][k] + dist2[pre][k] +
                                             dist1[k][j] + dist2[j][x]);
      }
      pre = x;
    }
    ll ans = LL_INF;
    REP(j, n) ans = min(ans, dp[r - 1][j]);
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

#define GET_ARG(a, b, c, F, ...) F
#define REP3(i, s, e) for (i = s; i <= e; i++)
#define REP2(i, n) REP3(i, 0, (int)(n)-1)
#define REP(...) GET_ARG(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define RREP3(i, s, e) for (i = s; i >= e; i--)
#define RREP2(i, n) RREP3(i, (int)(n)-1, 0)
#define RREP(...) GET_ARG(__VA_ARGS__, RREP3, RREP2)(__VA_ARGS__)
#define DEBUG(x) cerr << #x ": " << x << endl

constexpr int INF = 1e9;

vector<pair<int, pair<int, char>>> e[200]; // (y, t, sl)
int dist[200][200];                        // (pos, ship)

int main(void) {
  while (true) {
    int i, j, n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    REP(i, n) e[i].clear();
    REP(i, m) {
      int x, y, t;
      char sl;
      cin >> x >> y >> t >> sl;
      x--;
      y--;
      e[x].push_back({y, {t, sl}});
      e[y].push_back({x, {t, sl}});
    }
    priority_queue<pair<int, pair<int, int>>> q; // (-dist,x,ship)
    int r, goal = 0;
    cin >> r;
    REP(i, n) REP(j, n) dist[i][j] = INF;
    dist[0][0] = 0;
    while (r--) {
      REP(i, n) REP(j, n) if (i != goal) dist[i][j] = INF;
      REP(j, n) q.push({dist[goal][j], {goal, j}});
      cin >> goal;
      goal--;
      while (!q.empty()) {
        auto p = q.top();
        q.pop();
        int d = -p.first;
        int x = p.second.first;
        int ship = p.second.second;
        if (d > dist[x][ship])
          continue;
        for (auto pp : e[x]) {
          int y = pp.first;
          int t = pp.second.first;
          char sl = pp.second.second;
          if (sl == 'S') {
            if (ship == x && dist[y][y] > dist[x][x] + t) {
              dist[y][y] = dist[x][x] + t;
              q.push({-dist[y][y], {y, y}});
            }
          } else {
            if (dist[y][ship] > dist[x][ship] + t) {
              dist[y][ship] = dist[x][ship] + t;
              q.push({-dist[y][ship], {y, ship}});
            }
          }
        }
      }
    }
    int ans = INF;
    REP(j, n) ans = min(ans, dist[goal][j]);
    cout << ans << endl;
  }
  return 0;
}