使用 Kruskal 算法计算最小生成树时的错误答案

Wrong answer while calculating Minimum Spanning Tree Using Kruskal Algorithm

我正在尝试使用 kruskal 算法求解 this MST question on spoj。我的程序似乎适用于所有测试用例,但 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;
        }
    }
}

您的 union-find 实现不正确。考虑这个例子

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

这显然是不正确的。

您可以更改 checkCycle 如下:

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;
}

我强烈建议你阅读关于 Disjoint-set 的维基百科文章并实现路径压缩版本,这会提高复杂度。