結果

問題 No.3 ビットすごろく
ユーザー denderaKawazudenderaKawazu
提出日時 2018-03-19 23:55:27
言語 Java21
(openjdk 21)
結果
AC  
実行時間 201 ms / 5,000 ms
コード長 3,784 bytes
コンパイル時間 2,599 ms
コンパイル使用メモリ 84,756 KB
実行使用メモリ 58,232 KB
最終ジャッジ日時 2023-09-14 00:57:41
合計ジャッジ時間 9,513 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 133 ms
55,876 KB
testcase_01 AC 130 ms
56,216 KB
testcase_02 AC 134 ms
56,244 KB
testcase_03 AC 159 ms
55,524 KB
testcase_04 AC 141 ms
55,860 KB
testcase_05 AC 182 ms
58,012 KB
testcase_06 AC 178 ms
56,008 KB
testcase_07 AC 149 ms
55,848 KB
testcase_08 AC 184 ms
57,944 KB
testcase_09 AC 184 ms
58,208 KB
testcase_10 AC 193 ms
58,136 KB
testcase_11 AC 185 ms
58,044 KB
testcase_12 AC 183 ms
57,912 KB
testcase_13 AC 151 ms
55,676 KB
testcase_14 AC 195 ms
55,876 KB
testcase_15 AC 201 ms
57,824 KB
testcase_16 AC 198 ms
57,988 KB
testcase_17 AC 195 ms
58,072 KB
testcase_18 AC 146 ms
56,132 KB
testcase_19 AC 199 ms
58,104 KB
testcase_20 AC 137 ms
55,964 KB
testcase_21 AC 132 ms
55,996 KB
testcase_22 AC 196 ms
58,176 KB
testcase_23 AC 196 ms
57,980 KB
testcase_24 AC 192 ms
57,764 KB
testcase_25 AC 196 ms
57,908 KB
testcase_26 AC 131 ms
55,904 KB
testcase_27 AC 159 ms
53,996 KB
testcase_28 AC 200 ms
58,080 KB
testcase_29 AC 191 ms
58,180 KB
testcase_30 AC 136 ms
56,320 KB
testcase_31 AC 138 ms
55,772 KB
testcase_32 AC 191 ms
58,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.PriorityQueue;
import java.util.Collection;
import java.util.Scanner;
import java.util.Set;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Comparator;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(1, in, out);
        out.close();
    }

    static class Task {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            final int n = in.nextInt();

            Vertex[] vertices = new Vertex[n];
            for (int i = 0; i < n; i++) vertices[i] = new Vertex();

            for (int i = 0; i < n; i++) {
                int f = Integer.bitCount(i + 1);
                if (i + f < n) vertices[i].setEdge(vertices[i + f], 1);
                if (i - f >= 0) vertices[i].setEdge(vertices[i - f], 1);
            }

            out.println(Search.dijkstra(vertices[0], vertices[n - 1]));
        }

    }

    static class Vertex {
        private int value;
        private List<Edge> edges = new ArrayList<Edge>();

        public Vertex() {
        }

        public Vertex(int v) {
            this.value = v;
        }

        public void setEdge(Vertex v, int cost) {
            this.edges.add(new Edge(v, cost));
        }

        public int getDegree() {
            return this.edges.size();
        }

        public Edge getEdge(int index) {
            return this.edges.get(index);
        }

    }

    static class Edge {
        private Vertex vertex;
        private int cost;

        public Edge() {
        }

        public Edge(Vertex v) {
            this.vertex = v;
        }

        public Edge(Vertex v, int c) {
            this.vertex = v;
            this.cost = c;
        }

        public Vertex getVertex() {
            return this.vertex;
        }

        public int getCost() {
            return this.cost;
        }

    }

    static class Search {
        public static int dijkstra(Vertex start, Vertex goal) {
            Map<Vertex, Integer> cost = new HashMap<>();
            cost.put(start, 1);

            // initialize priority queue of vertices ordered by cost
            Comparator<Vertex> costComparator = (Vertex o1, Vertex o2) -> ((cost.get(o1) > cost.get(o2)) ? 1 : 0);
            Queue<Vertex> vertexQ = new PriorityQueue<>(costComparator);
            vertexQ.add(start);

            Set<Vertex> visited = new HashSet<>();

            while (vertexQ.size() > 0) {
                Vertex cv = vertexQ.poll();
                if (cv == goal) return cost.get(cv); // found shortest path
                visited.add(cv);

                // see every connected edges
                for (int i = 0; i < cv.getDegree(); i++) {
                    Vertex nextV = cv.getEdge(i).getVertex();
                    if (visited.contains(nextV)) continue;

                    int nextCost = cost.get(cv) + cv.getEdge(i).getCost();
                    // if new cost is smaller, replace it
                    if (nextCost < cost.getOrDefault(nextV, Integer.MAX_VALUE)) {
                        cost.put(nextV, nextCost);
                        if (!vertexQ.contains(nextV)) vertexQ.add(nextV);
                    }
                }
            }
            return -1; // unreachable
        }

    }
}

0