|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class BJ_10282_해킹 { |
| 6 | + |
| 7 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 8 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 9 | + private static final StringBuilder sb = new StringBuilder(); |
| 10 | + private static StringTokenizer st; |
| 11 | + |
| 12 | + private static int N, D, C; |
| 13 | + private static int[] dist; |
| 14 | + private static Queue<Node> pq; |
| 15 | + private static List<Node>[] graph; |
| 16 | + |
| 17 | + private static class Node implements Comparable<Node> { |
| 18 | + int to; |
| 19 | + int weight; |
| 20 | + |
| 21 | + Node(int to, int weight) { |
| 22 | + this.to = to; |
| 23 | + this.weight = weight; |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public int compareTo(Node o) { |
| 28 | + return Integer.compare(this.weight, o.weight); |
| 29 | + } |
| 30 | + |
| 31 | + } |
| 32 | + |
| 33 | + public static void main(String[] args) throws IOException { |
| 34 | + int T = Integer.parseInt(br.readLine()); |
| 35 | + |
| 36 | + while (T-- > 0) { |
| 37 | + init(); |
| 38 | + sol(); |
| 39 | + } |
| 40 | + bw.write(sb.toString()); |
| 41 | + bw.flush(); |
| 42 | + bw.close(); |
| 43 | + br.close(); |
| 44 | + } |
| 45 | + |
| 46 | + private static void init() throws IOException { |
| 47 | + st = new StringTokenizer(br.readLine()); |
| 48 | + N = Integer.parseInt(st.nextToken()); |
| 49 | + D = Integer.parseInt(st.nextToken()); |
| 50 | + C = Integer.parseInt(st.nextToken()); |
| 51 | + |
| 52 | + graph = new List[N + 1]; |
| 53 | + for (int i = 1; i <= N; i++) { |
| 54 | + graph[i] = new ArrayList<>(); |
| 55 | + } |
| 56 | + |
| 57 | + dist = new int[N + 1]; |
| 58 | + Arrays.fill(dist, Integer.MAX_VALUE); |
| 59 | + |
| 60 | + pq = new PriorityQueue<>(); |
| 61 | + for (int i = 0; i < D; i++) { |
| 62 | + st = new StringTokenizer(br.readLine()); |
| 63 | + |
| 64 | + int a = Integer.parseInt(st.nextToken()); |
| 65 | + int b = Integer.parseInt(st.nextToken()); |
| 66 | + int s = Integer.parseInt(st.nextToken()); |
| 67 | + |
| 68 | + graph[b].add(new Node(a, s)); |
| 69 | + } |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + private static void sol() throws IOException { |
| 74 | + pq.offer(new Node(C, 0)); |
| 75 | + dist[C] = 0; |
| 76 | + |
| 77 | + while (!pq.isEmpty()) { |
| 78 | + Node cur = pq.poll(); |
| 79 | + |
| 80 | + if (cur.weight > dist[cur.to]) continue; |
| 81 | + |
| 82 | + for (Node next : graph[cur.to]) { |
| 83 | + int nextWeight = dist[cur.to] + next.weight; |
| 84 | + |
| 85 | + if (dist[next.to] > nextWeight) { |
| 86 | + dist[next.to] = nextWeight; |
| 87 | + pq.offer(new Node(next.to, nextWeight)); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + int cnt = 0; |
| 93 | + int max = 0; |
| 94 | + for (int i = 1; i <= N; i++) { |
| 95 | + if (dist[i] == Integer.MAX_VALUE) continue; |
| 96 | + |
| 97 | + cnt++; |
| 98 | + max = Math.max(max, dist[i]); |
| 99 | + } |
| 100 | + sb.append(cnt).append(" ").append(max).append("\n"); |
| 101 | + } |
| 102 | + |
| 103 | +} |
| 104 | +``` |
0 commit comments