문제 링크
https://www.acmicpc.net/problem/1916
접근 방법
다익스트라 문제입니다.
소스 코드
import java.util.*;
import java.io.*;
public class Main {
static class Node implements Comparable<Node> {
int v, cost;
public Node(int v, int cost) {
this.v = v;
this.cost = cost;
}
@Override
public int compareTo(Node other) {
return this.cost - other.cost;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int city, bus, s, e;
city = Integer.parseInt(br.readLine());
bus = Integer.parseInt(br.readLine());
boolean[] visited = new boolean[city + 1];
int[] dist = new int[city + 1];
List<Node> []graph = new ArrayList[city + 1];
for (int i = 0; i <= city; i++) {
dist[i] = Integer.MAX_VALUE;
graph[i] = new ArrayList<>();
}
for (int i = 0; i < bus; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int a, b, cost;
a = Integer.parseInt(st.nextToken());
b = Integer.parseInt(st.nextToken());
cost = Integer.parseInt(st.nextToken());
graph[a].add(new Node(b, cost));
}
StringTokenizer st = new StringTokenizer(br.readLine());
s = Integer.parseInt(st.nextToken());
e = Integer.parseInt(st.nextToken());
PriorityQueue<Node> pq = new PriorityQueue<>();
dist[s] = 0;
pq.add(new Node(s, 0));
while (!pq.isEmpty()) {
Node cur = pq.poll();
if (visited[cur.v]) {
continue;
} if (cur.v == e) {
break;
}
visited[cur.v] = true;
for (Node next : graph[cur.v]) {
if (!visited[next.v] && (dist[next.v] > dist[cur.v] + next.cost)) {
dist[next.v] = dist[cur.v] + next.cost;
pq.add(new Node(next.v, dist[next.v]));
}
}
}
System.out.println(dist[e]);
}
}
'코딩테스트 > 백준' 카테고리의 다른 글
[백준 JAVA] 14502. 연구소 (0) | 2024.08.28 |
---|---|
[백준 JAVA] 18352. 특정 거리의 도시 찾기 (0) | 2024.08.22 |
[백준 JAVA] 23970. 알고리즘 수업 - 버블 정렬 3 (0) | 2024.08.20 |
[백준 JAVA] 1753. 최단경로 (0) | 2024.08.01 |
[백준 JAVA] 16472. 고냥이 (0) | 2024.07.25 |