-
[BOJ] 10282 해킹ALGORITHM/BOJ 2022. 6. 27. 22:32
https://www.acmicpc.net/problem/10282
2022-06-27
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.*;public class Main10282_Buffered {public static class Hacking implements Comparable<Hacking>{int com;int seconds;public Hacking(int com, int seconds) {this.com = com;this.seconds = seconds;}@Overridepublic int compareTo(Hacking o) {return this.seconds - o.seconds;}}public static List<List<Hacking>> list;public static void main(String[] args) throws IOException {BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(bf.readLine());int T = Integer.parseInt(st.nextToken());for(int ts = 1; ts <= T; ts++) {st = new StringTokenizer(bf.readLine());int n = Integer.parseInt(st.nextToken());int d = Integer.parseInt(st.nextToken());int c = Integer.parseInt(st.nextToken());list = new ArrayList<>();//initfor(int i = 0 ; i <= n; i++) list.add(new ArrayList<>());// dfor(int i = 0; i < d; i++) {st = new StringTokenizer(bf.readLine());int a = Integer.parseInt(st.nextToken());int b = Integer.parseInt(st.nextToken());int s = Integer.parseInt(st.nextToken());list.get(b).add(new Hacking(a, s));}int answer = 1;PriorityQueue<Hacking> pq = new PriorityQueue<>();pq.add(new Hacking(c, 0));int[] dist = new int[n+1];Arrays.fill(dist, 987654321);dist[c] = 0;boolean[] vtd = new boolean[n+1];while(!pq.isEmpty()) {Hacking tmp = pq.poll();if(vtd[tmp.com]) continue;vtd[tmp.com] = true;for(Hacking h: list.get(tmp.com)) {if(dist[h.com] > h.seconds + dist[tmp.com]) {dist[h.com] = h.seconds + dist[tmp.com];pq.add(new Hacking(h.com, dist[h.com]));}}}int max = 0;for(int i = 1; i <= n; i++) {if(i != c && dist[i] != 987654321) {answer++;max = Math.max(max, dist[i]);}}System.out.println(answer + " " + max);}}}cs #문제풀이
다익스트라 문제이다.
a가 b 한테 의존성을 갖고 있으면 s초 후에 감염이 되는 것을 그래프로 b -> a, s로 나타냈다.
'ALGORITHM > BOJ' 카테고리의 다른 글
[BOJ] 1541 잃어버린 괄호 (0) 2022.07.04 [BOJ] 11779 최소비용 구하기2 (0) 2022.06.29 [BOJ] 14938 서강그라운드 (0) 2022.06.26 [BOJ] 2458 키 순서 (0) 2022.06.26 [BOJ] 2164 카드2 (0) 2022.06.20