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

devtools/2024/10/18 15:34:56/

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

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

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

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

方法:

java">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次

方法:

java">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次松弛操作。每一次松弛操作都要基于上一次松弛操作的结果(这个目前不明白,先这样写吧,之后填坑。讲解在这里代码随想录

方法:

java">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.ppmy.cn/devtools/120986.html

相关文章

ArcGIS与ArcGIS Pro去除在线地图服务名单

我们之前给大家分享了很多在线地图集&#xff0c;有些地图集会带有制作者信息&#xff0c;在布局制图的时候会带上信息影响出图美观。 一套GIS图源集搞定&#xff01;清新规划底图、影像图、境界、海洋、地形阴影图、导航图 比如ArcGIS&#xff1a; 比如ArcGIS Pro&#xff1a…

每日一题学习笔记

编写一个函数&#xff0c;其作用是将输入的字符串反转过来。输入字符串以字符数组 s 的形式给出。 不要给另外的数组分配额外的空间&#xff0c;你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。 示例 1&#xff1a; 输入&#xff1a;s ["h","e…

微服务实战——平台属性

平台属性 中间表复杂业务 /*** 获取分类规格参数&#xff08;模糊查询&#xff09;** param params* param catelogId* param type type"base"时查询基础属性&#xff0c;type"sale"时查询销售属性* return*/ Override public PageUtils listByCatelogId…

基于无人机图像的洪水灾害受损评估分割数据集,共4494张高清无人机图像,10个类别,共22GB数据量,主要关注道路,建筑的受损情况。洪水应急救援

洪水应急救援&#xff0c;基于无人机图像的洪水灾害受损评估分割数据集&#xff0c;共4494张高清无人机图像&#xff0c;10个类别&#xff0c;共22GB数据量&#xff0c;主要关注道路&#xff0c;建筑的受损情况。 洪水应急救援&#xff0c;基于无人机图像的洪水灾害受损评估分…

SpringCloud-Netflix第一代微服务快速入门

1.springCloud常用组件 Netflix Eureka 当我们的微服务过多的时候&#xff0c;管理服务的通信地址是一个非常麻烦的事情&#xff0c;Eureka就是用来管理微服务的通信地址清单的&#xff0c;有了Eureka之后我们通过服务的名字就能实现服务的调用。 Netflix Ribbon\Feign : 客…

华硕天选笔记本外接音箱没有声音

系列文章目录 文章目录 系列文章目录一.前言二.解决方法第一种方法第二种方法 一.前言 华硕天选笔记本外接音箱没有声音&#xff0c;在插上外接音箱时&#xff0c;系统会自动弹出下图窗口 二.解决方法 第一种方法 在我的电脑上选择 Headphone Speaker Out Headset 这三个选项…

PHP 基础语法详解

✅作者简介&#xff1a;2022年博客新星 第八。热爱国学的Java后端开发者&#xff0c;修心和技术同步精进。 &#x1f34e;个人主页&#xff1a;Java Fans的博客 &#x1f34a;个人信条&#xff1a;不迁怒&#xff0c;不贰过。小知识&#xff0c;大智慧。 &#x1f49e;当前专栏…