|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +public class Main { |
| 8 | + static class Point { |
| 9 | + double r; |
| 10 | + double c; |
| 11 | + public Point (double r, double c) { |
| 12 | + this.r = r; |
| 13 | + this.c = c; |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + static class Edge implements Comparable<Edge>{ |
| 18 | + int from; |
| 19 | + int to; |
| 20 | + double weight; |
| 21 | + public Edge(int from, int to, double weight) { |
| 22 | + this.from = from; |
| 23 | + this.to = to; |
| 24 | + this.weight = weight; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public int compareTo(Edge o) { |
| 29 | + return Double.compare(this.weight, o.weight); |
| 30 | + } |
| 31 | + } |
| 32 | + static int n; |
| 33 | + static int[] parents; |
| 34 | + static Point[] points; |
| 35 | + static PriorityQueue<Edge> pq; |
| 36 | + |
| 37 | + |
| 38 | + public static void main(String[] args) throws IOException { |
| 39 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 40 | + n = Integer.parseInt(br.readLine()); |
| 41 | + points = new Point[n]; |
| 42 | + for (int i = 0; i < n; i++) { |
| 43 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 44 | + double r = Double.parseDouble(st.nextToken()); |
| 45 | + double c = Double.parseDouble(st.nextToken()); |
| 46 | + points[i] = new Point(r, c); |
| 47 | + } |
| 48 | + // 자기 자신을 포함하는 부분집합 구성 |
| 49 | + makeSet(); |
| 50 | + // Point간의 거리를 계산해서 PQ에 등록 |
| 51 | + pq = new PriorityQueue<>(); |
| 52 | + for (int i = 0; i < n-1; i++) { |
| 53 | + for (int j = i+1; j < n; j++) { |
| 54 | + pq.offer(new Edge(i, j, getDist(points[i], points[j]))); |
| 55 | + } |
| 56 | + } |
| 57 | + // ans: 별자리를 만드는 최소 비용 |
| 58 | + double ans = 0.0; |
| 59 | + int edgeCnt = 0; // Kruskal 알고리즘은 간선이 n-1개 선택되면 종료 |
| 60 | + while (!pq.isEmpty() && edgeCnt < n-1) { |
| 61 | + Edge e = pq.poll(); |
| 62 | + if (union(e.from, e.to)) { // from, to가 서로소 집합에 속한다면 ans에 비용 추가 |
| 63 | + ans += e.weight; |
| 64 | + edgeCnt++; |
| 65 | + } |
| 66 | + } |
| 67 | + System.out.printf("%.2f\n", ans); |
| 68 | + br.close(); |
| 69 | + } |
| 70 | + |
| 71 | + private static void makeSet() { |
| 72 | + parents = new int[n]; |
| 73 | + for (int i = 0; i < n; i++) { |
| 74 | + parents[i] = i; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static int find(int a) { |
| 79 | + if (a == parents[a]) return a; |
| 80 | + return parents[a] = find(parents[a]); |
| 81 | + } |
| 82 | + |
| 83 | + private static boolean union(int a, int b) { |
| 84 | + int aRoot = find(a); |
| 85 | + int bRoot = find(b); |
| 86 | + if (aRoot == bRoot) return false; |
| 87 | + if (aRoot < bRoot) { |
| 88 | + parents[bRoot] = aRoot; |
| 89 | + } else { |
| 90 | + parents[aRoot] = bRoot; |
| 91 | + } |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + private static double getDist(Point x, Point y) { |
| 96 | + return Math.sqrt(Math.pow(x.r - y.r, 2) + Math.pow(x.c - y.c, 2)); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +``` |
0 commit comments