#include #define fst(t) std::get<0>(t) #define snd(t) std::get<1>(t) #define thd(t) std::get<2>(t) #define unless(p) if(!(p)) #define until(p) while(!(p)) using ll = std::int64_t; using P = std::tuple; int N, M, L; int t[2100]; std::vector

G[2100]; int dist[2100]; std::priority_queue, std::greater

> pq; bool used[2100]; void dijkstra(int s){ std::fill(dist + 1, dist + 1 + N, std::numeric_limits::max()); dist[s] = 0; pq.emplace(0, s); until(pq.empty()){ int v, d; std::tie(d, v) = pq.top(); pq.pop(); if(d > dist[v]){ continue; } for(auto &e : G[v]){ int w, c; std::tie(w, c) = e; if(dist[w] > d + c){ dist[w] = d + c; pq.emplace(dist[w], w); } } } } int f1(int v){ used[v] = true; int res = 0; if(t[v] > 0){ res = v; } for(auto &e : G[v]){ int w, c; std::tie(w, c) = e; if(!used[w] && dist[w] == dist[v] + c){ int u = f1(w); if(u > 0 && dist[u] > dist[res]){ res = u; } } } return res; } int f2(int v){ used[v] = true; if(t[v] > 0){ return v; } int res = 0; for(auto &e : G[v]){ int w, c; std::tie(w, c) = e; if(!used[w] && dist[v] == dist[w] + c){ int u = f2(w); if(u > 0 && dist[u] > dist[res]){ res = u; } } } return res; } int main(){ std::cin.tie(nullptr); std::ios::sync_with_stdio(false); std::cin >> N >> M >> L; for(int i=1;i<=N;++i){ std::cin >> t[i]; } if(std::count_if(t + 1, t + 1 + N, [](int x){return x > 0;}) <= 1){ std::cout << 0 << std::endl; return 0; } for(int i=0;i> a >> b >> c; G[a].emplace_back(b, c); G[b].emplace_back(a, c); } ll minDist = std::numeric_limits::max(); for(int i=1;i<=N;++i){ // 頂点 i にトラックを集める dijkstra(i); ll d = 0; memset(used, 0, sizeof(used)); int v = f1(L); if(v > 0){ d = 2ll * dist[v] - dist[L]; t[v] -= 1; }else{ d = dist[L]; v = f2(L); if(v > 0){ t[v] -= 1; } } for(int j=1;j<=N;++j){ d += 2ll * t[j] * dist[j]; } minDist = std::min(minDist, d); if(v > 0){ t[v] += 1; } } std::cout << minDist << std::endl; }