Skip to content

Commit ebfcc13

Browse files
committed
[20260102] BOJ / G5 / 백도어 / 김민진
1 parent 7abecb6 commit ebfcc13

File tree

1 file changed

+108
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)