結果
問題 | No.1244 Black Segment |
ユーザー | tenten |
提出日時 | 2023-02-02 08:29:37 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 542 ms / 2,000 ms |
コード長 | 3,043 bytes |
コンパイル時間 | 2,550 ms |
コンパイル使用メモリ | 82,868 KB |
実行使用メモリ | 74,204 KB |
最終ジャッジ日時 | 2023-09-14 16:18:45 |
合計ジャッジ時間 | 16,670 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge11 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 45 ms
49,588 KB |
testcase_01 | AC | 46 ms
49,840 KB |
testcase_02 | AC | 45 ms
49,896 KB |
testcase_03 | AC | 44 ms
48,204 KB |
testcase_04 | AC | 46 ms
47,868 KB |
testcase_05 | AC | 47 ms
49,612 KB |
testcase_06 | AC | 47 ms
49,576 KB |
testcase_07 | AC | 47 ms
49,608 KB |
testcase_08 | AC | 47 ms
49,644 KB |
testcase_09 | AC | 45 ms
49,684 KB |
testcase_10 | AC | 46 ms
49,648 KB |
testcase_11 | AC | 45 ms
49,688 KB |
testcase_12 | AC | 46 ms
49,640 KB |
testcase_13 | AC | 125 ms
52,532 KB |
testcase_14 | AC | 366 ms
60,904 KB |
testcase_15 | AC | 131 ms
53,212 KB |
testcase_16 | AC | 310 ms
58,932 KB |
testcase_17 | AC | 111 ms
52,456 KB |
testcase_18 | AC | 365 ms
63,124 KB |
testcase_19 | AC | 466 ms
65,492 KB |
testcase_20 | AC | 483 ms
65,236 KB |
testcase_21 | AC | 337 ms
65,140 KB |
testcase_22 | AC | 485 ms
65,340 KB |
testcase_23 | AC | 542 ms
65,916 KB |
testcase_24 | AC | 448 ms
65,260 KB |
testcase_25 | AC | 407 ms
63,236 KB |
testcase_26 | AC | 453 ms
65,960 KB |
testcase_27 | AC | 487 ms
74,204 KB |
testcase_28 | AC | 456 ms
65,200 KB |
testcase_29 | AC | 484 ms
67,720 KB |
testcase_30 | AC | 417 ms
65,240 KB |
testcase_31 | AC | 477 ms
67,364 KB |
testcase_32 | AC | 488 ms
68,036 KB |
testcase_33 | AC | 491 ms
67,180 KB |
testcase_34 | AC | 282 ms
60,488 KB |
testcase_35 | AC | 251 ms
58,048 KB |
testcase_36 | AC | 326 ms
60,960 KB |
testcase_37 | AC | 486 ms
65,484 KB |
testcase_38 | AC | 499 ms
67,892 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(); } }