結果
問題 | No.3 ビットすごろく |
ユーザー | denderaKawazu |
提出日時 | 2018-03-19 22:05:37 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,085 ms / 5,000 ms |
コード長 | 3,944 bytes |
コンパイル時間 | 2,354 ms |
コンパイル使用メモリ | 79,980 KB |
実行使用メモリ | 62,560 KB |
最終ジャッジ日時 | 2024-07-01 08:58:33 |
合計ジャッジ時間 | 20,450 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 137 ms
53,932 KB |
testcase_01 | AC | 136 ms
53,732 KB |
testcase_02 | AC | 135 ms
54,024 KB |
testcase_03 | AC | 278 ms
59,768 KB |
testcase_04 | AC | 200 ms
56,832 KB |
testcase_05 | AC | 441 ms
59,332 KB |
testcase_06 | AC | 288 ms
60,028 KB |
testcase_07 | AC | 253 ms
59,104 KB |
testcase_08 | AC | 364 ms
59,752 KB |
testcase_09 | AC | 547 ms
61,916 KB |
testcase_10 | AC | 775 ms
62,400 KB |
testcase_11 | AC | 527 ms
60,012 KB |
testcase_12 | AC | 436 ms
59,760 KB |
testcase_13 | AC | 268 ms
59,948 KB |
testcase_14 | AC | 689 ms
61,964 KB |
testcase_15 | AC | 1,085 ms
62,148 KB |
testcase_16 | AC | 924 ms
62,056 KB |
testcase_17 | AC | 960 ms
62,560 KB |
testcase_18 | AC | 254 ms
59,980 KB |
testcase_19 | AC | 1,074 ms
62,248 KB |
testcase_20 | AC | 170 ms
53,896 KB |
testcase_21 | AC | 136 ms
54,308 KB |
testcase_22 | AC | 746 ms
62,216 KB |
testcase_23 | AC | 1,026 ms
61,368 KB |
testcase_24 | AC | 1,022 ms
62,208 KB |
testcase_25 | AC | 1,041 ms
61,760 KB |
testcase_26 | AC | 134 ms
53,732 KB |
testcase_27 | AC | 290 ms
59,524 KB |
testcase_28 | AC | 869 ms
62,464 KB |
testcase_29 | AC | 568 ms
59,796 KB |
testcase_30 | AC | 143 ms
54,172 KB |
testcase_31 | AC | 155 ms
54,028 KB |
testcase_32 | AC | 492 ms
59,972 KB |
ソースコード
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); } } }