Skip to content

Commit ddb2f8b

Browse files
authored
[20250810] BOJ / P4 / 무자비한 최단 경로 / 권혁준
1 parent 033c836 commit ddb2f8b

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
public class Main {
51+
52+
static IOController io;
53+
54+
//
55+
56+
static class Node {
57+
int idx, val;
58+
Node(int idx, int val) {
59+
this.idx = idx;
60+
this.val = val;
61+
}
62+
63+
@Override
64+
public boolean equals(Object o) {
65+
if (o == null || getClass() != o.getClass())
66+
return false;
67+
Node node = (Node)o;
68+
return idx == node.idx && val == node.val;
69+
}
70+
71+
@Override
72+
public int hashCode() {
73+
return Objects.hash(idx, val);
74+
}
75+
}
76+
77+
static final long INF = (long)1e18 + 7;
78+
79+
static int N, K;
80+
static int[][] x, y;
81+
static int[] xpos, ypos;
82+
static List<Node>[] z;
83+
static int[] rz;
84+
static PriorityQueue<long[]> pq;
85+
static long[] dist;
86+
87+
public static void main(String[] args) throws Exception {
88+
89+
io = new IOController();
90+
91+
init();
92+
solve();
93+
94+
io.close();
95+
96+
}
97+
98+
static void init() throws Exception {
99+
100+
N = io.nextInt();
101+
K = io.nextInt();
102+
x = new int[N][];
103+
y = new int[N][];
104+
z = new List[K];
105+
rz = new int[N];
106+
for(int i=0;i<K;i++) z[i] = new ArrayList<>();
107+
for(int i=0;i<N;i++) {
108+
int X = io.nextInt();
109+
int Y = io.nextInt();
110+
int Z = io.nextInt();
111+
rz[i] = Z;
112+
x[i] = new int[]{i,X};
113+
y[i] = new int[]{i,Y};
114+
z[Z%K].add(new Node(i,Z));
115+
}
116+
117+
}
118+
119+
static void solve() throws Exception {
120+
121+
Arrays.sort(x, (a,b) -> a[1]==b[1] ? a[0]-b[0] : a[1]-b[1]);
122+
Arrays.sort(y, (a,b) -> a[1]==b[1] ? a[0]-b[0] : a[1]-b[1]);
123+
for(int i=0;i<K;i++) Collections.sort(z[i], (a,b) -> a.val==b.val ? a.idx-b.idx : a.val-b.val);
124+
125+
xpos = new int[N];
126+
for(int i=0;i<N;i++) xpos[x[i][0]] = i;
127+
ypos = new int[N];
128+
for(int i=0;i<N;i++) ypos[y[i][0]] = i;
129+
130+
dist = new long[N];
131+
Arrays.fill(dist, INF);
132+
dist[0] = 0;
133+
pq = new PriorityQueue<>((a,b) -> Long.compare(a[0],b[0]));
134+
pq.offer(new long[]{0,0});
135+
while(!pq.isEmpty()) {
136+
long[] cur = pq.poll();
137+
int n = (int)cur[1];
138+
long d = cur[0];
139+
if(d > dist[n]) continue;
140+
z[rz[n]%K].remove(new Node(n, rz[n]));
141+
int X = xpos[n];
142+
if(X>0) process(x[X-1][0], d + x[X][1]-x[X-1][1]);
143+
if(X<N-1) process(x[X+1][0], d + x[X+1][1]-x[X][1]);
144+
int Y = ypos[n];
145+
if(Y>0) process(y[Y-1][0], d + y[Y][1]-y[Y-1][1]);
146+
if(Y<N-1) process(y[Y+1][0], d + y[Y+1][1]-y[Y][1]);
147+
int RZ = rz[n]%K;
148+
int NZ = (K-RZ)%K;
149+
for(Node next:z[NZ]) if(next.idx != n) {
150+
process(next.idx, d + rz[n] + next.val);
151+
}
152+
}
153+
for(int i=0;i<N;i++) io.write(dist[i] + "\n");
154+
155+
}
156+
157+
static void process(int next, long d) {
158+
if(dist[next] > d) {
159+
dist[next] = d;
160+
pq.offer(new long[]{dist[next], next});
161+
}
162+
}
163+
164+
165+
166+
}
167+
```

0 commit comments

Comments
 (0)