結果

問題 No.1577 織姫と彦星2
ユーザー tentententen
提出日時 2023-04-25 18:20:05
言語 Java21
(openjdk 21)
結果
AC  
実行時間 514 ms / 2,000 ms
コード長 3,135 bytes
コンパイル時間 3,941 ms
コンパイル使用メモリ 85,420 KB
実行使用メモリ 54,244 KB
最終ジャッジ日時 2024-04-26 22:38:26
合計ジャッジ時間 20,748 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
37,424 KB
testcase_01 AC 55 ms
37,116 KB
testcase_02 AC 58 ms
36,804 KB
testcase_03 AC 54 ms
37,096 KB
testcase_04 AC 55 ms
37,120 KB
testcase_05 AC 195 ms
45,608 KB
testcase_06 AC 139 ms
42,484 KB
testcase_07 AC 419 ms
51,716 KB
testcase_08 AC 246 ms
47,320 KB
testcase_09 AC 514 ms
54,244 KB
testcase_10 AC 248 ms
47,048 KB
testcase_11 AC 118 ms
41,064 KB
testcase_12 AC 314 ms
48,844 KB
testcase_13 AC 211 ms
45,592 KB
testcase_14 AC 358 ms
49,748 KB
testcase_15 AC 213 ms
46,300 KB
testcase_16 AC 433 ms
51,600 KB
testcase_17 AC 310 ms
48,704 KB
testcase_18 AC 59 ms
37,024 KB
testcase_19 AC 400 ms
50,784 KB
testcase_20 AC 359 ms
50,060 KB
testcase_21 AC 422 ms
51,064 KB
testcase_22 AC 370 ms
51,068 KB
testcase_23 AC 226 ms
47,012 KB
testcase_24 AC 426 ms
52,220 KB
testcase_25 AC 202 ms
45,988 KB
testcase_26 AC 252 ms
47,336 KB
testcase_27 AC 365 ms
50,280 KB
testcase_28 AC 332 ms
50,116 KB
testcase_29 AC 270 ms
48,288 KB
testcase_30 AC 299 ms
48,304 KB
testcase_31 AC 401 ms
50,840 KB
testcase_32 AC 110 ms
39,192 KB
testcase_33 AC 78 ms
38,672 KB
testcase_34 AC 277 ms
47,352 KB
testcase_35 AC 386 ms
50,836 KB
testcase_36 AC 373 ms
51,472 KB
testcase_37 AC 478 ms
53,216 KB
testcase_38 AC 469 ms
53,148 KB
testcase_39 AC 260 ms
47,528 KB
testcase_40 AC 261 ms
47,780 KB
testcase_41 AC 407 ms
51,732 KB
testcase_42 AC 315 ms
48,912 KB
testcase_43 AC 316 ms
49,040 KB
testcase_44 AC 104 ms
39,344 KB
testcase_45 AC 177 ms
44,324 KB
testcase_46 AC 190 ms
45,736 KB
testcase_47 AC 381 ms
50,932 KB
testcase_48 AC 267 ms
48,068 KB
testcase_49 AC 283 ms
48,088 KB
testcase_50 AC 236 ms
46,928 KB
testcase_51 AC 145 ms
42,812 KB
testcase_52 AC 449 ms
52,612 KB
testcase_53 AC 427 ms
52,360 KB
testcase_54 AC 290 ms
47,788 KB
testcase_55 AC 404 ms
51,964 KB
testcase_56 AC 210 ms
46,756 KB
testcase_57 AC 164 ms
44,780 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();
        int[] values = new int[n + 2];
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        HashMap<Integer, Integer> idxes = new HashMap<>();
        HashMap<Integer, ArrayList<Integer>> hamings = new HashMap<>();
        for (int i = 0; i < n + 2; i++) {
            values[i] = sc.nextInt();
            idxes.put(values[i], i);
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < n + 2; i++) {
            for (int j = 0; j < 30; j++) {
                values[i] ^= (1 << j);
                if (idxes.containsKey(values[i])) {
                    graph.get(i).add(idxes.get(values[i]));
                }
                values[i] ^= (1 << j);
            }
        }
        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