문제 링크
https://www.acmicpc.net/problem/10282
문제 설명
최흉최악의 해커 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)가 두 번 이상 존재하지 않는다.
출력
각 테스트 케이스마다 한 줄에 걸쳐 총 감염되는 컴퓨터 수, 마지막 컴퓨터가 감염되기까지 걸리는 시간을 공백으로 구분지어 출력한다.
구조화
다익스트라 활용
시작점이 주어지고, 그걸 기점으로 다른 컴퓨터에게 어디까지 전염되냐를 구하는 문제
각 가중치가 다르기때문에 다익스트라로 풀음
bfs로도 클래스를 사용한다면 풀 수 있으려나?
여러 방식으로 풀어봤는데
소스 코드(Comparable + DP)
import java.io.*;
import java.util.*;
public class Main {
static class Node implements Comparable<Node>{
int idx, time;
public Node(int idx, int time) {
this.idx = idx;
this.time = time;
}
@Override
public int compareTo(Node o) {
return this.time - o.time;
}
}
static List<Node>[] list;
static int[] dp;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int d = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken()); // start
dp = new int[n+1];
Arrays.fill(dp, Integer.MAX_VALUE);
list = new List[n+1];
for (int i = 1; i <= n; i++) {
list[i] = new LinkedList<>();
}
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 time = Integer.parseInt(st.nextToken());
list[from].add(new Node(to, time));
}
dijkstra(c);
int cnt = 0;
int max = 0;
for (int i = 1; i <= n; i++) {
if (dp[i]==Integer.MAX_VALUE) continue;
max = Math.max(max, dp[i]);
cnt++;
}
System.out.println(cnt+" "+max);
}
}
static void dijkstra(int start) {
dp[start] = 0;
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.offer(new Node(start, 0));
while (!pq.isEmpty()) {
Node cur = pq.poll();
if (dp[cur.idx] < cur.time) continue;
for (Node next : list[cur.idx]) {
int nextDist = next.time+dp[cur.idx];
if (dp[next.idx] > nextDist) {
dp[next.idx] = nextDist;
pq.offer(new Node(next.idx, nextDist));
}
}
}
}
}
우선순위 큐 자체에서 람다코드로도 해봄(868ms)
근데 클래스에 Comparable 쓰는 게 훨씬 빠름(820ms)
PriorityQueue<Node> pq = new PriorityQueue<>((o1, o2) -> o1.time - o2.time);
방문 배열을 만들어서도 해봄
근데 dp값이 현재 값보다 작으면 이미 방문했어서(갱신) 방문 체크까지 할 수 있기 때문에 방문 배열은 필요없어짐
static void dijkstra(int start) {
dp[start] = 0;
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.offer(new Node(start, 0));
while (!pq.isEmpty()) {
Node cur = pq.poll();
if (v[cur.idx]) continue;
v[cur.idx] = true;
for (Node next : list[cur.idx]) {
if (v[next.idx]) continue;
int nextDist = next.time+dp[cur.idx];
if (dp[next.idx] > nextDist) {
dp[next.idx] = nextDist;
pq.offer(new Node(next.idx, nextDist));
}
}
}
}
'Algorithm > 백준' 카테고리의 다른 글
[백준] PLAYERJINAH’S BOTTLEGROUNDS 🔫 (Java) (0) | 2024.11.21 |
---|---|
[백준] 10093 숫자 🔢 (Java) (1) | 2024.11.20 |
[백준] 1197 최소 스패닝 트리 🎄 (Java) (1) | 2024.11.19 |
[백준] 4659 비밀번호 발음하기 🤫 (Java) (0) | 2024.11.18 |
[백준] 14950 정복자 ⛳️ (Java) (0) | 2024.11.13 |