本文最后更新于 916 天前,其中的信息可能已经有所发展或是发生改变。
代码语言:javascript复制public class DijkstraHeap {
public static final int INF=100000;
public int[] dist=new int[10000];
public int[] s=new int[10000];
public int[] path=new int[10000];
private int start;
public void dijkstra(int arr[][],int start,int end){
this.start=start;
for (int i=0;i<10000;i ){
dist[i]=INF;
}
Queue<Node> nodeQueue=new PriorityQueue<>();
nodeQueue.add(new Node(start,0));
dist[start]=0;
path[start]=-1;
while (!nodeQueue.isEmpty()) {
Node node = nodeQueue.poll();
int u = node.to;
if (s[u] == 1)
continue;
s[u] = 1;
for(int i=0;i<10000;i ){
if(s[i]==0&&dist[i]>arr[u][i] node.cost){
dist[i]=arr[u][i] node.cost;
nodeQueue.add(new Node(i,dist[i]));
path[i]=u;
}
}
}
//输出start到end的路径和距离
String tempDist="距离为:";
tempDist =dist[end];
System.out.println(tempDist);
int shortest[]=new int[10000];
int k=0;
shortest[k]=end;
while(path[shortest[k]]!=start){
k ;
shortest[k]=path[shortest[k-1]];
}
k ;
shortest[k]=start;
String temp="路径为:";
for(;k>=0;k--){
temp =Integer.toString(shortest[k]);
if(k!=0){
temp ="->";
}
}
System.out.println(temp);
}
}
Post Views: 570