题目1144:Freckles(2009年北京大学计算机研究生机试真题)

来源:互联网 发布:邮箱smtp服务器端口 编辑:程序博客网 时间:2024/05/09 01:14
题目1144:Freckles

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:882

解决:452

题目描述:

    In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his Ripley's engagement falls through. 
    Consider Dick's back to be a plane with freckles at various (x,y) locations. Your job is to tell Richie how to connect the dots so as to minimize the amount of ink used. Richie connects the dots by drawing straight lines between pairs, possibly lifting the pen between lines. When Richie is done there must be a sequence of connected lines from any freckle to any other freckle. 

输入:

    The first line contains 0 < n <= 100, the number of freckles on Dick's back. For each freckle, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the freckle.

输出:

    Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the freckles.

样例输入:
31.0 1.02.0 2.02.0 4.0
样例输出:
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****************************************************************/


0 0
原创粉丝点击