From 4348235984648d0e9145d93bd80e77083e38d6bd Mon Sep 17 00:00:00 2001 From: JHLEE325 <82587652+JHLEE325@users.noreply.github.com> Date: Thu, 11 Sep 2025 08:18:28 +0900 Subject: [PATCH] =?UTF-8?q?[20250911]=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=9D=B4=EC=A4=80=ED=9D=AC?= 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" | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 "JHLEE325/202509/11 BOJ G3 \353\263\204\354\236\220\353\246\254 \353\247\214\353\223\244\352\270\260.md" diff --git "a/JHLEE325/202509/11 BOJ G3 \353\263\204\354\236\220\353\246\254 \353\247\214\353\223\244\352\270\260.md" "b/JHLEE325/202509/11 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..4455f401 --- /dev/null +++ "b/JHLEE325/202509/11 BOJ G3 \353\263\204\354\236\220\353\246\254 \353\247\214\353\223\244\352\270\260.md" @@ -0,0 +1,87 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + + static class node { + double x, y; + + public node(double x, double y) { + this.x = x; + this.y = y; + } + } + + static class edge implements Comparable { + int from, 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[] parent; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st; + + int n = Integer.parseInt(br.readLine()); + node[] stars = new node[n]; + + for (int i = 0; i < n; i++) { + st = new StringTokenizer(br.readLine()); + double x = Double.parseDouble(st.nextToken()); + double y = Double.parseDouble(st.nextToken()); + stars[i] = new node(x, y); + } + + List edges = new ArrayList<>(); + for (int i = 0; i < n; i++) { + for (int j = i + 1; j < n; j++) { + double dist = Math.sqrt(Math.pow(stars[i].x - stars[j].x, 2) + Math.pow(stars[i].y - stars[j].y, 2)); + edges.add(new edge(i, j, dist)); + } + } + + Collections.sort(edges); + + parent = new int[n]; + for (int i = 0; i < n; i++) parent[i] = i; + + double result = 0; + int cnt = 0; + for (edge e : edges) { + if (union(e.from, e.to)) { + result += e.weight; + cnt++; + if (cnt == n - 1) break; + } + } + + System.out.printf("%.2f\n", result); + } + + static int find(int x) { + if (parent[x] == x) return x; + return parent[x] = find(parent[x]); + } + + static boolean union(int a, int b) { + a = find(a); + b = find(b); + if (a == b) return false; + parent[b] = a; + return true; + } +} +```