Java中的最小生成树

时间:2014-06-21 23:36:58

标签: java algorithm minimum-spanning-tree

这是一个ACM-ICPC练习题:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=975。并且基本思想是找到最小生成树的最短权重。我试图用Kruskal算法解决它。我自己测试了它似乎工作正常。但是,当我将其提交给uva在线评委时,结果是"错误答案"。任何人都可以看到哪里不正确吗?谢谢!

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
//import java.util.Comparator;
import java.util.List;
//import java.math.*;

public class Main  {

    public static void main(String[] args) throws Exception, IOException {

        BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));  
        int numOfCases = Integer.parseInt(cin.readLine());

        while (numOfCases > 0) {
            cin.readLine();
            int numOfPoints = Integer.parseInt(cin.readLine());
            List<Points> listOfPoints = new ArrayList<Points>();
            List<Edge> listOfEdges = new ArrayList<Edge>();

            double inkUsed = 0;

            for (int i = 0; i < numOfPoints; i++) {
                String[] tokens = cin.readLine().split(" ");
                Points point = new Points(Double.parseDouble(tokens[0]),Double.parseDouble(tokens[1]));
                listOfPoints.add(point);
            }

            for (int i = 0; i < listOfPoints.size(); i++) {
                for (int j = i+1; j < listOfPoints.size(); j++) {
                    Edge edge = new Edge(listOfPoints.get(i), listOfPoints.get(j));
                    listOfEdges.add(edge);
                }
            }

            Collections.sort(listOfEdges);

            for (Edge e: listOfEdges) {

                Points pointA = null;
                Points pointB = null;

                // Find pointA and pointB in the list "listOfPoints"
                for (int i = 0; i < listOfPoints.size(); i++) {
                    if (listOfPoints.get(i).x == e.A.x && listOfPoints.get(i).y == e.A.y) {
                        pointA = listOfPoints.get(i);
                    }
                    if (listOfPoints.get(i).x == e.B.x && listOfPoints.get(i).y == e.B.y) {
                        pointB = listOfPoints.get(i);
                    }
                }

                Points rootOfA = findSet(pointA);
                Points rootOfB = findSet(pointB);

                if (rootOfA.x != rootOfB.x || rootOfA.y != rootOfB.y){  //add this edge
                    //System.out.println( "[" + "(" + pointA.x +"," + pointA.y+"); (" + pointB.x +"," + pointB.y+")"+ "]");
                    inkUsed += e.weight;
                    rootOfA.parent = rootOfB;
                }
            }

            DecimalFormat df = new DecimalFormat("#.00"); 
            System.out.println(df.format(inkUsed));
            numOfCases--;
        }
    }

    public static Points findSet(Points A) {
        Points rootOfA = A;

        while (rootOfA.parent != null) {
            rootOfA = rootOfA.parent;
        }
        return rootOfA;
    }
}

class Points {  

    public double x;
    public double y;
    public Points parent = null;

    Points (double x, double y) {
        this.x = x;
        this.y = y;
    }
}

class Edge implements Comparable<Edge> {

    Points A;
    Points B;
    double weight;

    Edge (Points var1, Points var2) {
        A = var1;
        B = var2;
        weight = Math.sqrt(Math.pow((A.x-B.x), 2) + Math.pow((A.y-B.y), 2));
    }

    public String toString() {
        return Double.toString(weight);
    }

    public int compareTo(Edge anotherEdge) {
        if (this.weight < anotherEdge.weight) {
            return -1;
        } else if (this.weight == anotherEdge.weight) {
            return 0;
        } else {
            return 1;
        }
    }
}

0 个答案:

没有答案