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