|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +public class boj10282 { |
| 6 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + static StringTokenizer st; |
| 8 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 9 | + static int nextInt() {return Integer.parseInt(st.nextToken());} |
| 10 | + |
| 11 | + static int INF = Integer.MAX_VALUE; |
| 12 | + |
| 13 | + static ArrayList<Computer>[] list; |
| 14 | + static int[] dist; |
| 15 | + static boolean[] visited; |
| 16 | + public static void main(String[] args) throws Exception { |
| 17 | + nextLine(); |
| 18 | + int T = nextInt(); |
| 19 | + |
| 20 | + for (int t = 0; t < T; t++) { |
| 21 | + nextLine(); |
| 22 | + |
| 23 | + int n = nextInt(); |
| 24 | + int d = nextInt(); |
| 25 | + int c = nextInt(); |
| 26 | + |
| 27 | + list = new ArrayList[n + 1]; |
| 28 | + dist = new int[n + 1]; |
| 29 | + visited = new boolean[n + 1]; |
| 30 | + |
| 31 | + for (int i = 1; i < n + 1; i++) { |
| 32 | + dist[i] = INF; |
| 33 | + list[i] = new ArrayList<>(); |
| 34 | + } |
| 35 | + |
| 36 | + for (int i = 0; i < d; i++) { |
| 37 | + nextLine(); |
| 38 | + int a = Integer.parseInt(st.nextToken()); |
| 39 | + int b = Integer.parseInt(st.nextToken()); |
| 40 | + int s = Integer.parseInt(st.nextToken()); |
| 41 | + |
| 42 | + list[b].add(new Computer(a, s)); |
| 43 | + } |
| 44 | + |
| 45 | + dijkstra(c); |
| 46 | + |
| 47 | + int infection = 0; |
| 48 | + int answer = 0; |
| 49 | + |
| 50 | + for (int i = 1; i < n + 1; i++) { |
| 51 | + if (dist[i] != INF) { |
| 52 | + infection++; |
| 53 | + answer = Math.max(answer, dist[i]); |
| 54 | + } |
| 55 | + } |
| 56 | + System.out.println(infection + " " + answer); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public static void dijkstra(int start) { |
| 61 | + PriorityQueue<Computer> q = new PriorityQueue<>(); |
| 62 | + |
| 63 | + dist[start] = 0; |
| 64 | + q.offer(new Computer(start, 0)); |
| 65 | + |
| 66 | + while (!q.isEmpty()) { |
| 67 | + int cur = q.poll().depend; |
| 68 | + |
| 69 | + if (!visited[cur]) { |
| 70 | + visited[cur] = true; |
| 71 | + |
| 72 | + for (Computer next : list[cur]) { |
| 73 | + if (dist[next.depend] > dist[cur] + next.time) { |
| 74 | + dist[next.depend] = dist[cur] + next.time; |
| 75 | + q.add(new Computer(next.depend, dist[next.depend])); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +class Computer implements Comparable<Computer> { |
| 84 | + int depend; |
| 85 | + int time; |
| 86 | + |
| 87 | + public Computer(int depend, int time) { |
| 88 | + this.depend = depend; |
| 89 | + this.time = time; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public int compareTo(Computer o) { |
| 94 | + return this.time - o.time; |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
0 commit comments