Skip to content

Commit

Permalink
docs:【0097-小明逛公园】补充 JAVA版本【基于三维数组的Floyd算法】
Browse files Browse the repository at this point in the history
docs:【0097-小明逛公园】补充 JAVA版本【基于三维数组的Floyd算法】
  • Loading branch information
holic-x committed Dec 12, 2024
1 parent 96bb3d1 commit b5cbc15
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions problems/kamacoder/0097.小明逛公园.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,71 @@ floyd算法的时间复杂度相对较高,适合 稠密图且源点较多的

### Java

- 基于三维数组的Floyd算法

```java
public class FloydBase {

// public static int MAX_VAL = Integer.MAX_VALUE;
public static int MAX_VAL = 10005; // 边的最大距离是10^4(不选用Integer.MAX_VALUE是为了避免相加导致数值溢出)

public static void main(String[] args) {
// 输入控制
Scanner sc = new Scanner(System.in);
System.out.println("1.输入N M");
int n = sc.nextInt();
int m = sc.nextInt();

System.out.println("2.输入M条边");

// ① dp定义(grid[i][j][k] 节点i到节点j 可能经过节点K(k∈[1,n]))的最短路径
int[][][] grid = new int[n + 1][n + 1][n + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 0; k <= n; k++) {
grid[i][j][k] = grid[j][i][k] = MAX_VAL; // 其余设置为最大值
}
}
}

// ② dp 推导:grid[i][j][k] = min{grid[i][k][k-1] + grid[k][j][k-1], grid[i][j][k-1]}
while (m-- > 0) {
int u = sc.nextInt();
int v = sc.nextInt();
int weight = sc.nextInt();
grid[u][v][0] = grid[v][u][0] = weight; // 初始化(处理k=0的情况) ③ dp初始化
}

// ④ dp推导:floyd 推导
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
grid[i][j][k] = Math.min(grid[i][k][k - 1] + grid[k][j][k - 1], grid[i][j][k - 1]);
}
}
}

System.out.println("3.输入[起点-终点]计划个数");
int x = sc.nextInt();

System.out.println("4.输入每个起点src 终点dst");

while (x-- > 0) {
int src = sc.nextInt();
int dst = sc.nextInt();
// 根据floyd推导结果输出计划路径的最小距离
if (grid[src][dst][n] == MAX_VAL) {
System.out.println("-1");
} else {
System.out.println(grid[src][dst][n]);
}
}
}
}
```



### Python

基于三维数组的Floyd
Expand Down

0 comments on commit b5cbc15

Please sign in to comment.