結果
問題 | No.1577 織姫と彦星2 |
ユーザー | tenten |
提出日時 | 2023-01-13 10:57:59 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 429 ms / 2,000 ms |
コード長 | 3,053 bytes |
コンパイル時間 | 2,388 ms |
コンパイル使用メモリ | 85,008 KB |
実行使用メモリ | 58,260 KB |
最終ジャッジ日時 | 2024-06-06 13:11:43 |
合計ジャッジ時間 | 17,805 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 47 ms
37,020 KB |
testcase_01 | AC | 47 ms
37,108 KB |
testcase_02 | AC | 50 ms
37,192 KB |
testcase_03 | AC | 48 ms
37,156 KB |
testcase_04 | AC | 47 ms
37,108 KB |
testcase_05 | AC | 160 ms
46,592 KB |
testcase_06 | AC | 108 ms
42,240 KB |
testcase_07 | AC | 349 ms
52,696 KB |
testcase_08 | AC | 224 ms
47,880 KB |
testcase_09 | AC | 429 ms
58,260 KB |
testcase_10 | AC | 184 ms
46,944 KB |
testcase_11 | AC | 110 ms
41,144 KB |
testcase_12 | AC | 265 ms
49,640 KB |
testcase_13 | AC | 174 ms
45,976 KB |
testcase_14 | AC | 278 ms
50,260 KB |
testcase_15 | AC | 167 ms
46,980 KB |
testcase_16 | AC | 346 ms
52,136 KB |
testcase_17 | AC | 263 ms
48,616 KB |
testcase_18 | AC | 53 ms
36,848 KB |
testcase_19 | AC | 297 ms
51,684 KB |
testcase_20 | AC | 293 ms
50,360 KB |
testcase_21 | AC | 344 ms
52,412 KB |
testcase_22 | AC | 310 ms
51,824 KB |
testcase_23 | AC | 190 ms
47,584 KB |
testcase_24 | AC | 379 ms
52,568 KB |
testcase_25 | AC | 168 ms
46,680 KB |
testcase_26 | AC | 202 ms
47,988 KB |
testcase_27 | AC | 313 ms
50,500 KB |
testcase_28 | AC | 279 ms
50,200 KB |
testcase_29 | AC | 226 ms
48,240 KB |
testcase_30 | AC | 242 ms
48,368 KB |
testcase_31 | AC | 333 ms
51,724 KB |
testcase_32 | AC | 89 ms
39,336 KB |
testcase_33 | AC | 75 ms
38,564 KB |
testcase_34 | AC | 220 ms
48,308 KB |
testcase_35 | AC | 293 ms
51,092 KB |
testcase_36 | AC | 328 ms
51,524 KB |
testcase_37 | AC | 393 ms
54,580 KB |
testcase_38 | AC | 358 ms
54,332 KB |
testcase_39 | AC | 215 ms
47,672 KB |
testcase_40 | AC | 232 ms
47,768 KB |
testcase_41 | AC | 325 ms
51,752 KB |
testcase_42 | AC | 258 ms
49,112 KB |
testcase_43 | AC | 265 ms
49,088 KB |
testcase_44 | AC | 86 ms
39,484 KB |
testcase_45 | AC | 153 ms
45,360 KB |
testcase_46 | AC | 163 ms
46,084 KB |
testcase_47 | AC | 320 ms
50,576 KB |
testcase_48 | AC | 213 ms
47,572 KB |
testcase_49 | AC | 248 ms
48,548 KB |
testcase_50 | AC | 189 ms
47,396 KB |
testcase_51 | AC | 127 ms
42,956 KB |
testcase_52 | AC | 357 ms
53,444 KB |
testcase_53 | AC | 353 ms
52,716 KB |
testcase_54 | AC | 243 ms
48,528 KB |
testcase_55 | AC | 318 ms
52,144 KB |
testcase_56 | AC | 177 ms
46,808 KB |
testcase_57 | AC | 142 ms
44,640 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(); ArrayList<ArrayList<Integer>> graph = new ArrayList<>(); int[] values = new int[n + 2]; HashMap<Integer, Integer> places = new HashMap<>(); for (int i = 0; i < n + 2; i++) { values[i] = sc.nextInt(); places.put(values[i], i); graph.add(new ArrayList<>()); } for (int i = 0; i < n + 2; i++) { for (int j = 0; j <= 30; j++) { if (places.containsKey(values[i] ^ (1 << j))) { int idx = places.get(values[i] ^ (1 << j)); graph.get(i).add(idx); } } } PriorityQueue<Path> queue = new PriorityQueue<>(); queue.add(new Path(0, 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)); } } if (costs[1] == Integer.MAX_VALUE) { System.out.println(-1); } else { System.out.println(costs[1] - 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(); } }