結果

問題 No.848 なかよし旅行
ユーザー nebukuro09nebukuro09
提出日時 2019-07-05 21:58:28
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 285 ms / 2,000 ms
コード長 1,708 bytes
コンパイル時間 887 ms
コンパイル使用メモリ 118,192 KB
実行使用メモリ 19,936 KB
最終ジャッジ日時 2023-09-04 01:50:57
合計ジャッジ時間 4,511 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 221 ms
19,196 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 5 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 90 ms
12,580 KB
testcase_12 AC 119 ms
13,056 KB
testcase_13 AC 164 ms
13,828 KB
testcase_14 AC 40 ms
5,896 KB
testcase_15 AC 152 ms
13,684 KB
testcase_16 AC 277 ms
15,952 KB
testcase_17 AC 171 ms
14,256 KB
testcase_18 AC 82 ms
11,980 KB
testcase_19 AC 71 ms
10,480 KB
testcase_20 AC 26 ms
4,376 KB
testcase_21 AC 220 ms
15,072 KB
testcase_22 AC 250 ms
19,936 KB
testcase_23 AC 35 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 285 ms
14,328 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 2 ms
4,376 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;

immutable long MOD = 10^^9 + 7;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto P = s[2] - 1;
    auto Q = s[3] - 1;
    auto T = s[4].to!long;

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

    auto d0 = dijkstra(N, 0, G);
    auto dp = dijkstra(N, P, G);
    auto dq = dijkstra(N, Q, G);

    if (d0[P] + dp[Q] + dq[0] <= T) {
        writeln(T);
        return;
    }

    long ans = -1;

    foreach (a; 0..N) {
        foreach (b; 0..N) {
            long time = d0[a] + max(dp[a] + dp[b], dq[a] + dq[b]) + d0[b];
            if (time > T) continue;
            ans = max(ans, T - max(dp[a] + dp[b], dq[a] + dq[b]));
        }
    }

    writeln(ans);
}

long[] dijkstra(int N, int s, ref Tuple!(int, long)[][] G) {
    auto dist = new long[](N);
    dist[] = 1L << 59;

    auto pq = new BinaryHeap!(Array!(Tuple!(int, long)), "a[1] > b[1]");
    pq.insert(tuple(s, 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 (to; G[n]) {
            auto m = to[0];
            auto nd = d + to[1];
            if (dist[m] <= nd) continue;
            pq.insert(tuple(m, nd));
        }
    }
    return dist;
}
0