import sys from itertools import starmap from operator import mul N, M, L = map(int, input().split()) L -= 1 t = list(map(int, input().split())) target = set([i for i, n in enumerate(t) if n]) edges = [[] for _ in [0]*N] for a, b, c in (map(int, l.split()) for l in sys.stdin): edges[a-1].append((b-1, c)) edges[b-1].append((a-1, c)) def dijkstra(v_count: int, edges: list, start: int, *, adj_matrix: bool = False, unreachable=10**18) -> list: """ ダイクストラ :param v_count: 頂点数 :param edges: 辺のリスト(隣接リストor隣接行列) :param start: スタートする頂点 :param adj_matrix: edgesに渡したリストが隣接行列ならTrue :param unreachable: 到達不能を表すコスト値 隣接行列の辺の有無の判定および返すリストの初期値に使用 """ from heapq import heappush, heappop vertices = [unreachable] * v_count vertices[start] = 0 q, rem = [(0, start)], v_count - 1 while q and rem: cost, v = heappop(q) if vertices[v] < cost: continue rem -= 1 for dest, _cost in edges[v]: newcost = cost + _cost if vertices[dest] > newcost: vertices[dest] = newcost heappush(q, (newcost, dest)) return vertices if t[L]: flag = True else: flag = False L_to_i = dijkstra(N, edges, L) ans = 10**18 for i in range(N): costs = dijkstra(N, edges, i) cost = sum(starmap(mul, zip(costs, t)))*2 if flag: cost -= costs[L] else: cost += min(L_to_i[i], min(L_to_i[j]-costs[j] for j in range(N) if t[j])) if ans > cost: ans = cost print(ans)