#include using ll = long long; #include template constexpr T INF() { return std::numeric_limits::max() / 16; } #include #include 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 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; }