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
74 changes: 74 additions & 0 deletions Seol-JY/202507/29 BOJ G5 최소비용 구하기.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
```java
import java.io.*;
import java.util.*;

public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int n = Integer.parseInt(br.readLine());
int m = Integer.parseInt(br.readLine());

ArrayDeque<Edge>[] graph = new ArrayDeque[n + 1];
for (int i = 1; i <= n; i++) {
graph[i] = new ArrayDeque<>();
}

for (int i = 0; i < m; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
int cost = Integer.parseInt(st.nextToken());

graph[from].add(new Edge(to, cost));
}

StringTokenizer st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());

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

PriorityQueue<Edge> pq = new PriorityQueue<>();
pq.offer(new Edge(start, 0));

while (!pq.isEmpty()) {
Edge current = pq.poll();
int currentNode = current.to;
int currentCost = current.cost;

if (currentCost > dist[currentNode]) {
continue;
}

for (Edge next : graph[currentNode]) {
int nextNode = next.to;
int nextCost = currentCost + next.cost;

if (nextCost < dist[nextNode]) {
dist[nextNode] = nextCost;
pq.offer(new Edge(nextNode, nextCost));
}
}
}

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

static class Edge implements Comparable<Edge> {
int to, cost;

public Edge(int to, int cost) {
this.to = to;
this.cost = cost;
}

@Override
public int compareTo(Edge o) {
return Integer.compare(this.cost, o.cost);
}
}
}
```