#include using namespace std; #define ALL(x) x.begin(),x.end() #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<bool chmax(T &a,const T &b){if(abool chmin(T &a,const T &b){if(b vector dijkstra(int s,vector>> &G){ const T TINF=numeric_limits::max(); using P=pair; int n=G.size(); vector d(n,TINF); vector b(n,-1);//previous point priority_queue,greater

> q; d[s]=0; q.emplace(d[s],s); while(!q.empty()){ P p=q.top();q.pop(); int v=p.second; if(d[v]d[v]+c){ d[u]=d[v]+c; b[u]=v; q.emplace(d[u],u); } } } return d; } signed main(){ cin.tie(0); ios::sync_with_stdio(0); int n,m,l;cin>>n>>m>>l;l--; vector t(n); int cnt=0; rep(i,n){ cin>>t[i]; cnt+=(t[i]>0); } if(cnt<=1){ cout<<0<>> g(n); rep(i,m){ int u,v;ll c;cin>>u>>v>>c;u--,v--; g[u].push_back({v,c}); g[v].push_back({u,c}); } auto ldis=dijkstra(l,g); ll ans=LINF; // iに集める.jを拾ってレッカーが行く rep(i,n){ auto dis=dijkstra(i,g); ll cost=0; rep(j,n) cost+=2*t[j]*dis[j]; chmin(ans,cost+ldis[i]); rep(j,n)if(t[j])chmin(ans,cost-dis[j]+ldis[j]); } cout<