惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
H
Help Net Security
B
Blog RSS Feed
G
Google Developers Blog
博客园 - 司徒正美
MongoDB | Blog
MongoDB | Blog
量子位
博客园 - 三生石上(FineUI控件)
The Cloudflare Blog
P
Proofpoint News Feed
小众软件
小众软件
人人都是产品经理
人人都是产品经理
云风的 BLOG
云风的 BLOG
V
V2EX
月光博客
月光博客
C
Check Point Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
A
Arctic Wolf
Help Net Security
Help Net Security
Schneier on Security
Schneier on Security
D
DataBreaches.Net
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Palo Alto Networks Blog
T
Tenable Blog
L
LangChain Blog
Attack and Defense Labs
Attack and Defense Labs
Google DeepMind News
Google DeepMind News
N
News and Events Feed by Topic
Forbes - Security
Forbes - Security
F
Fortinet All Blogs
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
大猫的无限游戏
大猫的无限游戏
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Y
Y Combinator Blog
WordPress大学
WordPress大学
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Engineering at Meta
Engineering at Meta
NISL@THU
NISL@THU
GbyAI
GbyAI
博客园 - Franky
S
Secure Thoughts
有赞技术团队
有赞技术团队
PCI Perspectives
PCI Perspectives
U
Unit 42

博客园 - 西北逍遥

实验启动指令 yolov8-pose监测人体关节并保存关节坐标 osg3.6绘制半球体 kinova jaco2 机械臂控制器故障灯闪烁(双绿灯)问题解决方法 IFC标准在学术界的研究与发展历程:从理论探索到产业实践的全面梳理IFC标准在学术界的研究与发展历程:从理论探索到产业实践的全面梳理 BIM的“普通话”:解密IFC标准如何重塑建筑行业 IfcCrewResource Qt重置 Brush pyqt 操作mysql数据库 泵仿真 Qt折线的显示与隐藏 Qt绘制折线 c++ Qt绘制传热云图 livox mid-70采集点云数据 随机配色 学习:LED灯闪烁 win10安装neo4j-community-3.5.7-windows win10安装MongoDB 3.0.15 Community python把图片合并成gif图 ubuntu20.04测试cuda start.bat
Djstra求解最短路径
西北逍遥 · 2025-09-01 · via 博客园 - 西北逍遥
import java.util.*;

class ShortestPathToWork {
    private static class Node implements Comparable<Node> {
        final String name;
        final int distance;

        public Node(String name, int distance) {
            this.name = name;
            this.distance = distance;
        }

        @Override
        public int compareTo(Node other) {
            return Integer.compare(this.distance, other.distance);
        }
    }

    private final Map<String, List<Node>> adjList = new HashMap<>();
    private final Map<String, Integer> distances = new HashMap<>();
    private final Map<String, String> previousNodes = new HashMap<>(); // 记录前驱节点

    public void addEdge(String from, String to, int distance) {
        adjList.computeIfAbsent(from, k -> new ArrayList<>()).add(new Node(to, distance));
    }

    public void dijkstra(String start) {
        PriorityQueue<Node> pq = new PriorityQueue<>();
        pq.add(new Node(start, 0));
        distances.put(start, 0);
        previousNodes.put(start, null); // 起点的前驱为 null

        while (!pq.isEmpty()) {
            Node current = pq.poll();
            String currentNode = current.name;

            if (adjList.containsKey(currentNode)) {
                for (Node neighbor : adjList.get(currentNode)) {
                    int newDist = distances.get(currentNode) + neighbor.distance;
                    if (!distances.containsKey(neighbor.name) || newDist < distances.get(neighbor.name)) {
                        distances.put(neighbor.name, newDist);
                        previousNodes.put(neighbor.name, currentNode); // 更新前驱节点
                        pq.add(new Node(neighbor.name, newDist));
                    }
                }
            }
        }
    }

    // 新增:计算从 start 到 end 的最短路径,并返回路径(节点列表)
    public List<String> getShortestPath(String start, String end) {
        dijkstra(start); // 重新计算最短路径(确保数据是最新的)
        List<String> path = new ArrayList<>();
        String current = end;

        // 回溯路径
        while (current != null) {
            path.add(current);
            current = previousNodes.get(current); // 获取前驱节点
        }

        Collections.reverse(path); // 反转路径,从起点到终点
        return path;
    }

    public void printShortestPath(String start, String end) {
        List<String> path = getShortestPath(start, end);
        System.out.println("Shortest path from " + start + " to " + end + ": " + String.join(" -> ", path));
        System.out.println("Total distance: " + distances.get(end)); // distances 已经在 dijkstra 中计算过
    }

    public static void main(String[] args) {
        ShortestPathToWork graph = new ShortestPathToWork();

        // 根据新图添加边和权重
        graph.addEdge("A", "E", 10);
        graph.addEdge("A", "H", 5);
        graph.addEdge("E", "C", 15);
        graph.addEdge("E", "O", 14);
        graph.addEdge("O", "E", 14);
        graph.addEdge("H", "O", 18);
        graph.addEdge("O", "H", 18);
        graph.addEdge("H", "D", 4);
        graph.addEdge("O", "G", 8);
        graph.addEdge("G", "O", 8);
        graph.addEdge("O", "F", 6);
        graph.addEdge("F", "O", 6);
        graph.addEdge("C", "G", 5);
        graph.addEdge("D", "F", 3);
        graph.addEdge("F", "B", 20);
        graph.addEdge("G", "B", 5);

        // 计算并打印从 A 到 B 的最短路径
        graph.printShortestPath("A", "B");
    }
}
Shortest path from A to B: A -> H -> D -> F -> O -> G -> B
Total distance: 31