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

#define _CRT_SECURE_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define MP make_pair
#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define STRING(x)                                                              \\
  string x;                                                                    \\
  cin >> x;
#define STR(x) STRING(x);
#define DOUBLE(x)                                                              \\
  double x;                                                                    \\
  cin >> x;
#define DBL(x) DOUBLE(x);
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << ":" << x << endl
#define INIT(a) memset(a, -1, sizeof a)
#define CLR(a) memset(a, 0, sizeof a)
#define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<int> vi;
typedef vector<lint> vl;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
// const int dx[4] = { 0,1,0,-1 }, dy[4] = { -1,0,1,0 };
// const int M = 1000000007;
template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

struct P {
  int now, ship, cost, job;
  P(int now, int ship, int cost, int job)
      : now(now), ship(ship), cost(cost), job(job) {}
  bool operator<(const P &other) const { return cost < other.cost; }
  bool operator>(const P &other) const { return cost > other.cost; }
};
signed main() {
  while (true) {
    int N, M;
    int SeaRoutes[201][201];
    int LandRoutes[201][201];
    int R;
    vi Destinations;
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    FOR(i, 201) FOR(j, 201) SeaRoutes[i][j] = LandRoutes[i][j] = INF;
    FOR(i, 201) SeaRoutes[i][i] = LandRoutes[i][i] = 0;
    FOR(i, M) {
      int x, y, t;
      char sl;
      cin >> x >> y >> t >> sl;
      if (sl == 'S') {
        SeaRoutes[x][y] = min(SeaRoutes[x][y], t);
        SeaRoutes[y][x] = min(SeaRoutes[y][x], t);
      } else {
        LandRoutes[x][y] = min(LandRoutes[x][y], t);
        LandRoutes[y][x] = min(LandRoutes[y][x], t);
      }
    }
    cin >> R;
    FOR(i, R) {
      int z;
      cin >> z;
      Destinations.push_back(z);
    }
    FOR(k, N + 1) FOR(i, N + 1) FOR(j, N + 1) {
      SeaRoutes[i][j] = min(SeaRoutes[i][j], SeaRoutes[i][k] + SeaRoutes[k][j]);
      LandRoutes[i][j] =
          min(LandRoutes[i][j], LandRoutes[i][k] + LandRoutes[k][j]);
    }
    int Flag[201][1001];
    FOR(i, 201) FOR(j, 1001) Flag[i][j] = INF;
    priority_queue<P, vector<P>, greater<P>> Queue;
    Queue.push(P(Destinations[0], Destinations[0], 0, 1));
    while (!Queue.empty()) {
      P Current = Queue.top();
      Queue.pop();
      if (Current.job == R) {
        cout << Current.cost << endl;
        break;
      }
      const int Destination = Destinations[Current.job];
      const int now = Current.now;
      const int ship = Current.ship;

      if (LandRoutes[now][Destination] < INF) {
        P A = Current;
        A.cost += LandRoutes[now][Destination];
        A.now = Destination;
        A.job++;
        if (Flag[A.ship][A.job] > A.cost) {
          Queue.push(A);
          Flag[A.ship][A.job] = A.cost;
        }
      }

      FOR(i, N + 1) {
        if (SeaRoutes[ship][i] < INF && LandRoutes[i][Destination] < INF) {
          P A = Current;
          A.cost += LandRoutes[now][ship] + SeaRoutes[ship][i] +
                    LandRoutes[i][Destination];
          A.now = Destination;
          A.ship = i;
          A.job++;
          if (Flag[A.ship][A.job] > A.cost) {
            Queue.push(A);
            Flag[A.ship][A.job] = A.cost;
          }
        }
      }
    }
  }
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define MP make_pair
#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define STRING(x)                                                              \\
  string x;                                                                    \\
  cin >> x;
#define STR(x) STRING(x);
#define DOUBLE(x)                                                              \\
  double x;                                                                    \\
  cin >> x;
#define DBL(x) DOUBLE(x);
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << ":" << x << endl
#define INIT(a) memset(a, -1, sizeof a)
#define CLR(a) memset(a, 0, sizeof a)
#define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<int> vi;
typedef vector<lint> vl;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
// const int dx[4] = { 0,1,0,-1 }, dy[4] = { -1,0,1,0 };
// const int M = 1000000007;
template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

struct P {
  int now, ship, cost, job;
  P(int now, int ship, int cost, int job)
      : now(now), ship(ship), cost(cost), job(job) {}
  bool operator<(const P &other) const { return cost < other.cost; }
  bool operator>(const P &other) const { return cost > other.cost; }
};
signed main() {
  while (true) {
    int N, M;
    int SeaRoutes[201][201];
    int LandRoutes[201][201];
    int R;
    vi Destinations;
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    FOR(i, 201) FOR(j, 201) SeaRoutes[i][j] = LandRoutes[i][j] = INF;
    FOR(i, 201) SeaRoutes[i][i] = LandRoutes[i][i] = 0;
    FOR(i, M) {
      int x, y, t;
      char sl;
      cin >> x >> y >> t >> sl;
      if (sl == 'S') {
        SeaRoutes[x][y] = min(SeaRoutes[x][y], t);
        SeaRoutes[y][x] = min(SeaRoutes[y][x], t);
      } else {
        LandRoutes[x][y] = min(LandRoutes[x][y], t);
        LandRoutes[y][x] = min(LandRoutes[y][x], t);
      }
    }
    cin >> R;
    FOR(i, R) {
      int z;
      cin >> z;
      Destinations.push_back(z);
    }
    FOR(k, N + 1) FOR(i, N + 1) FOR(j, N + 1) {
      SeaRoutes[i][j] = min(SeaRoutes[i][j], SeaRoutes[i][k] + SeaRoutes[k][j]);
      LandRoutes[i][j] =
          min(LandRoutes[i][j], LandRoutes[i][k] + LandRoutes[k][j]);
    }
    bool Flag[201][1001] = {};
    priority_queue<P, vector<P>, greater<P>> Queue;
    Queue.push(P(Destinations[0], Destinations[0], 0, 1));
    int p = 0;
    while (!Queue.empty()) {
      P Current = Queue.top();
      Queue.pop();
      if (Current.job == R) {
        cout << Current.cost << endl;
        break;
      }
      int Destination = Destinations[Current.job];

      if (Flag[Current.ship][Current.job])
        continue;
      Flag[Current.ship][Current.job] = true;

      P A = Current;
      A.cost += LandRoutes[A.now][Destination];
      A.now = Destination;
      A.job++;
      Queue.push(A);

      FOR(i, N + 1) {
        P B = Current;
        B.cost += LandRoutes[B.now][B.ship];
        B.job++;
        if (SeaRoutes[B.ship][i] < INF && LandRoutes[i][Destination] < INF) {
          B.cost += SeaRoutes[B.ship][i] + LandRoutes[i][Destination];
          B.now = Destination;
          B.ship = i;
          Queue.push(B);
        }
      }
    }
  }
}