結果

問題 No.3 ビットすごろく
ユーザー denderaKawazudenderaKawazu
提出日時 2018-03-19 22:05:37
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,075 ms / 5,000 ms
コード長 3,944 bytes
コンパイル時間 2,773 ms
コンパイル使用メモリ 76,668 KB
実行使用メモリ 64,288 KB
最終ジャッジ日時 2023-09-14 00:58:28
合計ジャッジ時間 20,283 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,700 KB
testcase_01 AC 125 ms
56,168 KB
testcase_02 AC 128 ms
56,264 KB
testcase_03 AC 276 ms
61,768 KB
testcase_04 AC 193 ms
58,660 KB
testcase_05 AC 450 ms
59,556 KB
testcase_06 AC 280 ms
61,604 KB
testcase_07 AC 252 ms
61,112 KB
testcase_08 AC 360 ms
61,500 KB
testcase_09 AC 612 ms
64,016 KB
testcase_10 AC 755 ms
63,492 KB
testcase_11 AC 509 ms
61,808 KB
testcase_12 AC 418 ms
61,428 KB
testcase_13 AC 258 ms
61,876 KB
testcase_14 AC 736 ms
63,592 KB
testcase_15 AC 1,051 ms
63,440 KB
testcase_16 AC 902 ms
63,928 KB
testcase_17 AC 972 ms
63,508 KB
testcase_18 AC 248 ms
61,444 KB
testcase_19 AC 990 ms
64,288 KB
testcase_20 AC 154 ms
57,568 KB
testcase_21 AC 122 ms
55,820 KB
testcase_22 AC 717 ms
64,052 KB
testcase_23 AC 1,075 ms
63,888 KB
testcase_24 AC 1,005 ms
64,284 KB
testcase_25 AC 1,066 ms
63,752 KB
testcase_26 AC 123 ms
55,948 KB
testcase_27 AC 287 ms
61,268 KB
testcase_28 AC 882 ms
64,244 KB
testcase_29 AC 552 ms
61,604 KB
testcase_30 AC 135 ms
55,504 KB
testcase_31 AC 146 ms
55,540 KB
testcase_32 AC 509 ms
61,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * 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);
            }
            BellmanFord bf = new BellmanFord(vertices[0]);
            if (bf.isReachable(vertices[n - 1])) out.println(1 + bf.getCost(vertices[n - 1]));
            else out.println(-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 from;
        private Vertex to;
        private int cost;

        public Edge() {
        }

        public Edge(Vertex to) {
            this.to = to;
        }

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

        public Edge(Vertex from, Vertex to) {
            this.to = to;
            this.from = from;
        }

        public Edge(Vertex from, Vertex to, int c) {
            this.to = to;
            this.from = from;
            this.cost = c;
        }

        public Vertex getTo() {
            return this.to;
        }

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

    }

    static class BellmanFord {
        private Vertex start;
        private Map<Vertex, Integer> cost = new HashMap<>();
        private Map<Vertex, Edge> route = new HashMap<>();

        public BellmanFord(Vertex start) {
            this.start = start;
            cost.put(start, 0);
            findPath();
        }

        private void findPath() {
            boolean updated = true;
            while (updated) {
                updated = false;
                List<Vertex> vertices = new ArrayList<>();
                vertices.addAll(cost.keySet());
                for (Vertex cv : vertices) {
                    for (int i = 0; i < cv.getDegree(); i++) {
                        Edge ce = cv.getEdge(i);
                        Vertex tv = ce.getTo();
                        int cc = cost.get(cv) + ce.getCost();
                        if (cc < cost.getOrDefault(tv, Integer.MAX_VALUE)) {
                            updated = true;
                            route.put(tv, ce);
                            cost.put(tv, cc);
                        }
                    }
                }
            }
        }

        public int getCost(Vertex goal) {
            return cost.get(goal);
        }

        public boolean isReachable(Vertex v) {
            return cost.containsKey(v);
        }

    }
}

0