#include #include #include #include using namespace std; typedef pair PII; typedef long long LL; const int N = 2010, M = 2 * N; const LL INF = 9e18; LL cnt, ans, cnt_a; bool st[N]; priority_queue, greater> pq; int n, m, l, a[N], h[N], e[M], ne[M], w[M], idx, d[N][N]; void Add(int x, int y, int z) { e[idx] = y, w[idx] = z, ne[idx] = h[x], h[x] = idx++; } void Dijkstra(int s) { memset(st, 0, sizeof(st)); pq.push({ 0, s }); while (!pq.empty()) { auto [dist, pos] = pq.top(); pq.pop(); if (st[pos]) continue; st[pos] = true; for (int i = h[pos]; i != -1; i = ne[i]) { int j = e[i]; if (d[s][j] > dist + w[i]) { d[s][j] = dist + w[i]; pq.push({ d[s][j], j }); } } } } int main() { // freopen("truck.in", "r", stdin); // freopen("truck.out", "w", stdout); // 全在一起记得打0 scanf("%d%d%d", &n, &m, &l); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); if (a[i] > 0) ++cnt_a; } memset(h, -1, sizeof(h)); for (int i = 1, x, y, z; i <= m; ++i) { scanf("%d%d%d", &x, &y, &z); Add(x, y, z), Add(y, x, z); } if (cnt_a == 1) { puts("0"); return 0; } memset(d, 0x3f, sizeof(d)); for (int i = 1; i <= n; ++i) d[i][i] = 0; for (int i = 1; i <= n; ++i) { Dijkstra(i); } ans = INF; for (int i = 1; i <= n; ++i) { // 遍历每种集合点 cnt = 0LL; if (i != l && a[l] == 0) { for (int j = 1; j <= n; ++j) { if (j == i) continue; cnt += 2LL * a[j] * d[i][j]; } LL mi = INF; for (int j = 1; j <= n; ++j) { if (!a[j]) continue; mi = min(mi, cnt - d[i][j] + d[l][j]); } ans = min(ans, mi); } else { --a[l]; cnt += d[i][l]; for (int j = 1; j <= n; ++j) { cnt += 2LL * a[j] * d[i][j]; } ++a[l]; ans = min(ans, cnt); } } printf("%lld\n", ans); return 0; }