#!/usr/bin/env python3.8 # %% import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import numpy as np from scipy.sparse.csgraph import dijkstra from scipy.sparse import csr_matrix # %% N, M, L = map(int, readline().split()) T = np.array(readline().split(), np.int64) ABC = np.array(read().split(), np.int64) A = ABC[::3] B = ABC[1::3] C = ABC[2::3] # %% graph = csr_matrix((C, (A - 1, B - 1)), (N, N)) dist = dijkstra(graph, directed=False) # %% dist = (dist + .5).astype(np.int64) cost = (dist * T[None, :]).sum(axis=1) * 2 if T[L - 1]: cost = cost - dist[L - 1, :] print(cost.min()) else: diff = dist[L - 1][None, :] - dist diff[:, T == 0] = dist[L - 1][:, None] cost += diff.min(axis=1) print(cost.min())