【算法】最短路径算法——Dijkstra(迪杰斯特拉) Java+PriorityQueue版

最短路径算法——Dijkstra(迪杰斯特拉) Java+PriorityQueue版

本文非解释原理,学习Dijkstra原理可参考 : 最短路径问题—Dijkstra算法详解

最近在备战天梯赛,遇到最短路问题,于是整理了下模板,较好理解+简易,其中利用了PriorityQueue,优化找最值的过程,减少了代码量。

import java.util.*;

public class Dijkstra {
    static class Edge {
        int to, weight;

        public Edge(int to, int weight) {
            this.to = to;
            this.weight = weight;
        }
    }

    static int INF = Integer.MAX_VALUE;
    static int[] dist;
    static int[] prev;
    static boolean[] visited;
    static List<List<Edge>> graph;

    public static void dijkstra(int start) {
        PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return dist[o1] - dist[o2];
            }
        });
        pq.offer(start);
        dist[start] = 0;

        while (!pq.isEmpty()) {
            //取出距离start最近的点
            int u = pq.poll();
            visited[u] = true;
            //遍历u的每条边u-v,如果能通过u使得start到v的距离变得更短,更新start到v的距离dist[v]
            for (Edge e : graph.get(u)) {
                int v = e.to;
                int weight = e.weight;
                if (!visited[v] && dist[v] > dist[u] + weight) {
                    dist[v] = dist[u] + weight;
                    //记录前驱节点
                    prev[v] = u;
                    //将v加入优先队列,下次参与比较距离start最近的点
                    pq.offer(v);
                }
            }
        }
    }

	public static List<Integer> getShortestPath(int start, int end) {
        List<Integer> path = new ArrayList<>();
        for (int v = end; v != start; v = prev[v]) {
            path.add(v);
        }
        path.add(start);
        Collections.reverse(path);
        return path;
    }

    public static void main(String[] args) {
        int n = 5;
        int start = 0;
        int end = 3;
        dist = new int[n];
        prev = new int[n];
        visited = new boolean[n];
        Arrays.fill(dist, INF);
        Arrays.fill(prev, -1);
        graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
		//get某点,add某边(终点,权重)
        graph.get(0).add(new Edge(1, 10));
        graph.get(0).add(new Edge(4, 3));
        graph.get(1).add(new Edge(2, 2));
        graph.get(1).add(new Edge(4, 4));
        graph.get(2).add(new Edge(3, 9));
        graph.get(3).add(new Edge(2, 7));
        graph.get(3).add(new Edge(0, 6));
        graph.get(4).add(new Edge(1, 1));
        graph.get(4).add(new Edge(2, 8));
        graph.get(4).add(new Edge(3, 2));

        dijkstra(start);

        System.out.println("Distance from " + start + " to " + end + " is " + dist[end]);
        List<Integer> shortestPath = getShortestPath(start, end);
        System.out.println("Shortest path: " + shortestPath.toString());
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

炬ju

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值