标签:解法 floyd算法 顶点 new code min https mat floyd
本博客只做代码训练, 理论阅读请直接点
https://www.cnblogs.com/ECJTUACM-873284962/p/6995648.html
解决图中最短路径问题
核心思想是DP。
状态转移矩阵:f[k][i][j] = min(f[k-1][i][j], f[k-1][i][k]+f[k-1][k][j])
1 package algorithm; 2 3 public class Floyd { 4 private int n; 5 6 private class Graph { 7 int[][] edges; 8 char[] vertex; 9 } 10 11 void floyd(Graph g) { 12 int[][] A = new int[n][n]; 13 for (int i = 0; i < n; i++) { 14 for (int j = 0; j < n; j++) { 15 A[i][j] = g.edges[i][j]; 16 } 17 } 18 19 for (int k = 0; k < n; k++) { 20 for (int i = 0; i < n; i++) 21 for (int j = 0; j < n; j++) { 22 A[i][j] = Math.min(A[i][j], A[i][k] + A[k][j]); 23 } 24 } 25 } 26 }
标签:解法 floyd算法 顶点 new code min https mat floyd
原文地址:https://www.cnblogs.com/ylxn/p/10357005.html