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

算法打卡:第十一章 图论part10

今日收获:Bellman_ford 队列优化算法(又名SPFA),bellman_ford之判断负权回路和单源有限最短路

1. Bellman_ford 队列优化算法(又名SPFA)

题目链接:94. 城市间货物运输 I (kamacoder.com)

思路:上篇文章中Bellman_ford算法是在每一次循环中对所有边进行松弛操作。实际上只需要对上一次更新的节点相连边进行松弛操作,可以使用队列来优化。

方法:

import java.util.*;public class Main{public static void main(String[] args){Scanner sc=new Scanner(System.in);// 接收数据int n=sc.nextInt();int m=sc.nextInt();List<List<Edge>> grid=new ArrayList<>(n+1);for (int i=0;i<n+1;i++){grid.add(new ArrayList<>());}for (int i=0;i<m;i++){int s=sc.nextInt();int t=sc.nextInt();int v=sc.nextInt();grid.get(s).add(new Edge(s,t,v));}// 初始化minDistint[] minDist=new int[n+1];Arrays.fill(minDist,Integer.MAX_VALUE);minDist[1]=0;// 存储已更新节点相连的点Queue<Integer> queue=new LinkedList<>();queue.add(1);// 判断节点是否已经在队列中boolean[] inQueue=new boolean[n+1];while (!queue.isEmpty()){int cur=queue.poll();inQueue[cur]=false;for (Edge edge:grid.get(cur)){if (minDist[cur]+edge.val<minDist[edge.to]){minDist[edge.to]=minDist[cur]+edge.val;if (!inQueue[edge.to]){queue.offer(edge.to);inQueue[edge.to]=true;}}}}if (minDist[n]!=Integer.MAX_VALUE){System.out.println(minDist[n]);}else {System.out.println("unconnected");}}
}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;}
}

2. bellman_ford之判断负权回路

题目链接:95. 城市间货物运输 II (kamacoder.com)

思路:这道题中出现了负权回路。在没有负权回路的图中,松弛 n 次以上 ,结果不会有变化;但在有负权回路的情况下,如果松弛 n 次,结果就会有变化了,因为有负权回路可以无限最短路径(一直绕圈,就可以一直得到无限小的最短距离)。

(1)如果没有用队列优化,则松弛n次看终点的成本会不会变化

(2)如果用队列优化的版本,假设每个节点和所有的节点相连则有n-1条边,每个节点最多放入队列n-1次。如果有优先队列则存在节点被放入队列n次

方法:

import java.util.*;public class Main{public static void main(String[] args){Scanner sc=new Scanner(System.in);// 接收数据int n=sc.nextInt();int m=sc.nextInt();List<List<Edge>> grid=new ArrayList<>(n+1);for (int i=0;i<n+1;i++){grid.add(new ArrayList<>());}for (int i=0;i<m;i++){int s=sc.nextInt();int t=sc.nextInt();int v=sc.nextInt();grid.get(s).add(new Edge(s,t,v));}// 初始化minDistint[] minDist=new int[n+1];Arrays.fill(minDist,Integer.MAX_VALUE);minDist[1]=0;// 存储已更新节点相连的点Queue<Integer> queue=new LinkedList<>();queue.add(1);// 判断节点是否已经在队列中boolean[] inQueue=new boolean[n+1];// 记录节点加入队列的次数int[] count=new int[n+1];count[1]++;// 是否存在负权回路boolean flag=false;while (!queue.isEmpty()){int cur=queue.poll();inQueue[cur]=false;for (Edge edge:grid.get(cur)){if (minDist[cur]+edge.val<minDist[edge.to]){minDist[edge.to]=minDist[cur]+edge.val;if (!inQueue[edge.to]){queue.offer(edge.to);inQueue[edge.to]=true;count[edge.to]++;}if (count[edge.to]==n){flag=true;while (!queue.isEmpty()){  // 结束内外层循环queue.poll();}break;}}}}if (flag){System.out.println("circle");return;}if (minDist[n]!=Integer.MAX_VALUE){System.out.println(minDist[n]);}else {System.out.println("unconnected");}}
}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;}
}

3. bellman_ford之单源有限最短路

题目链接:96. 城市间货物运输 III (kamacoder.com)

思路:因为最多可以经过k个城市,所以对所有边进行k+1次松弛操作。每一次松弛操作都要基于上一次松弛操作的结果(这个目前不明白,先这样写吧,之后填坑。讲解在这里代码随想录

方法:

import java.util.*;public class Main{public static void main(String[] args){Scanner sc=new Scanner(System.in);int n=sc.nextInt();int m=sc.nextInt();// 存储边List<Edge> edges=new ArrayList<>(m);for (int i=0;i<m;i++){int s=sc.nextInt();int t=sc.nextInt();int v=sc.nextInt();edges.add(new Edge(s,t,v));}int src=sc.nextInt();int dst=sc.nextInt();int k=sc.nextInt();// minDist数组和初始化int[] minDist=new int[n+1];Arrays.fill(minDist,Integer.MAX_VALUE);minDist[src]=0;int[] copy=new int[n+1];for (int i=0;i<k+1;i++){  // k+1次松弛边copy=Arrays.copyOf(minDist,n+1);for (Edge edge:edges){if (copy[edge.from]!=Integer.MAX_VALUE&&copy[edge.from]+edge.val<minDist[edge.to]){minDist[edge.to]=copy[edge.from]+edge.val;}}}if (minDist[dst]==Integer.MAX_VALUE){System.out.println("unreachable");}else {System.out.println(minDist[dst]);}}
}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;}
}

http://www.mrgr.cn/news/39310.html

相关文章:

  • CSS给一行按钮统一设置间隔
  • C# 调用虚拟打印,尝试隐藏进度窗体
  • 低代码平台中的宿主概念解析与字典、角色、岗位及权限管理
  • 推荐4款2024年热门的PDF转ppt工具
  • CSS 的user-select属性,控制用户是否能够选中文本内容
  • 基于springboot+小程序的自习室选座与门禁管理系统(自习室1)(源码+sql脚本+视频导入教程+文档)
  • [算法日常] 分层图最短路
  • 心觉:如何重塑高效学习的潜意识(5)终结篇
  • 什么是计算机视觉算法?一文读懂!
  • 浅谈GDDRAM的三种寻址模式
  • Latex 自定义运算符加限定条件的实现
  • 《深度学习》OpenCV 图像拼接 拼接原理、参数解析、案例实现
  • 【STM32开发环境搭建】-4-在STM32CubeMX中新增Keil(MDK-ARM) 5的工程目录(包含指定路径的C和H文件)
  • cuda程序编译流程
  • 时间复杂度及空间复杂度(简略)
  • 842真题上的各种简答题
  • MySQL - 运维篇
  • 基于Python的自然语言处理系列(19):基于LSTM的语言模型实现
  • 高效的视频压缩标准H.264介绍,以及H.264在视频监控系统中的应用
  • Js垃圾回收的两种方式