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

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1001001001001001LL;
int N, M;
int A[200][200], B[200][200];
int dp[2][200];
signed main() {
  while (cin >> N >> M, N || M) {
    fill_n(*A, 200 * 200, INF);
    fill_n(*B, 200 * 200, INF);
    for (int i = 0; i < N; i++) {
      A[i][i] = 0;
      B[i][i] = 0;
    }
    for (int i = 0; i < M; i++) {
      int a, b, c;
      char d;
      cin >> a >> b >> c >> d;
      a--;
      b--;
      if (d == 'L')
        A[a][b] = A[b][a] = min(A[a][b], c);
      else
        B[a][b] = B[b][a] = min(B[a][b], c);
    }

    for (int k = 0; k < N; k++) {
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
          A[i][j] = min(A[i][j], A[i][k] + A[k][j]);
          B[i][j] = min(B[i][j], B[i][k] + B[k][j]);
        }
      }
    }

    int R;
    cin >> R;
    int prev;
    cin >> prev;
    prev--;
    fill_n(*dp, 2 * 200, INF);
    dp[1][prev] = 0;

    for (int i = 1; i < R; i++) {
      int cur;
      cin >> cur;
      cur--;
      for (int j = 0; j < N; j++) {
        for (int k = 0; k < N; k++) {
          if (j != k)
            dp[(i + 1) & 1][j] =
                min(dp[(i + 1) & 1][j],
                    dp[i & 1][k] + A[prev][k] + B[k][j] + A[j][cur]);
          else
            dp[(i + 1) & 1][j] =
                min(dp[(i + 1) & 1][j], dp[i & 1][j] + A[prev][cur]);
        }
      }
      for (int j = 0; j < N; j++)
        dp[i & 1][j] = INF;
      prev = cur;
    }

    int mi = INF;
    for (int i = 0; i < N; i++)
      mi = min(mi, dp[R & 1][i]);

    cout << mi << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

struct edge {
  int to, cost;
  bool S;
  edge(int a, int b, bool c) : to(a), cost(b), S(c) {}
};

struct data {
  int pos, cost, ship;
  data(int a, int b, int c) : pos(a), cost(b), ship(c) {}
  bool operator<(const data &d) const { return cost > d.cost; }
};

int N, M, T;
vector<edge> G[200];
vector<int> task;
int dist[200][200];
const int INF = 1001001001001001001;
void solve() {
  priority_queue<data> Q;

  for (int i = 0; i < T - 1; i++) {
    if (i == 0) {
      for (int j = 0; j < N; j++)
        fill_n(dist[j], N, INF);
      dist[task[0]][task[0]] = 0;
      Q.push(data(task[0], 0, task[0]));
    } else {
      for (int j = 0; j < N; j++)
        if (task[i] != j)
          fill_n(dist[j], N, INF);
      for (int j = 0; j < N; j++) {
        Q.push(data(task[i], dist[task[i]][j], j));
      }
    }

    while (Q.size()) {
      data d = Q.top();
      Q.pop();

      if (dist[d.pos][d.ship] < d.cost)
        continue;

      for (int j = 0; j < G[d.pos].size(); j++) {
        edge &e = G[d.pos][j];
        if (e.S) {
          if (d.pos != d.ship)
            continue;
          if (dist[e.to][e.to] <= d.cost + e.cost)
            continue;
          dist[e.to][e.to] = d.cost + e.cost;
          Q.push(data(e.to, d.cost + e.cost, e.to));
        } else {
          if (dist[e.to][d.ship] <= d.cost + e.cost)
            continue;
          dist[e.to][d.ship] = d.cost + e.cost;
          Q.push(data(e.to, d.cost + e.cost, d.ship));
        }
      }
    }
  }

  int ans = INF;
  for (int i = 0; i < N; i++)
    ans = min(ans, dist[task[T - 1]][i]);
  printf("%lld\\n", ans);
}

signed main() {
  while (scanf("%lld%lld", &N, &M), N || M) {
    for (int i = 0; i < N; i++)
      G[i].clear();
    for (int i = 0; i < M; i++) {
      int a, b, c;
      char d;
      scanf("%lld%lld%lld %c", &a, &b, &c, &d);
      a--;
      b--;
      G[a].push_back(edge(b, c, (d == 'S')));
      G[b].push_back(edge(a, c, (d == 'S')));
    }
    scanf("%d", &T);
    task.resize(T);
    for (int i = 0; i < T; i++)
      scanf("%d", &task[i]), task[i]--;

    solve();
  }
  return 0;
}