백준_1753 최단경로 (다익스트라)Algorithm/Algorithm 문제2024. 2. 22. 11:12
Table of Contents
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.StringTokenizer;
public class BJ_1753_최단경로 {
static ArrayList<Edge>[] arr;
static boolean[] visited;
static int[] distance;
static Queue<Edge> queue;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int i;
int v = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(br.readLine());
arr = new ArrayList[v+1];
visited = new boolean[v+1];
distance = new int[v+1];
for (i=1; i<v+1; i++) {
arr[i] = new ArrayList<>();
distance[i] = Integer.MAX_VALUE;
}
int a, b, w;
for (i=0; i<e; i++) {
st = new StringTokenizer(br.readLine());
a = Integer.parseInt(st.nextToken());
b = Integer.parseInt(st.nextToken());
w = Integer.parseInt(st.nextToken());
arr[a].add(new Edge(b,w));
}
queue = new PriorityQueue<>(new Comparator<Edge>() {
@Override
public int compare(Edge o1, Edge o2) {
return o1.value - o2.value;
}
});
distance[k] = 0;
queue.add(new Edge(k, 0));
Edge now;
while (!queue.isEmpty()) {
now = queue.poll();
if (visited[now.vertex]) {
continue;
}
visited[now.vertex] = true;
for (Edge next : arr[now.vertex]) {
if (visited[next.vertex]) {
continue;
}
if (distance[next.vertex] > distance[now.vertex] + next.value) {
distance[next.vertex] = distance[now.vertex] + next.value;
queue.add(new Edge(next.vertex, distance[next.vertex]));
}
}
}
StringBuilder sb = new StringBuilder();
for (i=1; i<v+1; i++) {
if (visited[i]) {
sb.append(distance[i]).append('\n');
} else {
sb.append("INF\n");
}
}
System.out.println(sb);
}
static class Edge {
int vertex;
int value;
public Edge(int vertex, int value) {
this.vertex = vertex;
this.value = value;
}
}
}
'Algorithm > Algorithm 문제' 카테고리의 다른 글
백준_1854 k번째 최단경로 찾기 (다익스트라) (0) | 2024.02.24 |
---|---|
백준_1916 최소비용 구하기 (다익스트라) (0) | 2024.02.23 |
백준_1948 임계경로 (위상정렬, 에지 뒤집기) (0) | 2024.02.21 |
백준_1516 게임 개발 (위상정렬) (0) | 2024.02.20 |
백준_2252 줄 세우기 (위상정렬) (0) | 2024.02.19 |
@kjyyjk :: 녕의 학습 기록
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!