結果

問題 No.788 トラックの移動
ユーザー karinohito
提出日時 2024-09-17 00:56:56
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 497 ms / 2,000 ms
コード長 1,453 bytes
コンパイル時間 2,022 ms
コンパイル使用メモリ 209,264 KB
最終ジャッジ日時 2025-02-24 09:04:14
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
using ll = long long;


int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll N,M,L;
    cin>>N>>M>>L;
    vector<ll> T(N);
    ll cnt=0;
    for(int i=0;i<N;i++){
        cin>>T[i];
        if(T[i]>0)cnt++;
    }
    if(cnt==1){
        cout<<0<<endl;
        return 0;
    }
    vector<vector<pair<ll,ll>>> G(N);
    for(int i=0;i<M;i++){
        ll a,b,c;
        cin>>a>>b>>c;
        a--;b--;
        G[a].push_back({b,c});
        G[b].push_back({a,c});
    }
    ll an=1e18;
    vector<vector<ll>> dist(N);
    for(int i=0;i<N;i++){
        vector<bool> seen(N,false);
        priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> Q;
        vector<ll> D(N,1e18);
        Q.push({0,i});
        D[i]=0;
        while(!Q.empty()){
            auto [c,p]=Q.top();
            Q.pop();
            if(seen[p])continue;
            seen[p]=1;
            for(auto [v,w]:G[p]){
                if(seen[v])continue;
                if(D[v]<=c+w)continue;
                D[v]=c+w;
                Q.push({D[v],v});
            }
        }
        dist[i]=D;
    }
    for(int i=0;i<N;i++){
        ll res=0;
        for(int j=0;j<N;j++){
            res+=dist[i][j]*T[j]*2;
        }
        for(int j=0;j<N;j++){
            if(T[j]>0)an=min(an,res-dist[i][j]*2+dist[i][j]+dist[L-1][j]);
        }
    }
    cout<<an<<endl;
    
}
0