Language: C++

#include<iostream>
#include<vector>
#include<tuple>
#include<queue>
#include<functional>
#include<algorithm>
#include<string>
using namespace std;

#define WHITE 0
#define GRAY 1
#define BLACK 2
#define MAX_N 220
#define MAX_R 1020
#define INF 131211109876543210LL

priority_queue<tuple<long long, long long, long long>, vector<tuple<long long, long long, long long>>, greater<tuple<long long, long long, long long>>> Q;
priority_queue<tuple<long long, long long, long long>, vector<tuple<long long, long long, long long>>, greater<tuple<long long, long long, long long>>> Q2;
vector<tuple<long long, long long, int>>X[MAX_N];
string A4;
int N, M, R;
long long A1, A2, A3;
long long travel[MAX_R];
long long DIST[MAX_N][MAX_N], DIST2[MAX_N];
long long COLOR[MAX_N][MAX_N], COLOR2[MAX_N];
int PATH[MAX_N][MAX_N][MAX_N];
long long DP[MAX_R][MAX_N];
long long cnt;

void dijkstra(long long st) {
	while (!Q.empty()) {
		cnt++;
		tuple<long long, long long, long long>tup = Q.top();
		long long a1 = get<0>(tup);
		long long a2 = get<1>(tup);
		long long a3 = get<2>(tup);
		COLOR[a2][a3] = BLACK;
		Q.pop();

		for (int i = 0; i < X[a2].size(); i++) {
			long long a4 = get<0>(X[a2][i]);
			long long a5 = get<1>(X[a2][i]);
			long long a6 = get<2>(X[a2][i]);

			if (a6 == 0) {
				long long a7 = a1 + a5;
				if (DIST[a4][a3] > a7 && COLOR[a4][a3] != BLACK) {
					DIST[a4][a3] = a7;
					cnt++;
					COLOR[a4][a3] = GRAY;
					Q2.push(make_tuple(DIST[a4][a3], a4, a3));
				}
			}

			if (a6 == 1 && a2 == a3) {
				long long a8 = a1 + a5;
				if (DIST[a4][a4] > a8 && COLOR[a4][a4] != BLACK) {
					DIST[a4][a4] = a8;
					cnt++;
					COLOR[a4][a4] = GRAY;
					Q2.push(make_tuple(DIST[a4][a4], a4, a4));
				}
			}

		}
		while (!Q2.empty()) {
			Q.push(Q2.top());
			Q2.pop();
		}
	}
}

void _memset() {
	N = 0; M = 0; R = 0; A1 = 0; A2 = 0; A3 = 0; A4 = "";
	for (int i = 0; i < MAX_N; i++) {
		for (int j = 0; j < MAX_N; j++) {
			DIST[i][j] = 0;
			DIST2[i] = 0;
			COLOR[i][j] = WHITE;
			COLOR2[i] = WHITE;
		}
	}
	for (int i = 0; i < MAX_N; i++) {
		for (int j = 0; j < MAX_N; j++) {
			for (int k = 0; k < MAX_N; k++) {
				PATH[i][j][k] = INF;
			}
		}
	}
	for (int i = 0; i < MAX_R; i++) {
		for (int j = 0; j < MAX_N; j++) {
			DP[i][j] = INF;
		}
	}
	for (int i = 0; i < MAX_R; i++) {
		travel[i] = 0;
	}
	for (int i = 0; i < MAX_N; i++) {
		X[i].clear();
	}
	while (!Q.empty()) {
		Q.pop();
	}
	while (!Q2.empty()) {
		Q2.pop();
	}
	cnt = 0;
}

int main() {
	while (true) {
		_memset();
		cin >> N >> M;
		if (N == 0 && M == 0) {
			break;
		}
		for (int i = 0; i < M; i++) {
			cin >> A1 >> A2 >> A3 >> A4;
			if (A4 == "L") {
				X[A1].push_back(make_tuple(A2, A3, 0));
				X[A2].push_back(make_tuple(A1, A3, 0));
			}
			if (A4 == "S") {
				X[A1].push_back(make_tuple(A2, A3, 1));
				X[A2].push_back(make_tuple(A1, A3, 1));
			}
		}
		cin >> R;
		for (int i = 0; i < R; i++) {
			cin >> travel[i];
		}
		for (int i = 1; i <= N; i++) {
			for (int j = 0; j < MAX_N; j++) {
				for (int k = 0; k < MAX_N; k++) {
					DIST[j][k] = INF;
					COLOR[j][k] = WHITE;
				}
			}
			DIST[i][i] = 0;
			COLOR[i][i] = GRAY;
			Q.push(make_tuple(0, i, i));
			dijkstra(i);
			for (int j = 1; j <= N; j++) {
				for (int k = 1; k <= N; k++) {
					PATH[i][j][k] = min(PATH[i][j][k], DIST[j][k]);
				}
			}
		}
		DP[0][travel[0]] = 0;
		for (int i = 0; i < R - 1; i++) {
			for (int j = 1; j <= N; j++) {
				long long start = travel[i];
				long long goal = travel[i + 1];
				if (start == j) {
					for (int k = 1; k <= N; k++) {
						DP[i + 1][k] = min(DP[i + 1][k], DP[i][j] + PATH[start][goal][k]);
					}
				}
				else {
					for (int k = 1; k <= N; k++) {
						DP[i + 1][j] = min(DP[i + 1][j], DP[i][j] + PATH[start][goal][j]);
						DP[i + 1][k] = min(DP[i + 1][k], DP[i][j] + PATH[start][j][start] + PATH[j][goal][k]);
					}
				}
			}
		}
		long long res = INF;
		for (int i = 1; i <= N; i++) {
			res = min(res, DP[R - 1][i]);
		}
		cout << res << endl;
	}
	return 0;
}

Compiler output:
a.cc: In function 'void _memset()':
a.cc:15:13: warning: overflow in conversion from 'long long int' to 'int' changes value from '131211109876543210' to '717663978' [-Woverflow]
   15 | #define INF 131211109876543210LL
      |             ^~~~~~~~~~~~~~~~~~~~
a.cc:86:49: note: in expansion of macro 'INF'
   86 |                                 PATH[i][j][k] = INF;
      |                                                 ^~~
a.cc: In function 'int main()':
a.cc:145:60: error: no matching function for call to 'min(int&, long long int&)'
  145 |                                         PATH[i][j][k] = min(PATH[i][j][k], DIST[j][k]);
      |                                                         ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:145:60: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
  145 |                                         PATH[i][j][k] = min(PATH[i][j][k], DIST[j][k]);
      |                                                         ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:6:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:145:60: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  145 |                                         PATH[i][j][k] = min(PATH[i][j][k], DIST[j][k]);
      |                                                         ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~