結果
問題 | No.1244 Black Segment |
ユーザー |
![]() |
提出日時 | 2020-12-15 08:44:06 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 1,251 ms / 2,000 ms |
コード長 | 1,765 bytes |
コンパイル時間 | 2,239 ms |
コンパイル使用メモリ | 79,408 KB |
実行使用メモリ | 74,752 KB |
最終ジャッジ日時 | 2024-09-20 01:28:01 |
合計ジャッジ時間 | 31,389 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 36 |
ソースコード
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;}}}