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

【代码随想录Day58】图论Part09

dijkstra(堆优化版)精讲

题目链接/文章讲解:代码随想录

import java.util.*;class Edge {int to;  // 邻接顶点int val; // 边的权重Edge(int to, int val) {this.to = to;this.val = val;}
}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>> graph = new ArrayList<>(n + 1);for (int i = 0; i <= n; i++) {graph.add(new ArrayList<>());}// 读取边的信息for (int i = 0; i < m; i++) {int p1 = scanner.nextInt();int p2 = scanner.nextInt();int val = scanner.nextInt();graph.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);minDist[start] = 0;  // 起始点到自身的距离为0// 记录顶点是否被访问过boolean[] visited = new boolean[n + 1];// 优先队列,用于选择当前最短路径的节点PriorityQueue<Pair<Integer, Integer>> pq = new PriorityQueue<>(Comparator.comparingInt(pair -> pair.second));// 初始化队列,添加源点pq.add(new Pair<>(start, 0));while (!pq.isEmpty()) {// 取出当前距离源点最近的节点Pair<Integer, Integer> cur = pq.poll();int currentNode = cur.first;// 如果该节点已被访问,跳过if (visited[currentNode]) continue;// 标记该节点为已访问visited[currentNode] = true;// 更新与当前节点相连的邻接节点的路径for (Edge edge : graph.get(currentNode)) {// 如果该邻接节点未被访问且新路径更短,则更新最短路径if (!visited[edge.to] && minDist[currentNode] + edge.val < minDist[edge.to]) {minDist[edge.to] = minDist[currentNode] + edge.val;pq.add(new Pair<>(edge.to, minDist[edge.to])); // 将新路径加入优先队列}}}// 输出结果System.out.println(minDist[end] == Integer.MAX_VALUE ? -1 : 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 scanner = new Scanner(System.in);int numberOfNodes = scanner.nextInt(); // 节点数量int numberOfEdges = scanner.nextInt();  // 边的数量List<Edge> edges = new ArrayList<>();// 读取每一条边的信息for (int i = 0; i < numberOfEdges; i++) {int from = scanner.nextInt();int to = scanner.nextInt();int val = scanner.nextInt();edges.add(new Edge(from, to, val));}// 存储从起点到各节点的最小距离int[] minDistance = new int[numberOfNodes + 1];Arrays.fill(minDistance, Integer.MAX_VALUE); // 初始化最小距离为无穷大minDistance[1] = 0; // 起点到自身的距离为0// 执行 Bellman-Ford 算法,放松所有边 n-1 次for (int i = 1; i < numberOfNodes; i++) {for (Edge edge : edges) {// 如果起始节点的距离不为无穷大,且通过当前边可以找到更短的路径if (minDistance[edge.from] != Integer.MAX_VALUE) {int newDistance = minDistance[edge.from] + edge.val;if (newDistance < minDistance[edge.to]) {minDistance[edge.to] = newDistance; // 更新最小距离}}}}// 输出结果if (minDistance[numberOfNodes] == Integer.MAX_VALUE) {System.out.println("unconnected"); // 如果目标节点不可达} else {System.out.println(minDistance[numberOfNodes]); // 输出目标节点的最小距离}scanner.close(); // 关闭扫描器以释放资源}
}
http://www.lryc.cn/news/472572.html

相关文章:

  • _或者%关键字模糊匹配查出所有数据
  • 【Python】转换得到图片的rgb565格式数据
  • 隨筆 20241024 Kafka中的ISR列表:分区副本的族谱
  • 【python】爬虫
  • 大语言模型数据类型与环境安装(llama3模型)
  • JS:列表操作
  • ECharts 折线图 / 柱状图 ,通用配置标注示例
  • 统计数据集的TXT、XML及JSON标注文件中各类别/每个标签的数量
  • Facebook登录客户追踪:了解用户访问路径,优化客户体验
  • NUUO摄像头 debugging_center_utils 远程命令执行漏洞复现
  • Nginx 的讲解和案例示范
  • 微信小程序元素水平居中或垂直居中
  • ClickHouse 神助攻:纽约城市公共交通管理(MTA)数据应用挑战赛
  • ELK + Filebeat + Spring Boot:日志分析入门与实践(二)
  • 使用 Docker Compose 将数据版 LobeChat 服务端部署
  • python如何完成金融领域的数据分析,思路以及常见的做法是什么?
  • 密码管理工具实现
  • 构造函数和new操作符 - 2024最新版前端秋招面试短期突击面试题【100道】
  • 6.Linux按键驱动-阻塞与非阻塞
  • Mac打开环境变量配置文件,source ~/.zshrc无法打开问题解决
  • 计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-23
  • 【C#】搭建环境之CSharp+OpenCV
  • 100种算法【Python版】第25篇——Bidirectional Search算法
  • WebSocket与Socket
  • Python 3 维护有序列表 bisect
  • vue版本太低无法执行vue ui命令
  • 数据结构 之 二叉树的遍历------先根遍历(五)
  • Xss_less靶场攻略(1-18)
  • 【AI语音克隆整合包及教程】声临其境,让想象成为现实——第二代GPT-SoVITS引领语音克隆新时代!
  • echarts属性之dataZoom