結果

問題 No.2565 はじめてのおつかい
ユーザー InTheBloomInTheBloom
提出日時 2023-12-02 15:49:52
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 165 ms / 2,000 ms
コード長 1,657 bytes
コンパイル時間 4,369 ms
コンパイル使用メモリ 164,704 KB
実行使用メモリ 13,320 KB
最終ジャッジ日時 2023-12-02 15:50:07
合計ジャッジ時間 9,780 ms
ジャッジサーバーID
(参考情報)
judge12 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 165 ms
13,252 KB
testcase_04 AC 136 ms
13,252 KB
testcase_05 AC 1 ms
6,548 KB
testcase_06 AC 87 ms
13,252 KB
testcase_07 AC 45 ms
7,436 KB
testcase_08 AC 45 ms
9,540 KB
testcase_09 AC 79 ms
13,252 KB
testcase_10 AC 56 ms
11,588 KB
testcase_11 AC 123 ms
13,252 KB
testcase_12 AC 31 ms
6,548 KB
testcase_13 AC 54 ms
11,588 KB
testcase_14 AC 89 ms
13,252 KB
testcase_15 AC 93 ms
13,252 KB
testcase_16 AC 79 ms
13,252 KB
testcase_17 AC 18 ms
6,548 KB
testcase_18 AC 23 ms
6,548 KB
testcase_19 AC 109 ms
13,252 KB
testcase_20 AC 92 ms
13,252 KB
testcase_21 AC 84 ms
13,252 KB
testcase_22 AC 47 ms
9,540 KB
testcase_23 AC 59 ms
11,588 KB
testcase_24 AC 11 ms
6,548 KB
testcase_25 AC 104 ms
13,252 KB
testcase_26 AC 59 ms
11,588 KB
testcase_27 AC 84 ms
13,252 KB
testcase_28 AC 105 ms
13,252 KB
testcase_29 AC 117 ms
13,252 KB
testcase_30 AC 92 ms
13,252 KB
testcase_31 AC 103 ms
13,252 KB
testcase_32 AC 55 ms
11,588 KB
testcase_33 AC 93 ms
13,252 KB
testcase_34 AC 102 ms
13,252 KB
testcase_35 AC 91 ms
13,252 KB
testcase_36 AC 106 ms
13,252 KB
testcase_37 AC 57 ms
11,588 KB
testcase_38 AC 97 ms
13,252 KB
testcase_39 AC 87 ms
13,252 KB
testcase_40 AC 107 ms
13,252 KB
testcase_41 AC 115 ms
13,320 KB
testcase_42 AC 61 ms
11,588 KB
testcase_43 AC 88 ms
13,252 KB
testcase_44 AC 48 ms
9,540 KB
testcase_45 AC 24 ms
6,548 KB
testcase_46 AC 99 ms
13,252 KB
testcase_47 AC 67 ms
10,732 KB
testcase_48 AC 54 ms
9,540 KB
testcase_49 AC 30 ms
6,548 KB
testcase_50 AC 79 ms
11,068 KB
testcase_51 AC 1 ms
6,548 KB
testcase_52 AC 57 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main () {
    int N, M; readln.read(N, M);
    int[][] graph = new int[][](N, 0);
    foreach (i; 0..M) {
        int u, v; readln.read(u, v);
        u--, v--;
        graph[u] ~= v;
    }

    solve(N, M, graph);
}

void solve (int N, int M, int[][] graph) {
    /* 最適ルートは 1 -> N-1 -> N -> 1 or 1 -> N -> N-1 -> 1のどちらか。これを全部BFSで調べる */
    int[] visited = new int[](N);
    DList!int Q;
    void BFS (int begin) {
        visited[] = int.max;
        visited[begin] = 0;
        Q.insertBack(begin);

        while (!Q.empty) {
            auto head = Q.front; Q.removeFront;
            foreach (to; graph[head]) {
                if (visited[to] == int.max) {
                    visited[to] = visited[head] + 1;
                    Q.insertBack(to);
                }
            }
        }

    }

    int ans = int.max;
    { // 1 -> N-1 -> N -> 1
        long candi = 0;
        BFS(0);
        candi += visited[N-2];
        BFS(N-2);
        candi += visited[N-1];
        BFS(N-1);
        candi += visited[0];

        if (candi < int.max) ans = min(ans, cast(int) candi);
    }
    { // 1 -> N -> N-1 -> 1
        long candi = 0;
        BFS(0);
        candi += visited[N-1];
        BFS(N-1);
        candi += visited[N-2];
        BFS(N-2);
        candi += visited[0];

        if (candi < int.max) ans = min(ans, cast(int) candi);
    }

    if (ans == int.max) {
        writeln(-1);
    }
    else {
        writeln(ans);
    }
}

void read (T...) (string S, ref T args) {
    auto buf = S.split;
    foreach (i, ref arg; args) {
        arg = buf[i].to!(typeof(arg));
    }
}
0