結果

問題 No.1565 Union
ユーザー kokatsukokatsu
提出日時 2022-11-30 22:43:45
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 265 ms / 2,000 ms
コード長 657 bytes
コンパイル時間 4,152 ms
コンパイル使用メモリ 204,496 KB
実行使用メモリ 14,976 KB
最終ジャッジ日時 2024-04-29 23:45:21
合計ジャッジ時間 9,964 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 107 ms
6,656 KB
testcase_11 AC 141 ms
8,576 KB
testcase_12 AC 187 ms
7,424 KB
testcase_13 AC 48 ms
5,376 KB
testcase_14 AC 203 ms
8,192 KB
testcase_15 AC 265 ms
10,880 KB
testcase_16 AC 237 ms
10,368 KB
testcase_17 AC 264 ms
10,880 KB
testcase_18 AC 265 ms
11,008 KB
testcase_19 AC 264 ms
11,008 KB
testcase_20 AC 185 ms
10,752 KB
testcase_21 AC 195 ms
11,136 KB
testcase_22 AC 189 ms
10,752 KB
testcase_23 AC 195 ms
11,264 KB
testcase_24 AC 194 ms
11,264 KB
testcase_25 AC 204 ms
14,464 KB
testcase_26 AC 188 ms
12,288 KB
testcase_27 AC 204 ms
14,208 KB
testcase_28 AC 201 ms
14,976 KB
testcase_29 AC 195 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