Dijkstra算法堆优化java版本
本文最后更新于 1556 天前,其中的信息可能已经有所发展或是发生改变。
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);
    }
}
作者:Yuyy
博客:https://yuyy.info
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇