From 29e85834da0f7131a2b878258ec87ea5d56f9c45 Mon Sep 17 00:00:00 2001 From: Donew <47556347+03do-new30@users.noreply.github.com> Date: Wed, 26 Mar 2025 22:18:02 +0900 Subject: [PATCH] =?UTF-8?q?[20250326]=20BOJ=20/=20G3=20/=20=EB=B3=84?= =?UTF-8?q?=EC=9E=90=EB=A6=AC=20=EB=A7=8C=EB=93=A4=EA=B8=B0=20/=20?= =?UTF-8?q?=EC=8B=A0=EB=8F=99=EC=9C=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4 \353\247\214\353\223\244\352\270\260.md" | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 "03do-new30/202503/26 BOJ G3 \353\263\204\354\236\220\353\246\254 \353\247\214\353\223\244\352\270\260.md" diff --git "a/03do-new30/202503/26 BOJ G3 \353\263\204\354\236\220\353\246\254 \353\247\214\353\223\244\352\270\260.md" "b/03do-new30/202503/26 BOJ G3 \353\263\204\354\236\220\353\246\254 \353\247\214\353\223\244\352\270\260.md" new file mode 100644 index 00000000..9937aaca --- /dev/null +++ "b/03do-new30/202503/26 BOJ G3 \353\263\204\354\236\220\353\246\254 \353\247\214\353\223\244\352\270\260.md" @@ -0,0 +1,100 @@ +```java +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +public class Main { + static class Point { + double r; + double c; + public Point (double r, double c) { + this.r = r; + this.c = c; + } + } + + static class Edge implements Comparable{ + int from; + int to; + double weight; + public Edge(int from, int to, double weight) { + this.from = from; + this.to = to; + this.weight = weight; + } + + @Override + public int compareTo(Edge o) { + return Double.compare(this.weight, o.weight); + } + } + static int n; + static int[] parents; + static Point[] points; + static PriorityQueue pq; + + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + n = Integer.parseInt(br.readLine()); + points = new Point[n]; + for (int i = 0; i < n; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + double r = Double.parseDouble(st.nextToken()); + double c = Double.parseDouble(st.nextToken()); + points[i] = new Point(r, c); + } + // 자기 자신을 포함하는 부분집합 구성 + makeSet(); + // Point간의 거리를 계산해서 PQ에 등록 + pq = new PriorityQueue<>(); + for (int i = 0; i < n-1; i++) { + for (int j = i+1; j < n; j++) { + pq.offer(new Edge(i, j, getDist(points[i], points[j]))); + } + } + // ans: 별자리를 만드는 최소 비용 + double ans = 0.0; + int edgeCnt = 0; // Kruskal 알고리즘은 간선이 n-1개 선택되면 종료 + while (!pq.isEmpty() && edgeCnt < n-1) { + Edge e = pq.poll(); + if (union(e.from, e.to)) { // from, to가 서로소 집합에 속한다면 ans에 비용 추가 + ans += e.weight; + edgeCnt++; + } + } + System.out.printf("%.2f\n", ans); + br.close(); + } + + private static void makeSet() { + parents = new int[n]; + for (int i = 0; i < n; i++) { + parents[i] = i; + } + } + + private static int find(int a) { + if (a == parents[a]) return a; + return parents[a] = find(parents[a]); + } + + private static boolean union(int a, int b) { + int aRoot = find(a); + int bRoot = find(b); + if (aRoot == bRoot) return false; + if (aRoot < bRoot) { + parents[bRoot] = aRoot; + } else { + parents[aRoot] = bRoot; + } + return true; + } + + private static double getDist(Point x, Point y) { + return Math.sqrt(Math.pow(x.r - y.r, 2) + Math.pow(x.c - y.c, 2)); + } +} + +```