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