ALGORITHM/BOJ

[BOJ] 10282 해킹

0298 2022. 6. 27. 22:32

https://www.acmicpc.net/problem/10282

 

10282번: 해킹

최흉최악의 해커 yum3이 네트워크 시설의 한 컴퓨터를 해킹했다! 이제 서로에 의존하는 컴퓨터들은 점차 하나둘 전염되기 시작한다. 어떤 컴퓨터 a가 다른 컴퓨터 b에 의존한다면, b가 감염되면

www.acmicpc.net

2022-06-27


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import 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;
        }
 
        @Override
        public 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<>();
            //init
            for(int i = 0 ; i <= n; i++) list.add(new ArrayList<>());
 
            // d
            for(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로 나타냈다.