[백준 10282] 해킹 (자바)
알고리즘/백준

[백준 10282] 해킹 (자바)

반응형

백준 10282번 해킹 (자바)

 

 

 

출처

www.acmicpc.net/problem/10282

 

10282번: 해킹

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

www.acmicpc.net

 

 

 

문제

최흉최악의 해커 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

 

 

 

걸린 시간

 

 

 

 

접근 방법

다익스트라의 기본 시리즈..!!

 

기존과 다른 점은 의존하는 순서가 다르다.

 

b가 a를 의존한다고 했기 때문에 b에서 a로 가는 시간을 넣어주어야 한다.

그 이후, 다익스트라로 최단 경로들을 구한 후에 개수와 최댓값을 구해준다.

 

 

 

내 코드

package dijkstra;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Hacking {
/**
* 백준 10282 해킹 (https://www.acmicpc.net/problem/10282)
*/
private static int n,d,c;
private final static int INF = (int) 1e9;
private static ArrayList<ArrayList<Node>> graph;
private static int[] dis = new int[10001];
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(reader.readLine());
StringTokenizer st;
//2
//3 2 2
//2 1 5
//3 2 5
//3 3 1
//2 1 2
//3 1 8
//3 2 4
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
st = new StringTokenizer(reader.readLine());
n = Integer.parseInt(st.nextToken());//컴퓨터 개수
d = Integer.parseInt(st.nextToken());//의존성 개수
c = Integer.parseInt(st.nextToken());//해킹당한 컴퓨터 번호
graph = new ArrayList<>();
for (int i=0; i<=n; i++) {
graph.add(new ArrayList<>());
}
for (int i=0; i<d; i++) {
st = new StringTokenizer(reader.readLine());
int a = Integer.parseInt(st.nextToken());//컴퓨터 a가
int b = Integer.parseInt(st.nextToken());//컴퓨터 b를 의존
int s = Integer.parseInt(st.nextToken());//b가 감염되면 s초후 a도 감염됨
graph.get(b).add(new Node(a, s));
}
Arrays.fill(dis,INF);
dijkstra(c);
int cnt = 0;
int result = 0;
for (int i=1; i<=n; i++) {
if (dis[i] != INF) {
cnt++;
result = Math.max(result, dis[i]);
}
}
sb.append(cnt + " " + result + "\n");
}
System.out.print(sb);
}
private static void dijkstra(int start) {
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.add(new Node(start, 0));
dis[start] = 0;
while (!pq.isEmpty()) {
Node node = pq.poll();
int dist = node.distance;
int now = node.index;
if (dis[now] < dist) {
continue;
}
for (int i=0; i<graph.get(now).size(); i++) {
int cost = dis[now] + graph.get(now).get(i).getDistance();
if (cost < dis[graph.get(now).get(i).getIndex()]) {
dis[graph.get(now).get(i).getIndex()] = cost;
pq.add(new Node(graph.get(now).get(i).getIndex(), cost));
}
}
}
}//dijkstra
static class Node implements Comparable<Node> {
int index, distance;
Node (int index, int distance) {
this.index = index;
this.distance = distance;
}
private int getIndex() {
return this.index;
}
private int getDistance() {
return this.distance;
}
@Override
public int compareTo(Node o) {
if (this.distance < o.distance) {
return -1;
}
return 1;
}
}
}
view raw Hacking.java hosted with ❤ by GitHub

 

 

 

고려할 점

1. 다익스트라 알고리즘 사용할 것

 

 

 

반응형