前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Dijkstra算法堆优化java版本

Dijkstra算法堆优化java版本

作者头像
Yuyy
发布2022-06-28 18:45:38
3960
发布2022-06-28 18:45:38
举报
文章被收录于专栏:yuyy.info技术专栏

本文最后更新于 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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-4-21 2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档