Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Kruskal算法计算最小生成树时回答错误_Java_Algorithm_Minimum Spanning Tree_Kruskals Algorithm_Union Find - Fatal编程技术网

Java 使用Kruskal算法计算最小生成树时回答错误

Java 使用Kruskal算法计算最小生成树时回答错误,java,algorithm,minimum-spanning-tree,kruskals-algorithm,union-find,Java,Algorithm,Minimum Spanning Tree,Kruskals Algorithm,Union Find,我正在尝试使用kruskal算法求解。我的程序似乎在所有测试用例上都能工作,但spoj在这段代码上反复给出WA 我无法在此代码上找到任何失败的测试用例。有人能指出我做错了什么吗 import java.io.PrintWriter; import java.util.Arrays; public class CSTREET { static final int MAX = 1002; static Node edgeList[]; static int parent

我正在尝试使用kruskal算法求解。我的程序似乎在所有测试用例上都能工作,但spoj在这段代码上反复给出WA

我无法在此代码上找到任何失败的测试用例。有人能指出我做错了什么吗

import java.io.PrintWriter;
import java.util.Arrays;


public class CSTREET {

    static final int MAX = 1002;
    static Node edgeList[];
    static int parent[] = new int[MAX];


    public static void main(String[] args) throws Exception {
        Reader in = new Reader();
        PrintWriter out = new PrintWriter(System.out, true);
        int t = in.nextInt();
        while (t-- != 0) {

            int price = in.nextInt();
            int vertices = in.nextInt();
            int edge = in.nextInt();
            int idx = 0;
            edgeList = new Node[edge];
            for (int i = 1; i <= vertices; i++) {
                parent[i] = i;
            }

            while (idx < edge) {

                int src = in.nextInt();
                int dest = in.nextInt();
                int cost = in.nextInt();
                Node node = new Node(src, dest, cost);

                edgeList[idx] = node;
                idx++;
            }

            Arrays.sort(edgeList);
            int edgeCount = 0;


            long totalCost = 0;
            idx = 0;

            while (edgeCount < vertices-1 ) {
                Node curEdge = edgeList[idx];
                if (!checkCycle(curEdge.src, curEdge.dest)) {

                    edgeCount++;
                    totalCost += curEdge.cost;

                }
                idx++;

            }
            out.println(totalCost * price);
        }
    }


    static boolean checkCycle(int src, int dest) {

        if (findParent(src) == findParent(dest)) {
            return true;
        }

        while (parent[dest] != parent[src]) {
            parent[dest] = src;
            src = parent[src];
        }

        return false;

    }

    static int findParent(int i) {

        while (parent[i] != i) {
            i = parent[i];
        }

        return i;
    }


    static class Node implements Comparable<Node> {

        int src;
        int dest;
        int cost;

        public Node(int src, int dest, int cost) {
            this.src = src;
            this.dest = dest;
            this.cost = cost;
        }

        @Override
        public int compareTo(Node o) {
            return this.cost - o.cost;
        }
    }
}
导入java.io.PrintWriter;
导入java.util.array;
公共类CSTREET{
静态最终int MAX=1002;
静态节点边缘列表[];
静态整数父项[]=新整数[MAX];
公共静态void main(字符串[]args)引发异常{
Reader in=新读取器();
PrintWriter out=新的PrintWriter(System.out,true);
int t=in.nextInt();
而(t-!=0){
int price=in.nextInt();
int顶点=in.nextInt();
int edge=in.nextInt();
int-idx=0;
edgeList=新节点[边];

对于(int i=1;i),您的UNION发现的实现是不正确的。考虑这个例子

x -> y ( y is parent of x )

A -> B -> C
D -> E
当您调用
checkCycle(A,D)
时,应该发生的是所有5个节点都应该转到一个集合,例如:

A -> B -> C
D -> E -> C
但在您的代码中发生的是:

A -> B -> C
D -> C
E
这显然是不正确的

您可以按如下方式更改
检查周期

static boolean checkCycle(int src, int dest) {

    int srcRoot = findParent(src);
    int destRoot = findParent(dest);
    if (srcRoot == destRoot ) {
        return true;
    }
    parent[destRoot] = srcRoot;
    return false;
}

<>我强烈建议您阅读有关维基百科的文章并实现路径压缩版本,这就提高了复杂性。

您的UNION发现的实现是不正确的。
x -> y ( y is parent of x )

A -> B -> C
D -> E
当您调用
checkCycle(A,D)
时,应该发生的是所有5个节点都应该转到一个集合,例如:

A -> B -> C
D -> E -> C
但在您的代码中发生的是:

A -> B -> C
D -> C
E
这显然是不正确的

您可以按如下方式更改
检查周期

static boolean checkCycle(int src, int dest) {

    int srcRoot = findParent(src);
    int destRoot = findParent(dest);
    if (srcRoot == destRoot ) {
        return true;
    }
    parent[destRoot] = srcRoot;
    return false;
}

我强烈建议您阅读wikipedia文章并实现路径压缩版本,这将提高复杂性。

请输入您实际提交的代码。我在提交此代码时遇到编译错误。请输入您实际提交的代码。我在提交此代码时遇到编译错误。谢谢。请输入Dect是我的union find实现中的bug。谢谢。它确实是我的union find实现中的bug。