Skip to content

Commit 0cffed4

Browse files
authored
[20251205] BOJ / G5 / 백도어 / 이종환
1 parent 5f7175e commit 0cffed4

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

0224LJH/202512/05 BOJ 백도어.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
```java
2+
3+
import java.util.StringTokenizer;
4+
import java.util.*;
5+
import java.io.*;
6+
7+
public class Main {
8+
9+
static StringBuilder sb = new StringBuilder();
10+
static HashSet<Integer> set = new HashSet<>();
11+
static int nodeCnt,edgeCnt;
12+
static long ans = -1;
13+
static long[] dis;
14+
static Node[] nodes;
15+
static boolean[] canGo,visited;
16+
17+
static class Node implements Comparable<Node>{
18+
int num;
19+
Map<Integer, Integer> to;
20+
21+
public Node (int num) {
22+
this.num = num;
23+
to = new HashMap<>();
24+
}
25+
26+
public int compareTo(Node n) {
27+
return Long.compare(dis[this.num], dis[n.num]);
28+
}
29+
}
30+
31+
32+
public static void main(String[] args) throws IOException {
33+
init();
34+
process();
35+
print();
36+
37+
}
38+
39+
private static void init() throws IOException{
40+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
41+
StringTokenizer st = new StringTokenizer(br.readLine());
42+
nodeCnt = Integer.parseInt(st.nextToken());
43+
edgeCnt = Integer.parseInt(st.nextToken());
44+
45+
nodes = new Node[nodeCnt];
46+
visited = new boolean[nodeCnt];
47+
canGo = new boolean[nodeCnt];
48+
dis = new long[nodeCnt];
49+
Arrays.fill(dis, Long.MAX_VALUE);
50+
dis[0] = 0;
51+
52+
for (int i = 0; i < nodeCnt; i++) {
53+
nodes[i] = new Node(i);
54+
}
55+
56+
st = new StringTokenizer(br.readLine());
57+
for (int i = 0; i < nodeCnt; i++) {
58+
canGo[i] = (Integer.parseInt(st.nextToken()) == 1);
59+
}
60+
61+
for (int i = 0; i < edgeCnt; i++) {
62+
st = new StringTokenizer(br.readLine());
63+
int from = Integer.parseInt(st.nextToken());
64+
int to = Integer.parseInt(st.nextToken());
65+
int cost = Integer.parseInt(st.nextToken());
66+
67+
nodes[from].to.put(to,cost);
68+
nodes[to].to.put(from,cost);
69+
}
70+
71+
}
72+
73+
private static void process() throws IOException {
74+
PriorityQueue<Node> pq = new PriorityQueue<>();
75+
pq.add(nodes[0]);
76+
77+
while(!pq.isEmpty()) {
78+
Node n = pq.poll();
79+
if (visited[n.num]) continue;
80+
visited[n.num] = true;
81+
82+
for (int to: n.to.keySet()) {
83+
if (to != nodeCnt-1 && canGo[to]) {
84+
continue;
85+
}
86+
int cost = n.to.get(to);
87+
if (dis[to] > dis[n.num] + cost) {
88+
dis[to] = dis[n.num] + cost;
89+
pq.add(nodes[to]);
90+
}
91+
}
92+
}
93+
94+
if (dis[nodeCnt-1] != Long.MAX_VALUE) ans = dis[nodeCnt-1];
95+
96+
}
97+
98+
private static void print() {
99+
System.out.println(ans);
100+
}
101+
102+
}
103+
```

0 commit comments

Comments
 (0)