Language: C++

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <functional>
#include <limits>
#include <queue>
#include <iomanip>
#include <deque>
#include <stack>
#include <cstdio>
#include <cmath>
#include <complex.h>
#include <cstdio>
#include <random>
#include <chrono>
#include <numeric>
#include <cassert>
#include <iso646.h>

using namespace std;

typedef long long i64;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

#ifndef ONLINE_JUDGE
	FILE *stream;
	freopen_s(&stream, "input.txt", "r", stdin);
	// freopen_s(&stream, "output.txt", "w", stdout);
#endif

    int n, m, r;
    cin >> n >> m >> r;

    if (r > 8)
        throw exception("bad");

    int target_cities[8];
    for (int i = 0; i < r; i++)
        cin >> target_cities[i];

    const i64 inf = 1e9;
    i64 dist[201][201] = {};
    for (int v = 1; v <= n; v++)
        for (int u = 1; u <= n; u++)
            if (u != v)
                dist[u][v] = inf;

    int max_dist = 0;
    for (int i = 0; i < m; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;

        if (dist[a][b] < inf ||
            dist[b][a] < inf)
            throw exception("bad");

        dist[a][b] = c;
        dist[b][a] = c;

        max_dist = max(max_dist, c);
    }

    for (int k = 1; k <= n; k++)
        for (int v = 1; v <= n; v++)
            for (int u = 1; u <= n; u++)
                if (dist[v][u] > dist[v][k] + dist[k][u])
                    dist[v][u] = dist[v][k] + dist[k][v];

    sort(target_cities, target_cities + r);

    i64 min_dist = 1e18;
    do
    {
        i64 cur_dist = 0;
        for (int i = 0; i + 1 < r; i++)
        {
            const int u = target_cities[i];
            const int v = target_cities[i + 1];

            cur_dist += dist[u][v];
        }

        min_dist = min(min_dist, cur_dist);

    } while (next_permutation(target_cities, target_cities + r));

    if (min_dist > (r - 1) * max_dist)
        throw exception("bad");

    cout << min_dist;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:35:9: error: 'freopen_s' was not declared in this scope; did you mean 'freopen64'?
   35 |         freopen_s(&stream, "input.txt", "r", stdin);
      |         ^~~~~~~~~
      |         freopen64
a.cc:43:30: error: no matching function for call to 'std::exception::exception(const char [4])'
   43 |         throw exception("bad");
      |                              ^
In file included from /usr/include/c++/14/exception:36,
                 from /usr/include/c++/14/ios:41,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/exception.h:67:5: note: candidate: 'constexpr std::exception::exception(std::exception&&)'
   67 |     exception(exception&&) = default;
      |     ^~~~~~~~~
/usr/include/c++/14/bits/exception.h:67:15: note:   no known conversion for argument 1 from 'const char [4]' to 'std::exception&&'
   67 |     exception(exception&&) = default;
      |               ^~~~~~~~~~~
/usr/include/c++/14/bits/exception.h:65:5: note: candidate: 'constexpr std::exception::exception(const std::exception&)'
   65 |     exception(const exception&) = default;
      |     ^~~~~~~~~
/usr/include/c++/14/bits/exception.h:65:15: note:   no known conversion for argument 1 from 'const char [4]' to 'const std::exception&'
   65 |     exception(const exception&) = default;
      |               ^~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/exception.h:62:5: note: candidate: 'std::exception::exception()'
   62 |     exception() _GLIBCXX_NOTHROW { }
      |     ^~~~~~~~~
/usr/include/c++/14/bits/exception.h:62:5: note:   candidate expects 0 arguments, 1 provided
a.cc:64:34: error: no matching function for call to 'std::exception::exception(const char [4])'
   64 |             throw exception("bad");
      |                                  ^
/usr/include/c++/14/bits/exception.h:67:5: note: candidate: 'constexpr std::exception::exception(std::exception&&)'
   67 |     exception(exception&&) = default;
      |     ^~~~~~~~~
/usr/include/c++/14/bits/exception.h:67:15: note:   no known conversion for argument 1 from 'const char [4]' to 'std::exception&&'
   67 |     exception(exception&&) = default;
      |               ^~~~~~~~~~~
/usr/include/c++/14/bits/exception.h:65:5: note: candidate: 'constexpr std::exception::exception(const std::exception&)'
   65 |     exception(const exception&) = default;
      |     ^~~~~~~~~
/usr/include/c++/14/bits/exception.h:65:15: note:   no known conversion for argument 1 from 'const char [4]' to 'const std::exception&'
   65 |     exception(const exception&) = default;
      |               ^~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/exception.h:62:5: note: candidate: 'std::exception::exception()'
   62 |     exception() _GLIBCXX_NOTHROW { }
      |     ^~~~~~~~~
/usr/include/c++/14/bits/exception.h:62:5: note:   candidate expects 0 arguments, 1 provided
a.cc:97:30: error: no matching function for call to 'std::exception::exception(const char [4])'
   97 |         throw exception("bad");
      |                              ^
/usr/include/c++/14/bits/exception.h:67:5: note: candidate: 'constexpr std::exception::exception(std::exception&&)'
   67 |     exception(exception&&) = default;
      |     ^~~~~~~~~
/usr/include/c++/14/bits/exception.h:67:15: note:   no known conversion for argument 1 from 'const char [4]' to 'std::exception&&'
   67 |     exception(exception&&) = default;
      |               ^~~~~~~~~~~
/usr/include/c++/14/bits/exception.h:65:5: note: candidate: 'constexpr std::exception::exception(const std::exception&)'
   65 |     exception(const exception&) = default;
      |     ^~~~~~~~~
/usr/include/c++/14/bits/exception.h:65:15: note:   no known conversion for argument 1 from 'const char [4]' to 'const std::exception&'
   65 |     exception(const exception&) = default;
      |               ^~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/exception.h:62:5: note: candidate: 'std::exception::exception()'
   62 |     exception() _GLIBCXX_NOTHROW { }
      |     ^~~~~~~~~
/usr/include/c++/14/bits/exception.h:62:5: note:   candidate expects 0 arguments, 1 provided