当前位置: 首页 > ops >正文

代码随想录打卡|Day51 图论(dijkstra(堆优化版)精讲、Bellman_ford 算法精讲)

图论part09

dijkstra(堆优化版)精讲(不熟悉)

代码随想录链接
题目链接

在这里插入图片描述
在这里插入图片描述

import java.util.*;class Edge {int to;  // 邻接顶点int val; // 边的权重Edge(int to, int val) {this.to = to;this.val = val;}
}class MyComparison implements Comparator<Pair<Integer, Integer>> {@Overridepublic int compare(Pair<Integer, Integer> lhs, Pair<Integer, Integer> rhs) {return Integer.compare(lhs.second, rhs.second);}
}class Pair<U, V> {public final U first;public final V second;public Pair(U first, V second) {this.first = first;this.second = second;}
}public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();int m = scanner.nextInt();List<List<Edge>> grid = new ArrayList<>(n + 1);for (int i = 0; i <= n; i++) {grid.add(new ArrayList<>());}for (int i = 0; i < m; i++) {int p1 = scanner.nextInt();int p2 = scanner.nextInt();int val = scanner.nextInt();grid.get(p1).add(new Edge(p2, val));}int start = 1;  // 起点int end = n;    // 终点// 存储从源点到每个节点的最短距离int[] minDist = new int[n + 1];Arrays.fill(minDist, Integer.MAX_VALUE);// 记录顶点是否被访问过boolean[] visited = new boolean[n + 1];// 优先队列中存放 Pair<节点,源点到该节点的权值>PriorityQueue<Pair<Integer, Integer>> pq = new PriorityQueue<>(new MyComparison());// 初始化队列,源点到源点的距离为0,所以初始为0pq.add(new Pair<>(start, 0));minDist[start] = 0;  // 起始点到自身的距离为0while (!pq.isEmpty()) {// 1. 第一步,选源点到哪个节点近且该节点未被访问过(通过优先级队列来实现)// <节点, 源点到该节点的距离>Pair<Integer, Integer> cur = pq.poll();if (visited[cur.first]) continue;// 2. 第二步,该最近节点被标记访问过visited[cur.first] = true;// 3. 第三步,更新非访问节点到源点的距离(即更新minDist数组)for (Edge edge : grid.get(cur.first)) { // 遍历 cur指向的节点,cur指向的节点为 edge// cur指向的节点edge.to,这条边的权值为 edge.valif (!visited[edge.to] && minDist[cur.first] + edge.val < minDist[edge.to]) { // 更新minDistminDist[edge.to] = minDist[cur.first] + edge.val;pq.add(new Pair<>(edge.to, minDist[edge.to]));}}}if (minDist[end] == Integer.MAX_VALUE) {System.out.println(-1); // 不能到达终点} else {System.out.println(minDist[end]); // 到达终点最短路径}}
}

Bellman_ford 算法精讲(不熟悉)

代码随想录链接
题目链接

在这里插入图片描述
在这里插入图片描述

代码

import java.util.*;public class Main {// 定义边的数据结构static class Edge {int from;  // 边的起点int to;    // 边的终点int val;   // 边的权值(距离/成本)// 构造函数public Edge(int from, int to, int val) {this.from = from;this.to = to;this.val = val;}}public static void main(String[] args) {Scanner sc = new Scanner(System.in);// 1. 输入处理int n = sc.nextInt();  // 节点数(编号1~n)int m = sc.nextInt();  // 边数List<Edge> edges = new ArrayList<>();  // 存储所有边// 读取每条边的信息for (int i = 0; i < m; i++) {int from = sc.nextInt();int to = sc.nextInt();int val = sc.nextInt();edges.add(new Edge(from, to, val));  // 添加到边列表}// 2. 初始化距离数组int[] minDist = new int[n + 1];  // minDist[i]表示节点1到节点i的最短距离Arrays.fill(minDist, Integer.MAX_VALUE);  // 初始化为无穷大minDist[1] = 0;  // 起点到自身的距离为0// 3. Bellman-Ford 核心算法:松弛操作for (int i = 1; i < n; i++) {  // 最多进行n-1轮松弛boolean updated = false;  // 标记本轮是否更新for (Edge edge : edges) {// 如果起点可达,且通过当前边可以缩短距离if (minDist[edge.from] != Integer.MAX_VALUE && minDist[edge.from] + edge.val < minDist[edge.to]) {minDist[edge.to] = minDist[edge.from] + edge.val;  // 更新最短距离updated = true;  // 标记有更新}}if (!updated) break;  // 提前终止:如果本轮未更新,说明已收敛}// 4. 输出结果if (minDist[n] == Integer.MAX_VALUE) {System.out.println("unconnected");  // 终点不可达} else {System.out.println(minDist[n]);  // 输出最短距离}}
}
http://www.xdnf.cn/news/9671.html

相关文章:

  • 开发时如何通过Service暴露应用?ClusterIP、NodePort和LoadBalancer类型的使用场景分别是什么?
  • Python+VR:如何让虚拟世界更懂你?——用户行为分析的实践
  • 【Linux】(1)—进程概念-②Linux中的操作系统概念
  • 桂花网体育运动监测方案:开启幼儿园运动健康管理新篇章
  • 【Linux】shell脚本的变量与运算
  • Spring框架学习day2--Bean管理(IOC)
  • 【博客系统】博客系统第十一弹:部署博客系统项目到 Linux 系统
  • Elasticsearch集群管理的相关工具介绍
  • [Rust_1] 环境配置 | vs golang | 程序运行 | 包管理
  • 自定义异常小练习
  • Intellij IDEA 查找接口实现类的快捷键
  • CVPR2025重磅突破:AnomalyAny框架实现单样本生成逼真异常数据,破解视觉检测瓶颈!
  • 数据可视化(第4、5、6次课)
  • 【Java Web】速通JavaScript
  • k8s pod启动失败问题排查
  • NanoGPT的BenchMarking.py
  • 数据治理是什么意思?数据治理平台有哪些?
  • 如何使用.Net Reactor 批量加密 DLL
  • PostgreSQL 备份与恢复策略
  • docker网络相关内容详解
  • Java开发经验——阿里巴巴编码规范实践解析7
  • Axure设计案例——科技感立体柱状图
  • 动态规划-931.下降路径最小和-力扣(LeetCode)
  • 高光谱成像相机:基于高光谱成像技术的玉米种子纯度检测研究
  • 华为OD机试真题——阿里巴巴找黄金宝箱(II)(2025A卷:100分)Java/python/JavaScript/C/C++/GO最佳实现
  • 小程序 - 视图与逻辑
  • MySQL的基本架构
  • 社群分享:义乌|杭州电商|店群卖家,私域鱼塘运营的排单系统开源|私域鱼塘运营|返款软件开源
  • Typora-macOS 风格代码块
  • Kotlin Multiplatform与Flutter深度对比:跨平台开发方案的实战选择