dijkstra关于线程中的多个图异常;“主要”;java.lang.IllegalArgumentException:比较方法违反了其一般约定

dijkstra关于线程中的多个图异常;“主要”;java.lang.IllegalArgumentException:比较方法违反了其一般约定,java,graph,Java,Graph,我正在努力实现dijkstra 在hackerrank上,它为每个测试用例中的第一个图提供正确的ans 但如果testcase中有超过1个图形,则异常为“import java.io.* import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static class Node implements Comparabl

我正在努力实现dijkstra 在hackerrank上,它为每个测试用例中的第一个图提供正确的ans 但如果testcase中有超过1个图形,则异常为“import java.io.*

import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
public static class Node implements Comparable<Node>{
    int dst=Integer.MAX_VALUE;
    boolean visit=false;
    int index;
    Node(){};
    Node(int index){
        this.index=index;
    };

    public int compareTo(Node n1){
        try{
        return dst<n1.dst?-1:1;
       }catch(Exception e){return 1;}
    }
}
    public static class Edge{
        int index;
        int dst=0;
        Edge(){}
        Edge(int inedx){
            this.index=index;
        }


    }
       public static Node[] dij(ArrayList<Node> nod,ArrayList<LinkedList<Edge>> aee,Node[] nrr){

        while(!nod.isEmpty()){
        Collections.sort(nod);
        Node ne=nod.remove(0);
        LinkedList<Edge> queue=aee.get(ne.index);
        while(!queue.isEmpty()){
            Edge temp=queue.remove(0);
            if((nrr[temp.index].dst>(nrr[ne.index].dst+temp.dst)&&(nrr[ne.index].dst!=Integer.MAX_VALUE))){
                nrr[temp.index].dst=nrr[ne.index].dst+temp.dst;
            }
        }

        }
        return nrr;
    }
    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
   Scanner in=new Scanner(System.in);
        int t=in.nextInt();
        while(t-->0){
            int n=in.nextInt();
            Node nrr[]=new Node[n];
            ArrayList<Node> nod=new ArrayList<>();
            for(int i=0;i<n;i++){
                Node ne=new Node(i);
                nrr[i]=ne;
                nod.add(ne);
            }
            int m=in.nextInt();
            ArrayList<LinkedList<Edge>> aee=new ArrayList<LinkedList<Edge>>(n);
            for(int i=0;i<n;i++)
                aee.add(new LinkedList<Edge>());
            for(int i=0;i<m;i++){
                int start=in.nextInt()-1;
                int end=in.nextInt()-1;
                int dst=in.nextInt();
                Edge e1=new Edge(end);
                e1.dst=dst;
                e1.index=end;
                //System.out.println(start+" "+end+" dd "+dst+" "+e1.index);
                aee.get(start).add(e1);
                Edge e2=new Edge(start);
                e2.index=start;
                e2.dst=dst;
               // System.out.println("dd");
                aee.get(end).add(e2);
                nrr[start].visit=true;
                nrr[end].visit=true;
            }
            int k=in.nextInt()-1;
            nrr[k].dst=0;
            Node[] nn=dij(nod,aee,nrr);
            int temp=Integer.MAX_VALUE+0;
      //  System.out.println(temp+" tem");
        for(int i=0;i<n;i++){
            if(i!=k)
                System.out.print(nn[i].dst+" ");
        }
            System.out.println();
        }


    }
}

我无法改进这一点。

与您的
相比,您的
违反了合同。如果两个节点具有相同的
dst
,则得到
n1。compareTo(n2)==1
n2。compareTo(n1)==1

正确的版本应该是这样的

 public int compareTo(Node n1){
    return dst<n1.dst?-1:(dst>n1.dst?1:0);
 }
public int compareTo(节点n1){
返回dstn1.dst?1:0);
}

PS:我不知道你为什么会在那里发现异常
n1
不能为
null

您是否阅读了
Compariable.comparieto上的Javadoc?特别是关于输入相等时如何返回零的部分?我返回1和-1是的,我注意到你返回了1和-1。这是错误的(至少是部分错误)。我返回1和-1,据我所知,当返回-1时,它会将值与all进行比较,当它返回-1时进行交换,而不是当它是1时(升序或降序排序)。所以当它们相等时,返回1,交换不会发生,对吗?阅读可比较的Javadoc并遵循它。当它们相等时,必须返回零。当第一个小于第二个时,必须返回负数。当第一个大于第二个时,必须返回正数。如果你不遵守这些规则,你会遇到这样的异常。
 public int compareTo(Node n1){
    return dst<n1.dst?-1:(dst>n1.dst?1:0);
 }