이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
import java.util.*;
import java.io.*;
import java.math.*;
class Codechef {
static class FastReader {
StringTokenizer str;
BufferedReader br;
FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
if (str == null || !str.hasMoreTokens()) {
try {
str = new StringTokenizer(br.readLine());
} catch (Exception e) {
return "";
}
}
return str.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
long[] nextLongArray(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
}
static class Node {
int vertex, dist;
Node(int a, int b) {
vertex = a;
dist = b;
}
int getDist() {
return dist;
}
}
static class Graph {
LinkedList<Node> adj[];
int n;
int distance[];
HashSet<Integer> settled;
PriorityQueue<Node> pq;
Graph(int n) {
this.n = n;
adj = new LinkedList[n];
distance = new int[n];
Arrays.fill(distance, Integer.MAX_VALUE);
settled = new HashSet<>(n);
pq = new PriorityQueue<>(Comparator.comparing(Node::getDist));
for (int i = 0; i < n; i++)
adj[i] = new LinkedList<>();
}
void addEdge(int a, int b, int dist) {
adj[a].add(new Node(b, dist));
}
int solve(int u, int v) {
distance[u] = 0;
pq.add(new Node(u, 0));
while (!settled.contains(v)) {
int p = pq.remove().vertex;
if (settled.contains(p))
continue;
settled.add(p);
check_neighbours(p);
}
if (distance[v] == Integer.MAX_VALUE)
return -1;
else
return distance[v];
}
void check_neighbours(int v) {
for (Node i : adj[v]) {
if (!settled.contains(i.vertex)) {
int newDist = distance[v] + i.dist;
distance[i.vertex] = Math.min(distance[i.vertex], newDist);
pq.add(new Node(i.vertex, distance[i.vertex]));
}
}
}
}
public static void main(String args[]) throws Exception {
FastReader fr = new FastReader();
int n = fr.nextInt(), m = fr.nextInt(), p0 = -1, p1 = -1;
Graph g = new Graph(n);
for (int i = 0; i < m; i++) {
int b = fr.nextInt(), p = fr.nextInt();
if (i == 0)
p0 = b;
if (i == 1)
p1 = b;
for (int j = b + p, k = 1; j < n; j += p, k++)
g.addEdge(b, j, k);
for (int j = b - p, k = 1; j >= 0; j -= p, k++)
g.addEdge(b, j, k);
}
System.out.print(g.solve(p0, p1));
}
}
컴파일 시 표준 에러 (stderr) 메시지
Note: skyscraper.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |