#pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include using namespace std; typedef long long ll; typedef long double ld; #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPR(i, n) for (int i = n - 1; i >= 0; --i) #define FOR(i, m, n) for (int i = m; i < n; ++i) #define FORR(i, m, n) for (int i = m; i >= n; --i) #define ALL(v) (v).begin(),(v).end() #define ALLR(v) (v).rbegin(),(v).rend() #define SORT(v) sort(ALL(v)) #define RSORT(v) sort(ALLR(v)) #define REV(v) reverse(ALL(v)) #define UNIQUE(x) SORT(x), x.erase(unique(ALL(x)), x.end()) #define fi first #define se second #define PB push_back #define EB emplace_back using pii = pair; using pll = pair; template using vc = vector; template using vvc = vector>; template using vvvc = vector>; using vi = vc; using vl = vc; using vpi = vc; using vpl = vc; // noimi #define INT(...) int __VA_ARGS__;IN(__VA_ARGS__) #define LL(...) ll __VA_ARGS__;IN(__VA_ARGS__) #define STR(...) string __VA_ARGS__;IN(__VA_ARGS__) #define DBL(...) double __VA_ARGS__;IN(__VA_ARGS__) #define VEC(type, name, size) vector name(size);IN(name) #define VEC2(type, name1, name2, size) vector name1(size), name2(size);for(int i = 0; i < size; i++) IN(name1[i], name2[i]) #define VEC3(type, name1, name2, name3, size) vector name1(size), name2(size), name3(size);for(int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i]) #define VEC4(type, name1, name2, name3, name4, size) vector name1(size), name2(size), name3(size), name4(size); for(int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i], name4[i]); #define VV(type, name, h, w) vector> name(h, vector(w));IN(name) int scan() { return getchar(); } void scan(int &a) { cin >> a; } void scan(long long &a) { cin >> a; } void scan(char &a) { cin >> a; } void scan(double &a) { cin >> a; } void scan(string &a) { cin >> a; } template void scan(pair &p) { scan(p.first), scan(p.second); } template void scan(vector &); template void scan(vector &a) {for(auto &i : a) scan(i);} template void scan(T &a) { cin >> a; } void IN() {} template void IN(Head &head, Tail &...tail) {scan(head);IN(tail...);} // tute7627 template using PQ = priority_queue; template using QP = priority_queue,greater>; templatevoid stout(const T &v,ll h,ll w){for(ll i=0;ivoid stout(const T &v,ll n){for(ll i=0;ivoid stout(const vector&v){stout(v,v.size());} templatevoid stout(const vector>&v){for(auto &vv:v)stout(vv,vv.size());} templatevoid debug(const T &v,ll h,ll w){for(ll i=0;ivoid debug(const T &v,ll n){for(ll i=0;ivoid debug(const vector&v){debug(v,v.size());} templatevoid debug(const vector>&v){for(auto &vv:v)debug(vv,vv.size());} templatevoid debug(stack st){while(!st.empty()){cerr<void debug(queue st){while(!st.empty()){cerr<void debug(deque st){while(!st.empty()){cerr<void debug(PQ st){while(!st.empty()){cerr<void debug(QP st){while(!st.empty()){cerr<void debug(const set&v){for(auto z:v)cerr<void debug(const multiset&v){for(auto z:v)cerr<void debug(const array &a){for(auto z:a)cerr<void debug(const map&v){for(auto z:v)cerr<<"["<void OUT(Head&& head, Tail&&... tail){cout << head;if(sizeof...(tail)) cout << " ";OUT(move(tail)...);} void ERR() {cerr << "\n";} template void ERR(Head&& head, Tail&&... tail){cerr << head;if(sizeof...(tail)) cerr << " ";ERR(move(tail)...);} void fsp(int n){cout << fixed << setprecision(n);} templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b // using namespace atcoder; // using mint=modint998244353; // ostream &operator<<(ostream &os, const mint &p) {return os << p.val();} // istream &operator>>(istream &is, mint &a) {int64_t t;is >> t;a = mint(t);return (is);} // using mint=modint1000000007; // Dijkstra template struct Edge { int to; T cost; }; template class Dijkstra { private: vector>> G; int num_v; public: Dijkstra(int num_v) : G(num_v), num_v(num_v) { } void add(int from, int to, T cost) { G[from].push_back((Edge){to, cost}); } // start: Start point // result: result void solve(int start, vector& result) { vector tmp; solve(start, result, tmp); } void solve(int start, vector& result, vector& prev) { result.resize(num_v); std::fill(result.begin(), result.end(), INF); prev.resize(num_v); std::fill(prev.begin(), prev.end(), -1); // get<0>(T): cost // get<1>(T): index // get<2>(T): ... ( for extent) using Tup = tuple; priority_queue, greater> que; que.emplace(0, start); result[start] = 0; while (!que.empty()) { Tup p = que.top(); que.pop(); T t = get<0>(p); int v = get<1>(p); // queueに入れてから別の最短経路が見つかった場合、探索処理は不要 if (result[v] != t) { continue; } // vの各辺に対しよりコストの低い経路があるかを確認 for (auto& e : G[v]) { if (result[e.to] > result[v] + e.cost) { prev[e.to] = v; result[e.to] = result[v] + e.cost; que.emplace(result[v] + e.cost, e.to); } } } } // t: 目的地 vector get_path(const vector& prev, int t) { vector path; for (int v = t; v != -1; v = prev[v]) { path.push_back(v); } reverse(path.begin(), path.end()); return path; } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); LL(n,m,l); l--; Dijkstra dij(n); VEC(ll,t,n); REP(i,m){ INT(a,b,c); a--,b--; dij.add(a,b,c); dij.add(b,a,c); } int cnt=0; REP(i,n) if(t[i]) cnt++; if(cnt==1){ OUT(0); return 0; } vvc d(n,vc(n)); REP(i,n){ dij.solve(i,d[i]); } ll ans=INF; REP(i,n){ ll res=d[l][i]; REP(j,n){ res+=d[i][j]*2*t[j]; } chmin(ans,res); REP(j,n){ if(t[j]){ // ERR(j,d[l][i],d[j][i],d[l][j]); chmin(ans,res-d[l][i]-d[j][i]+d[l][j]); } } // ERR(res); // chmin(ans,res); } OUT(ans); }