[Gold IV] 해킹 - 10282
성능 요약
메모리: 151944 KB, 시간: 860 ms
분류
데이크스트라, 그래프 이론, 최단 경로
제출 일자
2024년 6월 18일 00:56:54
문제 설명
최흉최악의 해커 yum3이 네트워크 시설의 한 컴퓨터를 해킹했다! 이제 서로에 의존하는 컴퓨터들은 점차 하나둘 전염되기 시작한다. 어떤 컴퓨터 a가 다른 컴퓨터 b에 의존한다면, b가 감염되면 그로부터 일정 시간 뒤 a도 감염되고 만다. 이때 b가 a를 의존하지 않는다면, a가 감염되더라도 b는 안전하다.
최흉최악의 해커 yum3이 해킹한 컴퓨터 번호와 각 의존성이 주어질 때, 해킹당한 컴퓨터까지 포함하여 총 몇 대의 컴퓨터가 감염되며 그에 걸리는 시간이 얼마인지 구하는 프로그램을 작성하시오.
입력
첫째 줄에 테스트 케이스의 개수가 주어진다. 테스트 케이스의 개수는 최대 100개이다. 각 테스트 케이스는 다음과 같이 이루어져 있다.
- 첫째 줄에 컴퓨터 개수 n, 의존성 개수 d, 해킹당한 컴퓨터의 번호 c가 주어진다(1 ≤ n ≤ 10,000, 1 ≤ d ≤ 100,000, 1 ≤ c ≤ n).
- 이어서 d개의 줄에 각 의존성을 나타내는 정수 a, b, s가 주어진다(1 ≤ a, b ≤ n, a ≠ b, 0 ≤ s ≤ 1,000). 이는 컴퓨터 a가 컴퓨터 b를 의존하며, 컴퓨터 b가 감염되면 s초 후 컴퓨터 a도 감염됨을 뜻한다.
각 테스트 케이스에서 같은 의존성 (a, b)가 두 번 이상 존재하지 않는다.
출력
각 테스트 케이스마다 한 줄에 걸쳐 총 감염되는 컴퓨터 수, 마지막 컴퓨터가 감염되기까지 걸리는 시간을 공백으로 구분지어 출력한다.
접근 방법
1. 감염된 컴퓨터를 의존하는 컴퓨터에 대해서 감염을 시킨다.
2. 이 때 감염이 빠른순으로 감염을 시키고 하나에 노드에 대해서 나머지 노드에 대한 거리를 구하는 것임으로 다익스트라가 제일 적합하다.
풀이 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
/**
* 25분 컷
*/
public class Main {
static final int INF = 10_000_001;
static class Node implements Comparable<Node>{
int idx;
int weight;
Node next;
public Node(int idx, int weight, Node next) {
this.idx = idx;
this.weight = weight;
this.next = next;
}
@Override
public int compareTo(Node o) {
return this.weight - o.weight;
}
}
static Node[] graph;
static int[] cost;
static int max, count;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PriorityQueue<Node> pq = new PriorityQueue<>();
StringBuilder sb = new StringBuilder();
StringTokenizer st;
int T = Integer.parseInt(br.readLine());
while (T-- > 0) {
st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken()); // 컴퓨터 개수
int D = Integer.parseInt(st.nextToken()); // 의존성 개수
int C = Integer.parseInt(st.nextToken()); // 해킹당한 컴퓨터
graph = new Node[N + 1]; // 의존 관계를 나타내는 그래프
cost = new int[N + 1]; // 각 걸리는 시간
max = count = 0; // 최대시간, 해킹당한 컴퓨터의 개수
Arrays.fill(cost, INF);
cost[C] = 0; // 해킹당한 컴퓨터의 경과시간은 0
// Input
for (int i = 0; i < D; i++) {
st = new StringTokenizer(br.readLine());
int to = Integer.parseInt(st.nextToken()); // 의존되는 컴퓨터
int from = Integer.parseInt(st.nextToken()); // 의존하는 컴퓨터
int weight = Integer.parseInt(st.nextToken()); // 걸리는 비용(시간)
graph[from] = new Node(to, weight, graph[from]);
} // Input End
pq.offer(new Node(C, 0, null));
while (!pq.isEmpty()) {
Node cur = pq.poll();
// 기존에 더 빠르게 간 기록이 있다면 넘어간다.
if (cost[cur.idx] < cur.weight) {
continue;
}
for (Node tmp = graph[cur.idx]; tmp != null; tmp = tmp.next) {
// 현재 이 노드를 거쳐서 가는게 더 빠른 경우 갱신 후 큐에 삽입
if (cost[tmp.idx] > tmp.weight + cost[cur.idx]) {
cost[tmp.idx] = tmp.weight + cost[cur.idx];
pq.offer(new Node(tmp.idx, cost[tmp.idx], null));
}
}
}
getAnswer(N); // 정답 갱신
sb.append(count).append(' ').append(max).append('\n');
}
System.out.println(sb);
}
private static void getAnswer(int n) {
for (int i = 1; i <= n; i++) {
if (cost[i] != INF) {
count++;
max = Math.max(max, cost[i]);
}
}
}
}
'알고리즘 - Baekjoon > Gold' 카테고리의 다른 글
[백준] 14284번 : 간선 이어가기2 Gold5(골드5) - JAVA[자바] (0) | 2024.06.18 |
---|---|
[백준] 2374번 : 같은 수로 만들기 Gold4(골드4) - JAVA[자바] (0) | 2024.06.18 |
[백준] 1253번 : 좋다 Gold4(골드4) - JAVA[자바] (0) | 2024.06.18 |
[백준] 1092번 : 배 Gold5(골드5) - JAVA[자바] (1) | 2024.06.16 |
[백준] 2229번 : 조 짜기 Gold5(골드5) - JAVA[자바] (1) | 2024.06.16 |