#include //!===========================================================!// //! dP dP dP !// //! 88 88 88 !// //! 88aaaaa88a .d8888b. .d8888b. .d888b88 .d8888b. 88d888b. !// //! 88 88 88ooood8 88' `88 88' `88 88ooood8 88' `88 !// //! 88 88 88. ... 88. .88 88. .88 88. ... 88 !// //! dP dP `88888P' `88888P8 `88888P8 `88888P' dP !// //!===========================================================!// using ld = long double; using ll = long long; std::mt19937 mt{std::random_device{}()}; template constexpr F PI() { return 3.1415926535897932385; } template std::ostream& operator<<(std::ostream& os, const std::array& v) { os << "["; for (const auto& e : v) { os << e << ","; } return (os << "]" << std::endl); } template std::ostream& operator<<(std::ostream& os, const std::deque& v) { os << "["; for (const auto& e : v) { os << e << ","; } return (os << "]" << std::endl); } template std::ostream& operator<<(std::ostream& os, const std::multimap& v) { os << "["; for (const auto& e : v) { os << "<" << e.first << ": " << e.second << ">,"; } return (os << "]" << std::endl); } template std::ostream& operator<<(std::ostream& os, const std::multiset& v) { os << "["; for (const auto& e : v) { os << e << ","; } return (os << "]" << std::endl); } template std::ostream& operator<<(std::ostream& os, const std::map& v) { os << "["; for (const auto& e : v) { os << "<" << e.first << ": " << e.second << ">,"; } return (os << "]" << std::endl); } template std::ostream& operator<<(std::ostream& os, const std::pair& v) { return (os << "<" << v.first << "," << v.second << ">"); } template std::ostream& operator<<(std::ostream& os, const std::set& v) { os << "["; for (const auto& e : v) { os << e << ","; } return (os << "]" << std::endl); } template std::ostream& operator<<(std::ostream& os, const std::unordered_multimap& v) { os << "["; for (const auto& e : v) { os << "<" << e.first << ": " << e.second << ">,"; } return (os << "]" << std::endl); } template std::ostream& operator<<(std::ostream& os, const std::unordered_multiset& v) { os << "["; for (const auto& e : v) { os << e << ","; } return (os << "]" << std::endl); } template std::ostream& operator<<(std::ostream& os, const std::unordered_map& v) { os << "["; for (const auto& e : v) { os << "<" << e.first << ": " << e.second << ">,"; } return (os << "]" << std::endl); } template std::ostream& operator<<(std::ostream& os, const std::unordered_set& v) { os << "["; for (const auto& e : v) { os << e << ","; } return (os << "]" << std::endl); } template std::ostream& operator<<(std::ostream& os, const std::vector& v) { os << "["; for (const auto& e : v) { os << e << ","; } return (os << "]" << std::endl); } #define show(x) std::cerr << #x << " = " << (x) << std::endl //!=======================================================================================!// //! a88888b. dP .88888. dP !// //! d8' `88 88 d8' `88 88 !// //! 88 .d8888b. .d8888b. d8888P 88 88d888b. .d8888b. 88d888b. 88d888b. !// //! 88 88' `88 Y8ooooo. 88 88 YP88 88' `88 88' `88 88' `88 88' `88 !// //! Y8. .88 88. .88 88 88 Y8. .88 88 88. .88 88. .88 88 88 !// //! Y88888P' `88888P' `88888P' dP `88888' dP `88888P8 88Y888P' dP dP !// //! 88 !// //! dP !// //!=======================================================================================!// template struct CostGraph { CostGraph(const std::size_t v) : V{v}, edge(v), rev_edge(v) {} struct Edge { Edge(const std::size_t from, const std::size_t to, const T cost) : from{from}, to{to}, cost{cost} {} const std::size_t from, to; const T cost; bool operator<(const Edge& e) const { return cost != e.cost ? cost < e.cost : to < e.to; } }; void addEdge(const std::size_t from, const std::size_t to, const T cost) { edge[from].push_back(Edge{from, to, cost}), rev_edge[to].push_back(Edge(to, from, cost)); } const std::size_t V; std::vector> edge, rev_edge; }; template constexpr T INF() { return std::numeric_limits::max() / 4; } //!==============================================================!// //! 888888ba oo oo dP dP !// //! 88 `8b 88 88 !// //! 88 88 dP dP 88 .dP .d8888b. d8888P 88d888b. .d8888b. !// //! 88 88 88 88 88888" Y8ooooo. 88 88' `88 88' `88 !// //! 88 .8P 88 88 88 `8b. 88 88 88 88. .88 !// //! 8888888P dP 88 dP `YP `88888P' dP dP `88888P8 !// //! 88 !// //! dP !// //!==============================================================!// template std::vector Dijkstra(const CostGraph& g, const std::size_t s) { std::vector d(g.V, INF()); using P = std::pair; std::priority_queue, std::greater

> q; d[s] = 0, q.push({0, s}); while (not q.empty()) { const T cost = q.top().first; const std::size_t v = q.top().second; q.pop(); if (d[v] < cost) { continue; } for (const auto& e : g.edge[v]) { if (d[e.to] <= d[v] + e.cost) { continue; } d[e.to] = d[v] + e.cost, q.push({d[e.to], e.to}); } } return d; } int main() { int N, M, L; std::cin >> N >> M >> L, L--; CostGraph g(N); std::vector t(N); ll tsum = 0; for (int i = 0; i < N; i++) { std::cin >> t[i], tsum += t[i]; } for (int i = 0, a, b, c; i < M; i++) { std::cin >> a >> b >> c, a--, b--, g.addEdge(a, b, c), g.addEdge(b, a, c); } for (int i = 0; i < N; i++) { if (t[i] == tsum) { return std::cout << 0 << std::endl, 0; } } std::vector> d(N, std::vector(N)); for (int i = 0; i < N; i++) { d[i] = Dijkstra(g, i); } ll ans = INF(); for (int i = 0; i < N; i++) { ll sum = 0; for (int j = 0; j < N; j++) { sum += 2 * d[i][j] * t[j]; } for (int j = 0; j < N; j++) { if (t[j] == 0) { continue; } ans = std::min(ans, sum + d[L][j] - d[i][j]); } } std::cout << ans << std::endl; return 0; }