英文:
Exception in thread "main" java.lang.ClassCastException: javafx.util.Pair cannot be cast to java.lang.Comparable
问题
以下是您提供的代码的翻译部分:
还有,这个问题已经被问了很多次,根据我的解决方案,我无法解决这个问题。我正在编写用于加权图的Dijkstra算法。我使用了ArrayList<ArrayList<Pair<Integer, Integer>>>来存储我的权重和边缘。在实现Dijkstra函数时,我使用了MinHeap,它是Java中Pair<Integer, Integer>类型的PriorityQueue。在将对添加到堆时,我遇到了这个错误。
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import javafx.util.Pair;
public class Solution {
public static void dijkstra(ArrayList<ArrayList<Pair<Integer, Integer>>> graph, int V, int E, int S) {
int dist[] = new int[V + 1];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[S] = 0;
PriorityQueue<Pair<Integer, Integer>> heap = new PriorityQueue<>();
heap.add(new Pair<Integer, Integer>(0, S));
while (heap.size() != 0) {
Pair<Integer, Integer> curr = heap.poll();
int w = curr.getKey(), x = curr.getValue();
System.out.println(x);
if (w <= dist[x]) {
for (Pair<Integer, Integer> temp : graph.get(x)) {
int x2 = temp.getKey(), w2 = temp.getValue();
if (w + w2 < dist[x2]) {
dist[x2] = w + w2;
// 在下面的行中遇到错误
heap.add(new Pair<Integer, Integer>(w + w2, x2));
}
}
}
}
for (int i = 1; i <= V; i++) {
if (i == S)
continue;
if (dist[i] == Integer.MAX_VALUE)
System.out.print("-1 ");
else
System.out.print(dist[i] + " ");
}
}
// 向图中添加边
public static void addEdge(ArrayList<ArrayList<Pair<Integer, Integer>>> graph, int u, int v, int w) {
graph.get(u).add(new Pair<Integer, Integer>(v, w));
graph.get(v).add(new Pair<Integer, Integer>(u, w));
}
// 主函数
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-- != 0) {
int n = in.nextInt();
int m = in.nextInt();
ArrayList<ArrayList<Pair<Integer, Integer>>> graph = new ArrayList<>();
for (int i = 0; i <= n; i++) {
graph.add(new ArrayList<Pair<Integer, Integer>>());
}
for (int i = 0; i < m; i++) {
addEdge(graph, in.nextInt(), in.nextInt(), in.nextInt());
}
int s = in.nextInt();
dijkstra(graph, n, m, s);
System.out.println();
}
}
}
希望这有助于您理解代码的翻译部分。如果您有任何其他问题,请随时提问。
英文:
Also, this question had asked many times, I'm unable to figure that out according to my solution. I'm writing the Dijkstra algorithm for the weighted graph. I used ArrayList<ArrayList<Pair<Integer, Integer>>> for storing my weights and edges. While the implementation of the Dijkstra function, I used MinHeap which is PriorityQueue in java of type Pair<Integer, Integer>. At the time of adding pairs to the heap, I getting this error.
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import javafx.util.Pair;
public class Solution{
public static void dijkstra(ArrayList<ArrayList<Pair<Integer, Integer>>> graph, int V, int E, int S) {
int dist[] = new int[V+1];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[S] = 0;
PriorityQueue< Pair<Integer, Integer> > heap = new PriorityQueue<>();
heap.add(new Pair<Integer, Integer>(0, S) );
while(heap.size() != 0) {
Pair<Integer, Integer> curr = heap.poll();
int w = curr.getKey(), x = curr.getValue();
System.out.println(x);
if(w <= dist[x]) {
for(Pair<Integer, Integer> temp : graph.get(x)){
int x2 = temp.getKey(), w2 = temp.getValue();
if(w+w2 < dist[x2]){
dist[x2] = w + w2;
//Facing the error in below line
heap.add(new Pair<Integer, Integer>(w+w2, x2));
}
}
}
}
for(int i = 1; i <= V; i++) {
if(i == S) continue;
if(dist[i] == Integer.MAX_VALUE)
System.out.print("-1 ");
else
System.out.print(dist[i]+" ");
}
}
//Adding edge to a graph
public static void addEdge( ArrayList<ArrayList<Pair<Integer, Integer>>> graph, int u, int v, int w){
graph.get(u).add(new Pair<Integer, Integer>(v, w));
graph.get(v).add(new Pair<Integer, Integer>(u, w));
}
//Main Function
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t-- != 0) {
int n = in.nextInt();
int m = in.nextInt();
ArrayList<ArrayList<Pair<Integer, Integer>>> graph = new ArrayList<>();
for(int i = 0; i <= n; i++) {
graph.add(new ArrayList<Pair<Integer, Integer>>());
}
for(int i = 0; i < m; i++) {
addEdge(graph, in.nextInt(), in.nextInt(), in.nextInt());
}
int s = in.nextInt();
dijkstra(graph, n, m, s);
System.out.println();
}
}
}
I'm also adding the duplicates into the heaps as it doesn't affect my algorithm.
答案1
得分: 2
因为PriorityQueue维护其存储对象的排序顺序,为此需要一些对象之间的比较逻辑。
在您的情况下,对象是Pair,因此错误要求您要么使您的Pair类实现Comparable接口,要么为优先队列提供一个比较器来对Pair对象进行排序。
请查看这个链接:https://www.callicoder.com/java-priority-queue/
基本上,您需要提供自定义比较器。
英文:
Because PriorityQueue maintains sorted orders of the objects it stores, And for that it need some comparison logic between objects.
In your case it is Pair, so the error is asking you to either make your Pair class implement Comparable interface or provide priority queue a comparator to do the sorting of pair object.
Have a look at this https://www.callicoder.com/java-priority-queue/
Basically you have to provide custom comparator.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论