結果
問題 | No.2565 はじめてのおつかい |
ユーザー | tenten |
提出日時 | 2023-12-04 17:22:43 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 527 ms / 2,000 ms |
コード長 | 2,951 bytes |
コンパイル時間 | 2,791 ms |
コンパイル使用メモリ | 92,356 KB |
実行使用メモリ | 65,272 KB |
最終ジャッジ日時 | 2024-09-26 23:04:46 |
合計ジャッジ時間 | 19,654 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 49 ms
37,084 KB |
testcase_01 | AC | 46 ms
37,084 KB |
testcase_02 | AC | 48 ms
37,096 KB |
testcase_03 | AC | 527 ms
62,504 KB |
testcase_04 | AC | 462 ms
65,272 KB |
testcase_05 | AC | 48 ms
37,164 KB |
testcase_06 | AC | 333 ms
50,992 KB |
testcase_07 | AC | 222 ms
49,476 KB |
testcase_08 | AC | 217 ms
47,908 KB |
testcase_09 | AC | 327 ms
50,584 KB |
testcase_10 | AC | 265 ms
49,760 KB |
testcase_11 | AC | 414 ms
54,220 KB |
testcase_12 | AC | 210 ms
47,548 KB |
testcase_13 | AC | 270 ms
49,632 KB |
testcase_14 | AC | 350 ms
52,148 KB |
testcase_15 | AC | 379 ms
51,760 KB |
testcase_16 | AC | 282 ms
52,896 KB |
testcase_17 | AC | 159 ms
44,208 KB |
testcase_18 | AC | 166 ms
44,992 KB |
testcase_19 | AC | 389 ms
53,312 KB |
testcase_20 | AC | 360 ms
52,720 KB |
testcase_21 | AC | 310 ms
52,548 KB |
testcase_22 | AC | 235 ms
48,244 KB |
testcase_23 | AC | 259 ms
49,340 KB |
testcase_24 | AC | 140 ms
42,436 KB |
testcase_25 | AC | 380 ms
53,268 KB |
testcase_26 | AC | 265 ms
49,804 KB |
testcase_27 | AC | 316 ms
53,012 KB |
testcase_28 | AC | 374 ms
53,064 KB |
testcase_29 | AC | 436 ms
54,400 KB |
testcase_30 | AC | 311 ms
52,640 KB |
testcase_31 | AC | 334 ms
53,076 KB |
testcase_32 | AC | 272 ms
49,884 KB |
testcase_33 | AC | 340 ms
53,196 KB |
testcase_34 | AC | 393 ms
52,752 KB |
testcase_35 | AC | 313 ms
53,168 KB |
testcase_36 | AC | 348 ms
53,624 KB |
testcase_37 | AC | 259 ms
49,236 KB |
testcase_38 | AC | 360 ms
52,852 KB |
testcase_39 | AC | 342 ms
51,872 KB |
testcase_40 | AC | 328 ms
53,444 KB |
testcase_41 | AC | 350 ms
55,120 KB |
testcase_42 | AC | 258 ms
49,272 KB |
testcase_43 | AC | 324 ms
51,920 KB |
testcase_44 | AC | 230 ms
48,012 KB |
testcase_45 | AC | 184 ms
46,544 KB |
testcase_46 | AC | 375 ms
52,348 KB |
testcase_47 | AC | 316 ms
49,536 KB |
testcase_48 | AC | 269 ms
49,452 KB |
testcase_49 | AC | 204 ms
47,424 KB |
testcase_50 | AC | 354 ms
51,968 KB |
testcase_51 | AC | 49 ms
37,060 KB |
testcase_52 | AC | 337 ms
51,908 KB |
ソースコード
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(); } }