3.41
import java.util.Arrays;import java.util.Scanner; public class Main{ static Point Points[] = new Point[101]; static int Tree[] = new int[101]; static Edge edges[] = new Edge[6000]; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while( scanner.hasNext() ){ Arrays.fill(Tree, -1); for (int i = 0; i < Points.length; i++) { Points[i] = new Point(); } int n = scanner.nextInt(); for (int i = 1; i <= n; i++) { Points[i].x = scanner.nextDouble(); Points[i].y = scanner.nextDouble(); } int size = 0; for (int i = 1; i <= n; i++) { for (int j = i+1; j <= n; j++) { edges[size] = new Edge(); edges[size].a = i; edges[size].b = j; edges[size].cost = cacluteLen(Points[i],Points[j]); size++; } } quickSortEdgeByCost(0,size-1,edges); double ans = 0; for (int i = 0; i <size; i++) { int a = findRoot(edges[i].a); int b = findRoot(edges[i].b); if(a != b){ Tree[a] = b; ans += edges[i].cost; } } System.out.println(String.format("%.2f",ans)); } } public static class Point{ double x; double y; } public static double cacluteLen(Point p1,Point p2){ double len = 0; len = Math.sqrt( (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y) ); return len; } public static class Edge{ int a; int b; double cost; } public static int findRoot(int x){ if(Tree[x] == -1) return x; else{ int tmp = findRoot(Tree[x]); Tree[x] = tmp; return tmp; } } public static void quickSortEdgeByCost(int low, int high, Edge edges[]) { double key = edges[low].cost; int begin = low; int end = high; while(low < high){ while(edges[low].cost <= key && low < high){ low ++; } while(edges[high].cost > key && low < high){ high--; } if(low < high){ Edge temp = edges[low]; edges[low] =edges[high]; edges[high] = temp; } } if(edges[low].cost < key){ Edge temp = edges[low]; edges[low] =edges[begin]; edges[begin] = temp; } if(begin < low - 1){ quickSortEdgeByCost(begin, low-1, edges); } if(end > low){ quickSortEdgeByCost(low, end, edges); } } } /************************************************************** Problem: 1144 User: yihukurama Language: Java Result: Accepted Time:270 ms Memory:24836 kb****************************************************************/