結果

問題 No.1244 Black Segment
ユーザー tentententen
提出日時 2020-12-15 08:44:06
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,305 ms / 2,000 ms
コード長 1,765 bytes
コンパイル時間 2,661 ms
コンパイル使用メモリ 80,208 KB
実行使用メモリ 88,180 KB
最終ジャッジ日時 2023-10-20 05:46:41
合計ジャッジ時間 35,277 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 133 ms
55,404 KB
testcase_01 AC 136 ms
57,588 KB
testcase_02 AC 135 ms
57,564 KB
testcase_03 AC 133 ms
57,616 KB
testcase_04 AC 138 ms
55,768 KB
testcase_05 AC 145 ms
57,836 KB
testcase_06 AC 144 ms
57,660 KB
testcase_07 AC 141 ms
57,732 KB
testcase_08 AC 140 ms
57,640 KB
testcase_09 AC 137 ms
57,544 KB
testcase_10 AC 139 ms
57,804 KB
testcase_11 AC 136 ms
57,596 KB
testcase_12 AC 138 ms
57,604 KB
testcase_13 AC 262 ms
60,976 KB
testcase_14 AC 1,027 ms
79,012 KB
testcase_15 AC 257 ms
59,584 KB
testcase_16 AC 1,003 ms
79,228 KB
testcase_17 AC 253 ms
60,968 KB
testcase_18 AC 937 ms
78,948 KB
testcase_19 AC 1,164 ms
85,076 KB
testcase_20 AC 1,202 ms
85,524 KB
testcase_21 AC 1,068 ms
85,080 KB
testcase_22 AC 1,185 ms
85,364 KB
testcase_23 AC 1,257 ms
85,220 KB
testcase_24 AC 1,190 ms
85,124 KB
testcase_25 AC 1,162 ms
85,164 KB
testcase_26 AC 1,137 ms
83,940 KB
testcase_27 AC 1,110 ms
85,960 KB
testcase_28 AC 1,203 ms
84,824 KB
testcase_29 AC 1,136 ms
83,992 KB
testcase_30 AC 1,165 ms
84,392 KB
testcase_31 AC 1,150 ms
85,064 KB
testcase_32 AC 1,191 ms
84,912 KB
testcase_33 AC 1,184 ms
85,732 KB
testcase_34 AC 1,305 ms
85,096 KB
testcase_35 AC 1,147 ms
86,528 KB
testcase_36 AC 1,198 ms
87,900 KB
testcase_37 AC 1,217 ms
88,180 KB
testcase_38 AC 1,127 ms
85,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int a = sc.nextInt();
        int b = sc.nextInt();
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        for (int i = 0; i <= n + 1; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            int x = sc.nextInt();
            int y = sc.nextInt() + 1;
            graph.get(x).add(y);
            graph.get(y).add(x);
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        for (int i = 1; i <= a; i++) {
            queue.add(new Path(i, 0));
        }
        int[] costs = new int[n + 2];
        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));
            }
        }
        int min = Integer.MAX_VALUE;
        for (int i = b + 1; i < costs.length; i++) {
            min = Math.min(min, costs[i]);
        }
        if (min == Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(min);
        }
    }
    
    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;
        }
    }
}
0