Skip to content

Commit 03cf849

Browse files
authored
Merge pull request #1736 from AlgorithmWithGod/zinnnn37
[20251224] BOJ / G2 / 수열과 개구리 / 김민진
2 parents b981512 + eb9f9d2 commit 03cf849

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class BJ_32294_수열과_개구리 {
6+
7+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
9+
private static final StringBuilder sb = new StringBuilder();
10+
private static StringTokenizer st;
11+
12+
private static int N;
13+
private static int[] pos, sec;
14+
private static long[] dist;
15+
private static List<Integer>[] graph;
16+
private static Queue<Node> pq;
17+
18+
private static class Node implements Comparable<Node> {
19+
int val;
20+
long cost;
21+
22+
public Node(int val, long cost) {
23+
this.val = val;
24+
this.cost = cost;
25+
}
26+
27+
@Override
28+
public int compareTo(Node o) {
29+
return Long.compare(cost, o.cost);
30+
}
31+
32+
}
33+
34+
public static void main(String[] args) throws IOException {
35+
init();
36+
sol();
37+
}
38+
39+
private static void init() throws IOException {
40+
N = Integer.parseInt(br.readLine());
41+
42+
graph = new ArrayList[N + 2];
43+
for (int i = 0; i <= N + 1; i++) {
44+
graph[i] = new ArrayList<>();
45+
}
46+
47+
pos = new int[N + 2];
48+
st = new StringTokenizer(br.readLine());
49+
for (int i = 1; i <= N; i++) {
50+
pos[i] = Integer.parseInt(st.nextToken());
51+
graph[Math.min(i + pos[i], N + 1)].add(i);
52+
graph[Math.max(i - pos[i], 0)].add(i);
53+
}
54+
55+
sec = new int[N + 2];
56+
st = new StringTokenizer(br.readLine());
57+
for (int i = 1; i <= N; i++) {
58+
sec[i] = Integer.parseInt(st.nextToken());
59+
}
60+
61+
dist = new long[N + 2];
62+
Arrays.fill(dist, Long.MAX_VALUE);
63+
dist[0] = 0;
64+
dist[N + 1] = 0;
65+
pq = new PriorityQueue<>();
66+
}
67+
68+
private static void sol() throws IOException {
69+
pq.offer(new Node(0, 0));
70+
pq.offer(new Node(N + 1, 0));
71+
72+
while (!pq.isEmpty()) {
73+
Node cur = pq.poll();
74+
75+
if (cur.cost > dist[cur.val]) continue;
76+
77+
for (int next : graph[cur.val]) {
78+
long newDist = sec[next] + cur.cost;
79+
80+
if (newDist < dist[next]) {
81+
dist[next] = newDist;
82+
pq.offer(new Node(next, dist[next]));
83+
}
84+
}
85+
}
86+
87+
for (int i = 1; i <= N; i++) {
88+
sb.append(dist[i]).append(" ");
89+
}
90+
bw.write(sb.toString());
91+
bw.flush();
92+
bw.close();
93+
br.close();
94+
}
95+
96+
}
97+
```

0 commit comments

Comments
 (0)