Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions LiiNi-coder/202508/29 BOJ 택배 배송.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main {
private static BufferedReader br;
private static int n;
private static int m;
private static List<Node>[] graph;
private static int[] dist;

private static class Node implements Comparable<Node> {
int v;
int w;

public Node(int v, int w) {
this.v = v;
this.w = w;
}

@Override
public int compareTo(Node other) {
return this.w - other.w;
}
}

public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
graph = new ArrayList[n + 1];
for (int i = 1; i <= n; i++) {
graph[i] = new ArrayList<>();
}
for (int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());

graph[a].add(new Node(b, c));
graph[b].add(new Node(a, c));
}

dist = new int[n + 1];
Arrays.fill(dist, Integer.MAX_VALUE);

dijkstra(1);

System.out.println(dist[n]);
}

private static void dijkstra(int start) {
PriorityQueue<Node> pq = new PriorityQueue<>();
dist[start] = 0;
pq.add(new Node(start, 0));

while (!pq.isEmpty()) {
Node now = pq.poll();

if (dist[now.v] < now.w) continue;

for (Node next : graph[now.v]) {
if (dist[next.v] > dist[now.v] + next.w) {
dist[next.v] = dist[now.v] + next.w;
pq.add(new Node(next.v, dist[next.v]));
}
}
}
}
}

```