// yukicoder: No.788 トラックの移動 // 2019.8.3 bal4u #include #include #include typedef long long ll; #if 1 #define gc() getchar_unlocked() #else #define gc() getchar() #endif int in() { // 非負整数の入力 int n = 0, c = gc(); do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0'); return n; } #define QMAX 150000 typedef struct { int t, s; } QUE; QUE que[QMAX]; int qsize; #define PARENT(i) ((i)>>1) #define LEFT(i) ((i)<<1) #define RIGHT(i) (((i)<<1)+1) void min_heapify(int i) { int l, r, min; l = LEFT(i), r = RIGHT(i); if (l < qsize && que[l].t < que[i].t) min = l; else min = i; if (r < qsize && que[r].t < que[min].t) min = r; if (min != i) { QUE qt = que[i]; que[i] = que[min], que[min] = qt; min_heapify(min); } } void deq() { que[0] = que[--qsize]; min_heapify(0); } void enq(int s, int t) { int i, min; i = qsize++; que[i].s = s, que[i].t = t; while (i > 0 && que[min = PARENT(i)].t > que[i].t) { QUE qt = que[i]; que[i] = que[min], que[min] = qt; i = min; } } #define Inf 0x7fffffffffffffffLL #define inf 0x77777777 int N, M, L; int t[2002]; int dis[2002][2002]; int hi[2002], *to[2002], *len[2002]; void dijkstra(int start, int *dis) { int i, s, e, d, nd; dis[start] = 0, qsize = 0, enq(start, 0); while (qsize) { s = que[0].s, d = que[0].t, deq(); if (dis[s] < d) continue; ; for (i = 0; i < hi[s]; i++) { e = to[s][i], nd = d + len[s][i]; if (dis[e] > nd) dis[e] = nd, enq(e, nd); } } } int main() { int i, j, a, b, c; ll s, ans; int *memo, sz; N = in(), M = in(), L = in() - 1; sz = 0; for (i = 0; i < N; i++) sz += t[i] = in(); if (sz == 1) { puts("0"); return 0; } memo = malloc(3 * sizeof(int)*M); sz = 0; for (i = 0; i < M; i++) { memo[sz++] = a = in() - 1, memo[sz++] = b = in() - 1, memo[sz++] = in(); hi[a]++, hi[b]++; } for (i = 0; i < N; i++) if (hi[i]) { to[i] = malloc(hi[i] * sizeof(int)); len[i] = malloc(hi[i] * sizeof(int)); } memset(hi, 0, sizeof(int)*N); i = 0; while (i < sz) { a = memo[i++], b = memo[i++], c = memo[i++]; j = hi[a]++, to[a][j] = b, len[a][j] = c; j = hi[b]++, to[b][j] = a, len[b][j] = c; } free(memo); memset(dis, inf, sizeof(dis)); for (i = 0; i < N; i++) dijkstra(i, dis[i]); ans = Inf; for (i = 0; i < N; i++) { s = 0, b = -inf; for (j = 0; j < N; j++) if (t[j]) { s += (ll)t[j] * (dis[i][j] << 1); a = dis[i][j] - dis[L][j]; if (a > b) b = a; } s -= b; // printf("[%d] b=%d, s=%lld, ans=%lld\n", i, b, s, ans); if (s < ans) ans = s; } printf("%lld\n", ans); return 0; }