Skip to content

Commit 9eac395

Browse files
authored
Merge pull request #773 from AlgorithmWithGod/LiiNi-coder
[20250829] BOJ / G5 / 택배 배송 / 이인희
2 parents 1d1df8f + 6efa040 commit 9eac395

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import java.util.PriorityQueue;
9+
import java.util.StringTokenizer;
10+
11+
public class Main {
12+
private static BufferedReader br;
13+
private static int n;
14+
private static int m;
15+
private static List<Node>[] graph;
16+
private static int[] dist;
17+
18+
private static class Node implements Comparable<Node> {
19+
int v;
20+
int w;
21+
22+
public Node(int v, int w) {
23+
this.v = v;
24+
this.w = w;
25+
}
26+
27+
@Override
28+
public int compareTo(Node other) {
29+
return this.w - other.w;
30+
}
31+
}
32+
33+
public static void main(String[] args) throws IOException {
34+
br = new BufferedReader(new InputStreamReader(System.in));
35+
StringTokenizer st = new StringTokenizer(br.readLine());
36+
n = Integer.parseInt(st.nextToken());
37+
m = Integer.parseInt(st.nextToken());
38+
graph = new ArrayList[n + 1];
39+
for (int i = 1; i <= n; i++) {
40+
graph[i] = new ArrayList<>();
41+
}
42+
for (int i = 0; i < m; i++) {
43+
st = new StringTokenizer(br.readLine());
44+
int a = Integer.parseInt(st.nextToken());
45+
int b = Integer.parseInt(st.nextToken());
46+
int c = Integer.parseInt(st.nextToken());
47+
48+
graph[a].add(new Node(b, c));
49+
graph[b].add(new Node(a, c));
50+
}
51+
52+
dist = new int[n + 1];
53+
Arrays.fill(dist, Integer.MAX_VALUE);
54+
55+
dijkstra(1);
56+
57+
System.out.println(dist[n]);
58+
}
59+
60+
private static void dijkstra(int start) {
61+
PriorityQueue<Node> pq = new PriorityQueue<>();
62+
dist[start] = 0;
63+
pq.add(new Node(start, 0));
64+
65+
while (!pq.isEmpty()) {
66+
Node now = pq.poll();
67+
68+
if (dist[now.v] < now.w) continue;
69+
70+
for (Node next : graph[now.v]) {
71+
if (dist[next.v] > dist[now.v] + next.w) {
72+
dist[next.v] = dist[now.v] + next.w;
73+
pq.add(new Node(next.v, dist[next.v]));
74+
}
75+
}
76+
}
77+
}
78+
}
79+
80+
```

0 commit comments

Comments
 (0)