結果
| 問題 |
No.3 ビットすごろく
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-03-19 22:05:37 |
| 言語 | Java (openjdk 23) |
| 結果 |
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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
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);
}
}
}