이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
import java.util.*;
import java.io.*;
import java.math.*;
public class skyscraper {
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
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;
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 {
Reader r = new Reader();
int n = r.nextInt(), m = r.nextInt(), p0 = -1, p1 = -1;
Graph g = new Graph(n);
for (int i = 0; i < m; i++) {
int b = r.nextInt(), p = r.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... |