結果

問題 No.1577 織姫と彦星2
ユーザー tentententen
提出日時 2023-01-13 10:57:59
言語 Java21
(openjdk 21)
結果
AC  
実行時間 476 ms / 2,000 ms
コード長 3,053 bytes
コンパイル時間 2,634 ms
コンパイル使用メモリ 83,000 KB
実行使用メモリ 68,736 KB
最終ジャッジ日時 2023-08-25 19:02:39
合計ジャッジ時間 21,692 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,668 KB
testcase_01 AC 44 ms
49,688 KB
testcase_02 AC 44 ms
49,652 KB
testcase_03 AC 44 ms
49,924 KB
testcase_04 AC 44 ms
49,568 KB
testcase_05 AC 178 ms
56,560 KB
testcase_06 AC 111 ms
54,880 KB
testcase_07 AC 367 ms
63,928 KB
testcase_08 AC 237 ms
59,628 KB
testcase_09 AC 476 ms
68,736 KB
testcase_10 AC 225 ms
59,288 KB
testcase_11 AC 103 ms
54,628 KB
testcase_12 AC 290 ms
61,348 KB
testcase_13 AC 174 ms
56,252 KB
testcase_14 AC 316 ms
61,440 KB
testcase_15 AC 185 ms
57,684 KB
testcase_16 AC 350 ms
63,372 KB
testcase_17 AC 273 ms
59,400 KB
testcase_18 AC 52 ms
49,812 KB
testcase_19 AC 335 ms
61,516 KB
testcase_20 AC 303 ms
61,644 KB
testcase_21 AC 346 ms
63,444 KB
testcase_22 AC 335 ms
63,284 KB
testcase_23 AC 220 ms
59,452 KB
testcase_24 AC 410 ms
63,860 KB
testcase_25 AC 183 ms
56,524 KB
testcase_26 AC 227 ms
59,412 KB
testcase_27 AC 326 ms
61,052 KB
testcase_28 AC 306 ms
61,316 KB
testcase_29 AC 247 ms
59,280 KB
testcase_30 AC 260 ms
59,524 KB
testcase_31 AC 352 ms
63,848 KB
testcase_32 AC 85 ms
51,956 KB
testcase_33 AC 66 ms
51,556 KB
testcase_34 AC 239 ms
59,908 KB
testcase_35 AC 329 ms
61,396 KB
testcase_36 AC 343 ms
61,456 KB
testcase_37 AC 405 ms
65,684 KB
testcase_38 AC 420 ms
64,360 KB
testcase_39 AC 251 ms
59,588 KB
testcase_40 AC 232 ms
59,484 KB
testcase_41 AC 349 ms
61,480 KB
testcase_42 AC 264 ms
59,176 KB
testcase_43 AC 274 ms
59,436 KB
testcase_44 AC 89 ms
51,636 KB
testcase_45 AC 165 ms
56,300 KB
testcase_46 AC 176 ms
56,480 KB
testcase_47 AC 325 ms
61,200 KB
testcase_48 AC 220 ms
58,636 KB
testcase_49 AC 262 ms
59,452 KB
testcase_50 AC 203 ms
58,924 KB
testcase_51 AC 122 ms
55,320 KB
testcase_52 AC 379 ms
63,656 KB
testcase_53 AC 397 ms
63,452 KB
testcase_54 AC 262 ms
59,252 KB
testcase_55 AC 365 ms
63,344 KB
testcase_56 AC 193 ms
57,120 KB
testcase_57 AC 145 ms
56,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
    }
    
}
0