-
[BOJ] 1753 최단경로ALGORITHM/BOJ 2022. 6. 19. 13:34
https://www.acmicpc.net/problem/1753
2022-06-19
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879import java.util.ArrayList;import java.util.List;import java.util.PriorityQueue;import java.util.Scanner;public class Main1753 {public static int V, E, sx;public static int[] dist;public static List<List<Route>> list;static class Route implements Comparable<Route> {int v;int w;public Route(int v, int w) {this.v = v;this.w = w;}@Overridepublic int compareTo(Route o) {return this.w - o.w;}}public static void solve() {boolean[] vtd = new boolean[V+1];PriorityQueue<Route> pq = new PriorityQueue<>();dist[sx] = 0;pq.add(new Route(sx, 0));while(!pq.isEmpty()) {int cur = pq.poll().v;if(vtd[cur]) continue;vtd[cur] = true;for(Route r: list.get(cur)) {if(dist[r.v] > dist[cur] + r.w) {dist[r.v] = dist[cur] + r.w;pq.add(new Route(r.v, dist[r.v]));}}}}public static void main(String[] args) {Scanner sc = new Scanner(System.in);V = sc.nextInt();E = sc.nextInt();dist = new int[V+1];list = new ArrayList<>();for(int i = 0; i <= V; i++) {list.add(new ArrayList<>());dist[i] = 987654321;}sx = sc.nextInt();for(int i = 0; i < E; i++) {int u = sc.nextInt();int v = sc.nextInt();int w = sc.nextInt();list.get(u).add(new Route(v, w));}solve();StringBuilder sb = new StringBuilder();for(int i = 1; i <= V; i++) {if(dist[i] == 987654321) sb.append("INF");else sb.append(dist[i]);sb.append("\n");}System.out.println(sb.toString());}}cs #문제풀이
다익스트라 문제이다.
'ALGORITHM > BOJ' 카테고리의 다른 글
[BOJ] 11404 플로이드 (0) 2022.06.19 [BOJ] 1916 최소비용 구하기 (0) 2022.06.19 [BOJ] 1018 체스판 다시 칠하기 (0) 2022.06.19 [백준] 10799 쇠막대기 (0) 2022.05.08 [백준] 1193 분수찾기 (0) 2022.02.06