結果
| 問題 | No.788 トラックの移動 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2019-02-11 09:50:15 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 376 ms / 2,000 ms | 
| コード長 | 1,579 bytes | 
| コンパイル時間 | 1,018 ms | 
| コンパイル使用メモリ | 83,124 KB | 
| 実行使用メモリ | 19,456 KB | 
| 最終ジャッジ日時 | 2024-12-15 07:03:09 | 
| 合計ジャッジ時間 | 3,507 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 14 | 
ソースコード
#include<iostream>
#include<queue>
#include<vector>
#include<climits>
using namespace std;
const int INF = INT_MAX;
typedef long long ll;
int main(){
    int n, m, l;
    cin >> n >> m >> l;
    l--;
    vector<int> t(n);
    for(int i = 0; i < n; i++)  cin >> t[i];
    int cnt = 0;
    for(int i = 0; i < n; i++)  if(t[i])    cnt++;
    if(cnt == 1){
        cout << 0 << endl;
        return 0;
    }
    vector<vector<int>> v(n, vector<int>(n, INF));
    vector<pair<int,int>> path[n];
    int a, b, c;
    while(m-- > 0){
        cin >> a >> b >> c;
        a--, b--;
        path[a].push_back({c,b});
        path[b].push_back({c,a});
    }
    for(int i = 0; i < n; i++){
        priority_queue<pair<int,int>> pq;
        pq.push({-0, i});
        while(!pq.empty()){
            pair<int,int> now = pq.top();   pq.pop();
            int cost = -now.first, pos = now.second;
            if(cost >= v[i][pos])   continue;
            v[i][pos] = cost;
            for(pair<int,int> p : path[pos]){
                if(cost+p.first < v[i][p.second]){
                    pq.push({-(cost+p.first), p.second});
                }
            }
        }
    }
    
    vector<ll> gather(n, 0);
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            gather[i] += 2 * (ll)v[i][j] * t[j];
        }
    }
    
    ll ans = 1ll<<60;
    for(int s = 0; s < n; s++){
        if(t[s] == 0)   continue;
        for(int g = 0; g < n; g++){
            ans = min(ans, gather[g] + v[l][s] - v[s][g]);
        }
    }
    cout << ans << endl;
    return 0;
}
            
            
            
        