結果

問題 No.1565 Union
ユーザー kokatsukokatsu
提出日時 2022-11-30 22:43:45
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 209 ms / 2,000 ms
コード長 657 bytes
コンパイル時間 1,919 ms
コンパイル使用メモリ 204,448 KB
実行使用メモリ 14,976 KB
最終ジャッジ日時 2024-04-27 16:53:44
合計ジャッジ時間 7,156 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 96 ms
6,944 KB
testcase_11 AC 117 ms
10,096 KB
testcase_12 AC 156 ms
7,392 KB
testcase_13 AC 41 ms
6,944 KB
testcase_14 AC 154 ms
8,140 KB
testcase_15 AC 197 ms
10,956 KB
testcase_16 AC 180 ms
10,676 KB
testcase_17 AC 209 ms
10,840 KB
testcase_18 AC 202 ms
11,032 KB
testcase_19 AC 196 ms
10,972 KB
testcase_20 AC 164 ms
10,736 KB
testcase_21 AC 175 ms
11,008 KB
testcase_22 AC 167 ms
10,784 KB
testcase_23 AC 172 ms
11,576 KB
testcase_24 AC 173 ms
11,252 KB
testcase_25 AC 175 ms
14,856 KB
testcase_26 AC 169 ms
12,288 KB
testcase_27 AC 181 ms
14,208 KB
testcase_28 AC 177 ms
14,976 KB
testcase_29 AC 176 ms
12,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main() {
    int N, M;
    readf("%d %d\n", N, M);

    auto edges = new int[][](N+1);
    foreach (_; 0 .. M) {
        int a, b;
        readf("%d %d\n", a, b);

        edges[a] ~= b, edges[b] ~= a;
    }

    auto dists = new int[](N+1);
    dists[] = int.max / 2;
    dists[1] = 0;

    int[] v = [1];
    while (!v.empty) {
        auto f = v.front;
        v.popFront;

        foreach (e; edges[f]) {
            if (dists[e] > dists[f] + 1) {
                dists[e] = dists[f] + 1;
                v ~= e;
            }
        }
    }

    if (dists[N] == int.max / 2) dists[N] = -1;

    int res = dists[N];
    res.writeln;
}
0