結果

問題 No.2565 はじめてのおつかい
ユーザー 👑 kakel-sankakel-san
提出日時 2023-12-02 15:32:22
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 224 ms / 2,000 ms
コード長 1,686 bytes
コンパイル時間 9,198 ms
コンパイル使用メモリ 158,380 KB
実行使用メモリ 209,616 KB
最終ジャッジ日時 2023-12-02 15:32:41
合計ジャッジ時間 17,380 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
33,060 KB
testcase_01 AC 75 ms
33,060 KB
testcase_02 AC 76 ms
33,060 KB
testcase_03 AC 199 ms
62,792 KB
testcase_04 AC 163 ms
62,848 KB
testcase_05 AC 73 ms
33,188 KB
testcase_06 AC 148 ms
54,180 KB
testcase_07 AC 116 ms
44,708 KB
testcase_08 AC 110 ms
43,684 KB
testcase_09 AC 133 ms
52,772 KB
testcase_10 AC 114 ms
45,732 KB
testcase_11 AC 176 ms
62,636 KB
testcase_12 AC 99 ms
42,148 KB
testcase_13 AC 113 ms
45,860 KB
testcase_14 AC 130 ms
53,412 KB
testcase_15 AC 153 ms
54,436 KB
testcase_16 AC 132 ms
58,660 KB
testcase_17 AC 87 ms
38,820 KB
testcase_18 AC 91 ms
40,996 KB
testcase_19 AC 224 ms
62,560 KB
testcase_20 AC 147 ms
56,356 KB
testcase_21 AC 135 ms
56,100 KB
testcase_22 AC 113 ms
46,884 KB
testcase_23 AC 128 ms
48,292 KB
testcase_24 AC 85 ms
35,876 KB
testcase_25 AC 140 ms
55,972 KB
testcase_26 AC 118 ms
47,908 KB
testcase_27 AC 135 ms
56,740 KB
testcase_28 AC 170 ms
62,536 KB
testcase_29 AC 172 ms
62,384 KB
testcase_30 AC 163 ms
62,692 KB
testcase_31 AC 157 ms
57,124 KB
testcase_32 AC 111 ms
47,268 KB
testcase_33 AC 132 ms
57,380 KB
testcase_34 AC 143 ms
56,612 KB
testcase_35 AC 167 ms
62,920 KB
testcase_36 AC 141 ms
57,124 KB
testcase_37 AC 113 ms
47,652 KB
testcase_38 AC 140 ms
57,124 KB
testcase_39 AC 147 ms
52,516 KB
testcase_40 AC 165 ms
62,412 KB
testcase_41 AC 172 ms
62,720 KB
testcase_42 AC 116 ms
48,164 KB
testcase_43 AC 137 ms
54,692 KB
testcase_44 AC 109 ms
44,196 KB
testcase_45 AC 91 ms
39,588 KB
testcase_46 AC 137 ms
56,228 KB
testcase_47 AC 133 ms
50,852 KB
testcase_48 AC 116 ms
46,116 KB
testcase_49 AC 100 ms
41,892 KB
testcase_50 AC 135 ms
55,460 KB
testcase_51 AC 79 ms
33,060 KB
testcase_52 AC 156 ms
209,616 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (114 ms)。
MSBuild のバージョン 17.7.3+8ec440e68 (.NET)
  main -> /home/judge/data/code/bin/Release/net7.0/main.dll
  main -> /home/judge/data/code/bin/Release/net7.0/publish/

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    static int[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray();
    static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, m) = (c[0], c[1]);
        var map = NMap(m);
        var tree = new List<int>[n];
        for (var i = 0; i < tree.Length; ++i) tree[i] = new List<int>();
        foreach (var edge in map) tree[edge[0]].Add(edge[1]);

        var len1 = Len(0, tree);
        var lenn1 = Len(n - 2, tree);
        var lenn = Len(n - 1, tree);
        var ans = Math.Min(len1[n - 2] + lenn1[n - 1] + lenn[0], len1[n - 1] + lenn[n - 2] + lenn1[0]);
        WriteLine(ans >= int.MaxValue / 10 ? -1 : ans);
    }
    static int[] Len(int s, List<int>[] tree)
    {
        var ans = Enumerable.Repeat(int.MaxValue / 10, tree.Length).ToArray();
        ans[s] = 0;
        var q = new Queue<int>();
        q.Enqueue(s);
        while (q.Count > 0)
        {
            var cur = q.Dequeue();
            foreach (var next in tree[cur])
            {
                if (ans[next] <= ans[cur] + 1) continue;
                ans[next] = ans[cur] + 1;
                q.Enqueue(next);
            }
        }
        return ans;
    }
}
0