結果
問題 | No.1244 Black Segment |
ユーザー | tenten |
提出日時 | 2023-02-02 08:29:37 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 589 ms / 2,000 ms |
コード長 | 3,043 bytes |
コンパイル時間 | 2,937 ms |
コンパイル使用メモリ | 86,384 KB |
実行使用メモリ | 68,744 KB |
最終ジャッジ日時 | 2024-07-01 23:26:00 |
合計ジャッジ時間 | 16,697 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 56 ms
37,088 KB |
testcase_01 | AC | 54 ms
37,180 KB |
testcase_02 | AC | 55 ms
37,116 KB |
testcase_03 | AC | 54 ms
36,940 KB |
testcase_04 | AC | 54 ms
36,804 KB |
testcase_05 | AC | 55 ms
37,212 KB |
testcase_06 | AC | 55 ms
37,164 KB |
testcase_07 | AC | 54 ms
37,320 KB |
testcase_08 | AC | 55 ms
37,280 KB |
testcase_09 | AC | 55 ms
37,208 KB |
testcase_10 | AC | 55 ms
37,212 KB |
testcase_11 | AC | 54 ms
37,212 KB |
testcase_12 | AC | 54 ms
37,276 KB |
testcase_13 | AC | 136 ms
40,252 KB |
testcase_14 | AC | 392 ms
52,928 KB |
testcase_15 | AC | 144 ms
40,092 KB |
testcase_16 | AC | 319 ms
48,236 KB |
testcase_17 | AC | 117 ms
39,504 KB |
testcase_18 | AC | 386 ms
53,444 KB |
testcase_19 | AC | 488 ms
54,664 KB |
testcase_20 | AC | 468 ms
54,156 KB |
testcase_21 | AC | 344 ms
53,600 KB |
testcase_22 | AC | 469 ms
53,980 KB |
testcase_23 | AC | 507 ms
55,940 KB |
testcase_24 | AC | 448 ms
54,216 KB |
testcase_25 | AC | 405 ms
52,884 KB |
testcase_26 | AC | 457 ms
55,772 KB |
testcase_27 | AC | 589 ms
68,744 KB |
testcase_28 | AC | 465 ms
55,040 KB |
testcase_29 | AC | 485 ms
56,824 KB |
testcase_30 | AC | 427 ms
53,996 KB |
testcase_31 | AC | 501 ms
56,564 KB |
testcase_32 | AC | 489 ms
57,264 KB |
testcase_33 | AC | 526 ms
56,440 KB |
testcase_34 | AC | 298 ms
47,212 KB |
testcase_35 | AC | 274 ms
45,924 KB |
testcase_36 | AC | 338 ms
50,512 KB |
testcase_37 | AC | 499 ms
55,640 KB |
testcase_38 | AC | 515 ms
57,132 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(); int a = sc.nextInt(); int b = sc.nextInt(); int size = b + 1 - a + 1; ArrayList<ArrayList<Integer>> graph = new ArrayList<>(); for (int i = 0; i < size; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < m; i++) { int left = Math.max(sc.nextInt(), a); int right = Math.min(sc.nextInt() + 1, b + 1); if (right < a || left > b + 1) { continue; } graph.get(left - a).add(right - a); graph.get(right - a).add(left - a); } PriorityQueue<Path> queue = new PriorityQueue<>(); queue.add(new Path(0, 0)); int[] costs = new int[size]; Arrays.fill(costs, Integer.MAX_VALUE); while (queue.size() > 0) { Path p = queue.poll(); if (costs[p.idx] <= p.value) { continue; } costs[p.idx] = p.value; for (int x : graph.get(p.idx)) { queue.add(new Path(x, p.value + 1)); } } if (costs[size - 1] == Integer.MAX_VALUE) { System.out.println(-1); } else { System.out.println(costs[size - 1]); } } static class Path implements Comparable<Path> { int idx; int value; public Path(int idx, int value) { this.idx = idx; this.value = value; } public int compareTo(Path another) { return value - another.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(); } }