結果
問題 | No.3 ビットすごろく |
ユーザー | denderaKawazu |
提出日時 | 2018-03-19 23:55:27 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 201 ms / 5,000 ms |
コード長 | 3,784 bytes |
コンパイル時間 | 2,538 ms |
コンパイル使用メモリ | 90,812 KB |
実行使用メモリ | 56,652 KB |
最終ジャッジ日時 | 2024-07-01 08:57:53 |
合計ジャッジ時間 | 9,186 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 139 ms
53,736 KB |
testcase_01 | AC | 139 ms
53,920 KB |
testcase_02 | AC | 137 ms
53,848 KB |
testcase_03 | AC | 151 ms
53,864 KB |
testcase_04 | AC | 148 ms
53,852 KB |
testcase_05 | AC | 181 ms
56,036 KB |
testcase_06 | AC | 167 ms
54,436 KB |
testcase_07 | AC | 153 ms
54,320 KB |
testcase_08 | AC | 181 ms
54,388 KB |
testcase_09 | AC | 185 ms
56,268 KB |
testcase_10 | AC | 196 ms
56,320 KB |
testcase_11 | AC | 185 ms
56,244 KB |
testcase_12 | AC | 183 ms
56,296 KB |
testcase_13 | AC | 155 ms
54,088 KB |
testcase_14 | AC | 195 ms
56,304 KB |
testcase_15 | AC | 196 ms
56,208 KB |
testcase_16 | AC | 200 ms
56,120 KB |
testcase_17 | AC | 189 ms
56,016 KB |
testcase_18 | AC | 154 ms
54,124 KB |
testcase_19 | AC | 190 ms
56,472 KB |
testcase_20 | AC | 145 ms
54,268 KB |
testcase_21 | AC | 137 ms
54,604 KB |
testcase_22 | AC | 189 ms
56,180 KB |
testcase_23 | AC | 196 ms
56,348 KB |
testcase_24 | AC | 186 ms
56,120 KB |
testcase_25 | AC | 201 ms
56,436 KB |
testcase_26 | AC | 139 ms
54,152 KB |
testcase_27 | AC | 161 ms
53,864 KB |
testcase_28 | AC | 189 ms
55,952 KB |
testcase_29 | AC | 180 ms
56,652 KB |
testcase_30 | AC | 143 ms
54,296 KB |
testcase_31 | AC | 142 ms
54,000 KB |
testcase_32 | AC | 181 ms
56,104 KB |
ソースコード
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 } } }