#include using namespace std; //#include // using namespace boost::multiprecision; // using cint = cpp_int; //#define input(a) scanf("%lld", &(a)) //#define output(a) printf("%lld\n", (a)) // Define using ll = long long; using ull = unsigned long long; using ld = long double; const ll MOD = 1e9 + 7; const ll INF = LONG_MAX; const ull MAX = ULONG_MAX; #define endl '\n' #define space ' ' #define def inline auto #define func inline constexpr ll #define run __attribute__((constructor)) def _ #define all(v) begin(v), end(v) // Debug #define debug(...) \ { \ cerr << __LINE__ << ": " << #__VA_ARGS__ << " = "; \ for (auto &&X : {__VA_ARGS__}) cerr << "[" << X << "] "; \ cerr << endl; \ } // Loop #define inc(i, a, n) for (ll i = (a), _##i = (n); i <= _##i; ++i) #define dec(i, a, n) for (ll i = (a), _##i = (n); i >= _##i; --i) #define each(i, a) for (auto &&i : a) #define rep(i, n) inc(i, 0, n - 1) // Stream #define fout(n) cout << fixed << setprecision(n) #define fasten cin.tie(0), ios::sync_with_stdio(0) // Speed run() { fasten, fout(10); } #pragma GCC optimize("O3") #pragma GCC optimization_level 3 #pragma GCC target("avx") // Math func gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } func lcm(ll a, ll b) { return a * b / gcd(a, b); } def input() { ll A; cin >> A; return A; } template struct DJK { ll V; using P = pair; vector> G; vector dist; vector used; DJK(ll v) : V(v), G(v), dist(v), used(v) { fill_n(dist.begin(), V, INF); fill_n(used.begin(), V, false); } void setDist(ll a, ll b, ll d) { G[a].push_back(P(d, b)); } void culc(ll s = 0) { priority_queue, greater

> Q; fill_n(dist.begin(), V, INF); fill_n(used.begin(), V, false); Q.push(P(0, s)); while (!Q.empty()) { T d; ll t; tie(d, t) = Q.top(), Q.pop(); if (used[t]) continue; used[t] = true, dist[t] = d; for (P e : G[t]) { if (dist[e.second] <= d + e.first) continue; Q.push(P(d + e.first, e.second)); } } } }; ll A, B, C, N, M, L, res = INF; vector T; vector> D; signed main() { cin >> N >> M >> L; DJK dijk(N); rep(i, N) T.push_back(input()); ll cnt = 0; rep(i, N) if (T[i]) cnt++; if (cnt == 0) return puts("0") & 0; rep(i, M) { cin >> A >> B >> C; dijk.setDist(A - 1, B - 1, C); dijk.setDist(B - 1, A - 1, C); } rep(i, N) dijk.culc(i), D.push_back(dijk.dist); rep(i, N) { ll S = 0, P = D[L - 1][i]; rep(j, N) { S += 2 * D[i][j] * T[j]; if (T[j]) P = min(P, D[L - 1][j] - D[i][j]); } res = min(res, S + P); } cout << res << endl; } // for compilation: g++ -Ofast -march=native -o _ _.cpp -std=c++17