結果

問題 No.2565 はじめてのおつかい
ユーザー tentententen
提出日時 2023-12-04 17:22:43
言語 Java21
(openjdk 21)
結果
AC  
実行時間 730 ms / 2,000 ms
コード長 2,951 bytes
コンパイル時間 4,074 ms
コンパイル使用メモリ 85,372 KB
実行使用メモリ 80,668 KB
最終ジャッジ日時 2023-12-04 17:23:08
合計ジャッジ時間 24,837 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
53,996 KB
testcase_01 AC 65 ms
53,992 KB
testcase_02 AC 56 ms
53,996 KB
testcase_03 AC 730 ms
80,668 KB
testcase_04 AC 543 ms
80,416 KB
testcase_05 AC 56 ms
53,996 KB
testcase_06 AC 449 ms
64,784 KB
testcase_07 AC 273 ms
65,204 KB
testcase_08 AC 291 ms
63,104 KB
testcase_09 AC 417 ms
64,844 KB
testcase_10 AC 333 ms
64,896 KB
testcase_11 AC 527 ms
68,836 KB
testcase_12 AC 237 ms
62,552 KB
testcase_13 AC 335 ms
64,672 KB
testcase_14 AC 431 ms
66,924 KB
testcase_15 AC 456 ms
66,864 KB
testcase_16 AC 317 ms
67,688 KB
testcase_17 AC 175 ms
59,260 KB
testcase_18 AC 184 ms
59,140 KB
testcase_19 AC 454 ms
66,936 KB
testcase_20 AC 419 ms
66,904 KB
testcase_21 AC 367 ms
66,820 KB
testcase_22 AC 274 ms
62,764 KB
testcase_23 AC 327 ms
64,820 KB
testcase_24 AC 156 ms
58,512 KB
testcase_25 AC 442 ms
66,876 KB
testcase_26 AC 324 ms
64,868 KB
testcase_27 AC 365 ms
66,896 KB
testcase_28 AC 472 ms
66,792 KB
testcase_29 AC 474 ms
68,916 KB
testcase_30 AC 373 ms
66,792 KB
testcase_31 AC 438 ms
66,872 KB
testcase_32 AC 315 ms
64,680 KB
testcase_33 AC 402 ms
66,792 KB
testcase_34 AC 472 ms
66,888 KB
testcase_35 AC 360 ms
66,688 KB
testcase_36 AC 451 ms
68,896 KB
testcase_37 AC 317 ms
64,904 KB
testcase_38 AC 447 ms
66,892 KB
testcase_39 AC 437 ms
66,912 KB
testcase_40 AC 416 ms
67,032 KB
testcase_41 AC 454 ms
68,964 KB
testcase_42 AC 333 ms
64,780 KB
testcase_43 AC 409 ms
66,860 KB
testcase_44 AC 279 ms
63,340 KB
testcase_45 AC 212 ms
60,592 KB
testcase_46 AC 470 ms
66,816 KB
testcase_47 AC 381 ms
62,364 KB
testcase_48 AC 335 ms
64,888 KB
testcase_49 AC 234 ms
62,508 KB
testcase_50 AC 408 ms
66,508 KB
testcase_51 AC 57 ms
53,984 KB
testcase_52 AC 382 ms
66,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            graph.get(sc.nextInt() - 1).add(sc.nextInt() - 1);
        }
        int[] starts = new int[]{0, n - 2, n - 1};
        int[][] costs = new int[3][n];
        ArrayDeque<Path> queue = new ArrayDeque<>();
        for (int i = 0; i < 3; i++) {
            Arrays.fill(costs[i], Integer.MAX_VALUE);
            queue.add(new Path(starts[i], 0));
            while (queue.size() > 0) {
                Path p = queue.poll();
                if (costs[i][p.idx] <= p.value) {
                    continue;
                }
                costs[i][p.idx] = p.value;
                for (int x : graph.get(p.idx)) {
                    queue.add(new Path(x, p.value + 1));
                }
            }
        }
        long strait = (long)costs[0][n - 2] + (long)costs[1][n - 1] + (long)costs[2][0];
        long reverse = (long)costs[0][n - 1] + (long)costs[2][n - 2] + (long)costs[1][0];
        long ans = Math.min(strait, reverse);
        if (ans >= Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(ans);
        }
    }
    
    static class Path {
        int idx;
        int value;
        
        public Path(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
    }
    
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0