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
67 changes: 67 additions & 0 deletions suyeun84/202508/01 BOJ G5 간선 이어가기 2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
```java
import java.util.*;
import java.io.*;

public class boj14284 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
static int nextInt() {return Integer.parseInt(st.nextToken());}

static int dist[];
static ArrayList<ArrayList<Node>> graph = new ArrayList<>();
static int n, m;

public static void main(String[] args) throws Exception {
nextLine();
n = nextInt();
m = nextInt();
dist = new int[n+1];
for (int i = 0; i < n+1; i++) graph.add(new ArrayList<>());

int a, b, c;
for (int i = 0; i < m; i++) {
nextLine();
a = nextInt();
b = nextInt();
c = nextInt();
graph.get(a).add(new Node(b, c));
graph.get(b).add(new Node(a, c));
}
nextLine();
int s = nextInt();
int t = nextInt();
System.out.println(dijkstra(s, t));
}

static int dijkstra(int s, int t) {
Arrays.fill(dist, Integer.MAX_VALUE);
PriorityQueue<Node> pq = new PriorityQueue<>(
(o1, o2) -> o1.v - o2.v
);
pq.add(new Node(s, 0));

dist[s] = 0;

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

for (Node next : graph.get(cur.e)) {
if (dist[next.e] > dist[cur.e] + next.v) {
dist[next.e] = dist[cur.e] + next.v;
pq.add(new Node(next.e, dist[next.e]));
}
}
}
return dist[t];
}

static class Node {
int e, v;
public Node(int e, int v) {
this.e = e;
this.v = v;
}
}
}
```