結果

問題 No.614 壊れたキャンパス
ユーザー nebukuro09nebukuro09
提出日時 2018-01-11 15:07:27
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,066 ms / 2,000 ms
コード長 1,741 bytes
コンパイル時間 2,961 ms
コンパイル使用メモリ 126,684 KB
実行使用メモリ 161,040 KB
最終ジャッジ日時 2023-09-03 18:04:03
合計ジャッジ時間 15,790 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1,051 ms
97,076 KB
testcase_09 AC 1,002 ms
92,484 KB
testcase_10 AC 831 ms
109,144 KB
testcase_11 AC 1,012 ms
81,016 KB
testcase_12 AC 1,066 ms
81,676 KB
testcase_13 AC 1,024 ms
83,012 KB
testcase_14 AC 1,005 ms
96,256 KB
testcase_15 AC 835 ms
102,556 KB
testcase_16 AC 970 ms
97,036 KB
testcase_17 AC 783 ms
161,040 KB
testcase_18 AC 802 ms
70,492 KB
testcase_19 AC 721 ms
69,200 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;

immutable long INF = 1L << 60;
alias Edge = Tuple!(int, "bld", int, "floor");

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto K = s[2];
    auto S = s[3];
    auto T = s[4];
    auto G = new Edge[][int][](N);
    auto floors = new int[][](N);
    foreach (_; 0..M) {
        s = readln.split.map!(to!int);
        G[s[0]-1][s[1]] ~= Edge(s[0], s[2]);
        floors[s[0]-1] ~= s[1];
        floors[s[0]] ~= s[2];
    }
    floors[0] ~= S;
    floors[N-1] ~= T;

    foreach (i; 0..N) {
        floors[i].sort();
        foreach (j; 0..floors[i].length.to!int-1) {
            G[i][floors[i][j]] ~= Edge(i, floors[i][j+1]);
            G[i][floors[i][j+1]] ~= Edge(i, floors[i][j]);
        }
    }


    auto dist = new long[int][](N);
    foreach (i; 0..N)
        foreach (j; floors[i])
            dist[i][j] = INF;
    
    auto pq = new BinaryHeap!(Array!(Tuple!(int, int, long)), "a[2] > b[2]");
    pq.insert(tuple(0, S, 0L));

    while (!pq.empty) {
        auto tt = pq.front;
        pq.removeFront;
        auto b = tt[0];
        auto f = tt[1];
        auto cost = tt[2];
        if (dist[b][f] <= cost) continue;
        dist[b][f] = cost;
        if (!(f in G[b])) continue;
        
        foreach (e; G[b][f]) {
            auto nb = e.bld;
            auto nf = e.floor;
            auto nc = cost + (b == nb ? abs(f-nf) : 0L);
            if (nc < dist[nb][nf]) pq.insert(tuple(nb, nf, nc));
        }
    }

    if (dist[N-1][T] >= INF) dist[N-1][T] = -1;
    dist[N-1][T].writeln; 
}
0