Skip to content

Commit a02dc4a

Browse files
authored
[20250810] BOJ / G5 / 택배 배송 / 이준희
1 parent 8336f7e commit a02dc4a

File tree

1 file changed

+76
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)