|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +public class Main { |
| 5 | + static List<Distance> distance; |
| 6 | + static int[] check; |
| 7 | + static int N; |
| 8 | + public static void main(String[] args) throws Exception{ |
| 9 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 11 | + StringTokenizer str; |
| 12 | + |
| 13 | + N = Integer.valueOf(br.readLine()); |
| 14 | + List<Point> star = new ArrayList<>(); |
| 15 | + distance = new ArrayList<>(); |
| 16 | + check = new int[N]; |
| 17 | + for(int i = 0; i < N; i++){ |
| 18 | + str = new StringTokenizer(br.readLine()); |
| 19 | + star.add(new Point(Double.valueOf(str.nextToken()), Double.valueOf(str.nextToken()), i)); |
| 20 | + check[i] = i; |
| 21 | + } |
| 22 | + |
| 23 | + for(int i = 0; i < N; i++){ |
| 24 | + for(int j = i + 1; j < N; j++){ |
| 25 | + distance.add(new Distance(star.get(i), star.get(j))); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + Collections.sort(distance); |
| 30 | + kruskal(); |
| 31 | + } |
| 32 | + |
| 33 | + private static void kruskal() { |
| 34 | + List<Distance> result = new ArrayList<>(); |
| 35 | + for(int i = 0; i < distance.size(); i++){ |
| 36 | + Distance dist = distance.get(i); |
| 37 | + |
| 38 | + if(find_root(dist.p1.index) == find_root(dist.p2.index)) continue; |
| 39 | + |
| 40 | + union_root(dist); |
| 41 | + result.add(dist); |
| 42 | + |
| 43 | + if(result.size() == N - 1){ |
| 44 | + double sum = 0; |
| 45 | + for(int j = 0; j < result.size(); j++){ |
| 46 | + sum += result.get(j).distance; |
| 47 | + } |
| 48 | + |
| 49 | + System.out.println(sum); |
| 50 | + return; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public static void union_root(Distance dist){ |
| 56 | + int x = find_root(dist.p1.index); |
| 57 | + int y = find_root(dist.p2.index); |
| 58 | + |
| 59 | + if(x != y) check[y] = x; |
| 60 | + } |
| 61 | + |
| 62 | + public static int find_root(int x){ |
| 63 | + if(check[x] == x) return x; |
| 64 | + return check[x] = find_root(check[x]); |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + public static class Point{ |
| 69 | + double x; |
| 70 | + double y; |
| 71 | + int index; |
| 72 | + public Point(double x, double y, int index){ |
| 73 | + this.x = x; |
| 74 | + this.y = y; |
| 75 | + this.index = index; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public static class Distance implements Comparable<Distance>{ |
| 80 | + Point p1; |
| 81 | + Point p2; |
| 82 | + double distance; |
| 83 | + public Distance(Point p1, Point p2){ |
| 84 | + this.p1 = p1; |
| 85 | + this.p2 = p2; |
| 86 | + this.distance = Math.round(Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2)) * 100) / 100.0; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public int compareTo(Distance o){ |
| 91 | + return Double.compare(this.distance, o.distance); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments