結果

問題 No.788 トラックの移動
ユーザー face4face4
提出日時 2019-02-11 09:50:15
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 405 ms / 2,000 ms
コード長 1,579 bytes
コンパイル時間 832 ms
コンパイル使用メモリ 82,528 KB
実行使用メモリ 19,284 KB
最終ジャッジ日時 2023-08-21 17:30:31
合計ジャッジ時間 3,679 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 374 ms
19,208 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 88 ms
7,348 KB
testcase_05 AC 365 ms
19,228 KB
testcase_06 AC 374 ms
19,220 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 84 ms
19,160 KB
testcase_16 AC 405 ms
19,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0