#include #include #include #include #include #include template using MinHeap = std::priority_queue, std::greater>; template struct Edge { int src, dst; Cost cost; Edge(int src = -1, int dst = -1, Cost cost = 1) : src(src), dst(dst), cost(cost){}; bool operator<(const Edge& e) const { return this->cost < e.cost; } bool operator>(const Edge& e) const { return this->cost > e.cost; } }; template struct Graph { std::vector>> graph; Graph(int n = 0) : graph(n) {} void span(bool direct, int src, int dst, Cost cost = 1) { graph[src].emplace_back(src, dst, cost); if (!direct) graph[dst].emplace_back(dst, src, cost); } int size() const { return graph.size(); } void clear() { graph.clear(); } void resize(int n) { graph.resize(n); } std::vector>& operator[](int v) { return graph[v]; } std::vector> operator[](int v) const { return graph[v]; } }; template std::vector dijkstra(const Graph& graph, int s) { constexpr Cost INF = std::numeric_limits::max(); std::vector dist(graph.size(), INF); dist[s] = 0; MinHeap> que; que.emplace(0, s); while (!que.empty()) { int v; Cost d; std::tie(d, v) = que.top(); que.pop(); if (d > dist[v]) continue; for (const auto& e : graph[v]) { if (dist[e.dst] <= dist[v] + e.cost) continue; dist[e.dst] = dist[v] + e.cost; que.emplace(dist[e.dst], e.dst); } } return dist; } using lint = long long; constexpr lint INF = 1LL << 60; void solve() { int n, m, r; std::cin >> n >> m >> r; --r; std::vector xs(n); for (auto& x : xs) std::cin >> x; if (std::count_if(xs.begin(), xs.end(), [](auto a) { return a > 0; }) == 1) { std::cout << 0 << "\n"; return; } Graph graph(n); while (m--) { int u, v; lint c; std::cin >> u >> v >> c; graph.span(false, --u, --v, c); } auto rds = dijkstra(graph, r); lint ans = INF; for (int c = 0; c < n; ++c) { auto ds = dijkstra(graph, c); lint cost = 0; for (int v = 0; v < n; ++v) cost += ds[v] * xs[v] * 2; lint red = 0; for (int v = 0; v < n; ++v) { red = std::max(red, ds[v] * std::min(xs[v], 1LL) - rds[v]); } ans = std::min(ans, cost - red); } std::cout << ans << "\n"; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }