九度OJ_Freckles_1144

来源:互联网 发布:易学软件下载 编辑:程序博客网 时间:2024/06/06 00:29

<span style="font-family: 'Microsoft Yahei'; background-color: rgb(255, 255, 255);">题目1144:Freckles</span>

题目描述:

    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
AC代码:

<span style="font-family:Microsoft YaHei;font-size:14px;">import java.text.DecimalFormat;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner cin = new Scanner(System.in);DecimalFormat df = new DecimalFormat("######0.00");Comparator<road> sortByWeight = new Comparator<Main.road>() {@Overridepublic int compare(road r1, road r2) {if(r1.weight>=r2.weight){return 1;}else{return -1;}}};while(cin.hasNext()){int num = cin.nextInt();ArrayList<point> points = new ArrayList<point>();ArrayList<road> roads = new ArrayList<road>();for(int i = 0;i<num;i++){point point = new point();point.x = cin.nextDouble();point.y = cin.nextDouble();point.index = i+1;points.add(point);}buildRoad(points,roads);Collections.sort(roads,sortByWeight);int tree[] = new int[roads.size()+1];for(int i = 0;i<=roads.size();i++){tree[i] = -1;}double result = 0;for(int i = 0;i<roads.size();i++){int a = findRoot(roads.get(i).a,tree);int b = findRoot(roads.get(i).b, tree);if(a!=b){tree[a] = b;result+=roads.get(i).weight;}}System.out.println(df.format(result));}}private static int findRoot(int x, int[] tree) {if(tree[x] == -1){return x;}else{int temp = findRoot(tree[x], tree);tree[x] = temp;return temp;}}private static void buildRoad(ArrayList<point> points, ArrayList<road> roads) {for(int i = 0;i<points.size();i++){for(int n = i+1;n<points.size();n++){road road = new road();road.a = points.get(i).index;road.b = points.get(n).index;road.weight = Math.sqrt(Math.abs(</span>
<span style="font-family:Microsoft YaHei;font-size:14px;">                                 (points.get(i).x-points.get(n).x)*(points.get(i).x-points.get(n).x)+(points.get(i).y-points.get(n).y)*(points.get(i).y-points.get(n).y)));roads.add(road);}}}public static class point{public int index;public double x;public double y;}public static class road{public int a;public int b;public double weight;}}</span><span style="font-family:Microsoft Yahei;font-size: 18px;"></span>

总结:

1.java保留两位小数方法:

  DecimalFormat df = new DecimalFormat("######0.00");  System.out.println(df.format(result));
2.java幂函数Math.pow(a,2)表示a的平方

3.java开方函数Math.sqrt(double x),绝度值函数Math.abs()


0 0
原创粉丝点击