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

搜搜提交网站龙岗做网站公司哪家好

搜搜提交网站,龙岗做网站公司哪家好,百度后台登录,游戏软件开发定制ZZHow(ZZHow1024) 💡 图的最短路径问题: 单源最短路 所有边权都是正数 朴素 Dijkstra 算法 O(n2)O(n ^ 2)O(n2)堆优化版 Dijkstra 算法 O(m∗log2n)O(m*log_2 n)O(m∗log2​n) 存在负权边 Bellman-Ford 算法 O(n∗m)O(n * m)O(n∗m)SPFA 算法 一般O(m)&…

@ZZHow(ZZHow1024)

图的最短路径问题:

  • 单源最短路
    • 所有边权都是正数
      • 朴素 Dijkstra 算法 O(n2)O(n ^ 2)O(n2)
      • 堆优化版 Dijkstra 算法 O(m∗log2n)O(m*log_2 n)O(mlog2n)
    • 存在负权边
      • Bellman-Ford 算法 O(n∗m)O(n * m)O(nm)
      • SPFA 算法 一般O(m),最坏O(n∗m)一般 O(m),最坏 O(n * m)一般O(m),最坏O(nm)
  • 多源汇最短路
    • Floyd 算法 O(n3)O(n ^ 3)O(n3)

朴素 Dijkstra

迪杰斯特拉算法采用的是一种贪心的策略。进行 n 次迭代去确定每个点到起点的最小值,最后输出的终点的即为我们要找的最短路的距离。

  • 例题:AcWing 849. Dijkstra求最短路 I
import java.util.Scanner;public class Main {static final int N = 510;static long[][] g = new long[N][N];static long[] dist = new long[N];static boolean[] st = new boolean[N];public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();int m = scanner.nextInt();for (int i = 0; i <= n; i++)for (int j = 0; j <= n; j++)g[i][j] = Integer.MAX_VALUE;while (m-- != 0) {int a = scanner.nextInt();int b = scanner.nextInt();int c = scanner.nextInt();g[a][b] = Math.min(g[a][b], c);}System.out.println(dijkstra(n));}public static long dijkstra(int n) {for (int i = 0; i <= n; i++)dist[i] = Integer.MAX_VALUE;dist[1] = 0;for (int i = 0; i < n; i++) {int t = -1;for (int j = 1; j <= n; j++)if (!st[j] && (t == -1 || dist[t] > dist[j]))t = j;st[t] = true;for (int j = 1; j <= n; j++)dist[j] = Math.min(dist[j], dist[t] + g[t][j]);}if (dist[n] == Integer.MAX_VALUE)return -1;elsereturn dist[n];}
}

堆优化版 Dijkstra

对朴素版 Dijkstra 进行优化,在时间复杂度最高的寻找距离最短的点 O(n2)O(n^2)O(n2) 时可以使用小根堆优化,Java 语言可使用优先队列(PriorityQueue),配合自定义的 Pair 类实现。

  • 例题:AcWing 850. Dijkstra求最短路 II
import java.util.*;public class Main {static final int N = 1000010;static int idx = 0;static int[] h = new int[N];static int[] e = new int[N];static int[] w = new int[N];static int[] ne = new int[N];static long[] dist = new long[N];static boolean[] st = new boolean[N];public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();int m = scanner.nextInt();Arrays.fill(h, -1);while (m-- != 0) {int a = scanner.nextInt();int b = scanner.nextInt();int c = scanner.nextInt();add(a, b, c);}System.out.println(dijkstra(n));}public static void add(int a, int b, int c) {e[idx] = b;w[idx] = c;ne[idx] = h[a];h[a] = idx++;}public static long dijkstra(int n) {Arrays.fill(dist, Integer.MAX_VALUE);dist[1] = 0;Queue<Pair> heap = new PriorityQueue<>();heap.add(new Pair(0L, 1));while (!heap.isEmpty()) {Pair t = heap.poll();long distance = t.first;int ver = t.second;if (st[ver])continue;st[ver] = true;for (int i = h[ver]; i != -1; i = ne[i]) {int j = e[i];if (dist[j] > distance + w[i]) {dist[j] = distance + w[i];heap.add(new Pair(dist[j], j));}}}if (dist[n] == Integer.MAX_VALUE)return -1;elsereturn dist[n];}
}class Pair implements Comparable<Pair> {public long first;public int second;public Pair() {}public Pair(long a, int b) {this.first = a;this.second = b;}@Overridepublic int compareTo(Pair o) {return (int)Math.signum(this.first - o.first);}
}

bellman-ford

迭代 nnn 次,每次循环所有边(a,b,w)(a, b, w)(a,b,w),更新路径:dist[b] = min(dist[b], dist[a] + w);(松弛操作)。全部操作完后满足三角不等式:dist[b]≤dist[a]+wdist[b] ≤ dist[a] + wdist[b]dist[a]+w

  • 例题:AcWing 853. 有边数限制的最短路
import java.util.*;public class Main {public static int n;public static int m;public static int k;public static int[] dist;public static int[] backup;public static Edge[] edge;public static final int MAX = 0x3f3f3f3f;public static void main(String[] args) {Scanner scanner= new Scanner(System.in);n = scanner.nextInt();m = scanner.nextInt();k = scanner.nextInt();dist = new int[n + 10];Arrays.fill(dist, MAX);dist[1] = 0;edge = new Edge[m + 10];for (int i = 0; i < m; i++) {int a = scanner.nextInt();int b = scanner.nextInt();int w = scanner.nextInt();edge[i] = new Edge(a, b, w);}int t = bellmanFord();if (t > MAX >> 1)System.out.println("impossible");elseSystem.out.println(t);}public static int bellmanFord() {for (int i = 0; i < k; i++) {backup = Arrays.copyOf(dist, dist.length);for (int j = 0; j < m; j++) {int a = edge[j].a;int b = edge[j].b;int w = edge[j].w;dist[b] = Math.min(dist[b], backup[a] + w);}}return dist[n];}
}class Edge {public int a;public int b;public int w;public Edge(int a, int b, int w) {this.a = a;this.b = b;this.w = w;}
}

SPFA

针对 bellman-ford 算法的 dist[b] = Math.min(dist[b], backup[a] + w); 使用队列进行优化。

  • 例题:AcWing 851. spfa求最短路
  • 例题:AcWing 852. spfa判断负环
import java.util.*;public class Main {public static int n;public static int m;public static int[] h;public static int[] w;public static int[] e;public static int[] ne;public static int idx;public static int[] dist;public static boolean[] st;public static final int MAX = 0x3f3f3f3f;public static void main(String[] args) {Scanner scanner = new Scanner(System.in);n = scanner.nextInt();m = scanner.nextInt();h = new int[n + 10];w = new int[m + 10];e = new int[m + 10];ne = new int[m + 10];dist = new int[n + 10];st = new boolean[n + 10];Arrays.fill(h, -1);while (m-- != 0) {int a = scanner.nextInt();int b = scanner.nextInt();int c = scanner.nextInt();add(a, b, c);}int t = spfa();if (t > MAX >> 1)System.out.println("impossible");elseSystem.out.println(t);}public static void add(int a, int b, int c) {e[idx] = b;w[idx] = c;ne[idx] = h[a];h[a] = idx++;}public static int spfa() {Arrays.fill(dist, MAX);dist[1] = 0;Queue<Integer> q = new ArrayDeque<>();q.add(1);st[1] = true;while (!q.isEmpty()) {int t = q.poll();st[t] = false;for (int i = h[t]; i != -1; i = ne[i]) {int j = e[i];if (dist[j] > dist[t] + w[i]) {dist[j] = dist[t] + w[i];if (!st[j]) {q.add(j);st[j] = true;}}}}return dist[n];}
}

Floyd

基于动态规划。枚举 k,i,jk, i, jk,i,j 更新路径:d(i, j) = min(d(i, j), d(i, k) + d(k, j));

  • 例题:AcWing 854. Floyd求最短路
import java.util.*;public class Main {public static int n;public static int m;public static int k;public static int[][] d;public static final int MAX = 0x3f3f3f3f;public static void main(String[] args) {Scanner scanner = new Scanner(System.in);n = scanner.nextInt();m = scanner.nextInt();k = scanner.nextInt();d = new int[n + 10][n + 10];for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)if (i == j)d[i][j] = 0;elsed[i][j] = MAX;while (m-- != 0) {int a = scanner.nextInt();int b = scanner.nextInt();int w = scanner.nextInt();d[a][b] = Math.min(d[a][b], w);}floyd();while (k-- != 0) {int a = scanner.nextInt();int b = scanner.nextInt();if (d[a][b] > MAX >> 1)System.out.println("impossible");elseSystem.out.println(d[a][b]);}}public static void floyd() {for (int k = 1; k <= n; k++)for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)d[i][j] = Math.min(d[i][j], d[i][k] + d[k][j]);}
}
http://www.dtcms.com/a/437576.html

相关文章:

  • 做网站网页文件网站开发实习生
  • 可以做动效的网站wordpress开启用户激活验证
  • 打鱼跟电子游戏网站怎么做c qq 互联网站开发代码
  • 佛山自动机设备骏域网站建设专家syntaxhighlighter wordpress
  • 网站开发适合女生不企业管理软件系统有哪些
  • 牌具做网站网站的关键词可以取消吗
  • 合理规划网站桑福生物科技网站开发
  • 网站维护中要多久才能重新进入10年中文域名注册多少费用
  • 昆明做个人网站.net网站建设实例
  • 做网站能收回吗做网站要了解哪些
  • 深圳seo网站简述电子商务网站开发的基本原则
  • 湖南网站优化代运营苏州网站建设自学
  • 厦门网站制作策划贵州省住房和城乡建设厅官方网站
  • 网站建设手机端是什么意思阿里免费做网站
  • 上海景泰建设股份有限公司网站网站怎么设置二级域名
  • 上海袜网站建设app推广代理
  • 化妆品商城网站建设开发策划方案网站备案管理系统
  • 插画网站网站开发赚钱
  • 德阳建设局网站首页网站没有被搜索引擎收录
  • wordpress大型网站wordpress主题 sen
  • 上海模板网站公司wordpress flat主题
  • 网站开店前的四项基本建设建设一个企业网站要多少钱
  • 诸城做网站公司wordpress排名怎样
  • 网站什么时候恢复彩色四川全美网络科技有限公司
  • 如何登录网站空间网站开发与应用课程讨论
  • 制作网页的网站有哪些拓者设计室内设计官网首页
  • 欧美网站源码哪里有网站建设中心
  • 猎头做单都有什么网站前端网页特效
  • 深圳网站建设公司乐云seowordpress如何添加目录菜单
  • 安全网站建设的研究方法4p 4c 4r营销理论区别