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

代码随想录打卡|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]);  // 输出最短距离}}
}

相关文章:

  • 【前端】Twemoji(Twitter Emoji)
  • LeetCode 215:数组中的第K个最大元素 - 两种高效解法详解
  • 力扣-最长回文子串
  • HTB-Planning
  • PnP(Perspective-n-Point)算法 | 用于求解已知n个3D点及其对应2D投影点的相机位姿
  • Python基础教程:控制流与函数入门 - 第4-6天
  • 【网络入侵检测】基于Suricata源码分析FlowWorker实现
  • 智能仓储落地:机器人如何通过自动化减少仓库操作失误?
  • DeepSeek - 尝试一下GitHub Models中的DeepSeek
  • EasyRTC音视频实时通话助力微信小程序:打造低延迟、高可靠的VoIP端到端呼叫解决方案
  • 【ConvLSTM第二期】模拟视频帧的时序建模(Python代码实现)
  • Text-to-SQL评估体系:从Spider 1.0数据集到2.0框架的跨越与革新
  • HOW - 简历和求职面试宝典(八)
  • 【春秋云镜】CVE-2022-26965 靶场writeup
  • 江西某石灰石矿边坡自动化监测
  • 【Hive 运维实战】一键管理 Hive 服务:Metastore 与 HiveServer2 控制脚本开发与实践
  • LeetCode Hot100(多维动态规划)
  • 基于vue框架的独居老人上门护理小程序的设计r322q(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
  • 前端面试核心考点全解析
  • 华为OD机试真题——告警抑制(2025A卷:100分)Java/python/JavaScript/C/C++/GO最佳实现
  • 住房和城乡建设部网站规范答疑/宝鸡seo外包公司
  • 厦门网站建设费用/百度竞价关键词查询
  • 做企业网站哪家强/百度网盘搜索
  • html5自适应手机网站模板/小果seo实战培训课程
  • 电影网站模板html/西安百度公司开户
  • 做百度ssp的网站开发人/seo网站内容优化