結果

問題 No.788 トラックの移動
ユーザー nebukuro09nebukuro09
提出日時 2019-02-08 22:07:26
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 1,172 ms / 2,000 ms
コード長 2,076 bytes
コンパイル時間 942 ms
コンパイル使用メモリ 126,484 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 23:37:36
合計ジャッジ時間 8,494 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,155 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 272 ms
4,376 KB
testcase_05 AC 1,136 ms
4,376 KB
testcase_06 AC 1,172 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 424 ms
4,380 KB
testcase_16 AC 981 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto K = s[2] - 1;
    auto T = readln.split.map!(to!long).array;
    auto G = new Tuple!(int, long)[][](N);
    foreach (_; 0..M) {
        s = readln.split.map!(to!int);
        G[s[0]-1] ~= tuple(s[1]-1, s[2].to!long);
        G[s[1]-1] ~= tuple(s[0]-1, s[2].to!long);
    }

    long ans = 1L << 59;
    auto dist = new long[](N);
    auto pq = new BinaryHeap!(Array!(Tuple!(int, long)), "a[1] > b[1]");
    Tuple!(int, long)[] x;

    {
        dist[] = 1L << 59;
        pq.clear();
        pq.insert(tuple(K, 0L));
        while (!pq.empty) {
            auto n = pq.front[0];
            auto d = pq.front[1];
            pq.removeFront;
            if (dist[n] <= d) continue;
            dist[n] = d;
            foreach (e; G[n]) {
                auto m = e[0];
                auto nd = e[1] + d;
                if (dist[m] <= nd) continue;
                pq.insert(tuple(m, nd));
            }
        }

        foreach (i; 0..N) if (T[i] > 0) x ~= tuple(i, dist[i]);
        x.sort!"a[1] < b[1]";
    }

    foreach (v; 0..N) {
        dist[] = 1L << 59;
        pq.clear();
        pq.insert(tuple(v, 0L));
        while (!pq.empty) {
            auto n = pq.front[0];
            auto d = pq.front[1];
            pq.removeFront;
            if (dist[n] <= d) continue;
            dist[n] = d;
            foreach (e; G[n]) {
                auto m = e[0];
                auto nd = e[1] + d;
                if (dist[m] <= nd) continue;
                pq.insert(tuple(m, nd));
            }
        }

        long tmp = 0;
        foreach (i; 0..N) {
            tmp += T[i] * 2 * dist[i];
        }

        auto y = x.front[0];
        auto z = x.front[1];
        tmp = min(tmp, tmp - dist[y] + z);

        ans = min(ans, tmp);
    }

    ans.writeln;
}
0