結果

問題 No.788 トラックの移動
ユーザー nebukuro09nebukuro09
提出日時 2019-02-08 22:07:26
言語 D
(dmd 2.099.1)
結果
AC  
実行時間 1,291 ms / 2,000 ms
コード長 2,076 bytes
コンパイル時間 2,281 ms
使用メモリ 3,932 KB
最終ジャッジ日時 2023-01-08 05:09:59
合計ジャッジ時間 10,682 ms
ジャッジサーバーID
(参考情報)
judge16 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 1,287 ms
3,896 KB
testcase_01 AC 1 ms
3,388 KB
testcase_02 AC 1 ms
3,360 KB
testcase_03 AC 1 ms
3,268 KB
testcase_04 AC 305 ms
3,552 KB
testcase_05 AC 1,265 ms
3,780 KB
testcase_06 AC 1,291 ms
3,776 KB
testcase_07 AC 1 ms
3,396 KB
testcase_08 AC 2 ms
3,324 KB
testcase_09 AC 2 ms
3,388 KB
testcase_10 AC 1 ms
3,388 KB
testcase_11 AC 2 ms
3,364 KB
testcase_12 AC 2 ms
3,404 KB
testcase_13 AC 1 ms
3,384 KB
testcase_14 AC 1 ms
3,376 KB
testcase_15 AC 555 ms
3,912 KB
testcase_16 AC 1,111 ms
3,932 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