-
[BOJ] 1916 최소비용 구하기ALGORITHM/BOJ 2022. 6. 19. 14:48
https://www.acmicpc.net/problem/1916
2022-06-19
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768import java.util.*;public class Main1916 {public static int N, M;public static int[] dist;public static List<List<Bus>> list;static class Bus implements Comparable<Bus> {int v;int w;public Bus(int v, int w) {this.v = v;this.w = w;}@Overridepublic int compareTo(Bus o) {return this.w - o.w;}}public static void solve(int sx) {boolean[] vtd = new boolean[N+1];PriorityQueue<Bus> pq = new PriorityQueue<>();pq.add(new Bus(sx, 0));dist[sx] = 0;while(!pq.isEmpty()) {int cur = pq.poll().v;if(vtd[cur]) continue;vtd[cur] = true;for(Bus b: list.get(cur)) {if(dist[b.v] > dist[cur] + b.w) {dist[b.v] = dist[cur] + b.w;pq.add(new Bus(b.v, dist[b.v]));}}}}public static void main(String[] args) {Scanner sc = new Scanner(System.in);N = sc.nextInt();M = sc.nextInt();dist = new int[N+1];list = new ArrayList<>();Arrays.fill(dist, 987654321);for(int i = 0; i <= N; i++) list.add(new ArrayList<>());for(int i = 0; i < M; i++) {int a = sc.nextInt();int b = sc.nextInt();int c = sc.nextInt();list.get(a).add(new Bus(b, c));}int sx = sc.nextInt();int sy = sc.nextInt();solve(sx);System.out.println(dist[sy]);}}cs #문제풀이
다익스트라 문제이다
'ALGORITHM > BOJ' 카테고리의 다른 글
[BOJ] 1181 단어 정렬 (0) 2022.06.19 [BOJ] 11404 플로이드 (0) 2022.06.19 [BOJ] 1753 최단경로 (0) 2022.06.19 [BOJ] 1018 체스판 다시 칠하기 (0) 2022.06.19 [백준] 10799 쇠막대기 (0) 2022.05.08