参考代码:
import java.util.*; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int n = cin.nextInt(); int[][] d = new int[n][n]; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { int z = cin.nextInt(); if(i != j && z != 0) d[i][j] = z; else d[i][j] = (int) 1e9; } } for(int k = 0; k < n; k++) { for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { d[i][j] = Math.min(d[i][j], d[i][k] + d[k][j]); } } } for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(i == j) { System.out.print(0 + " "); } else { int ans = d[i][j] == (int) 1e9 ? -1 : d[i][j]; System.out.print(ans + " "); } } System.out.println(); } } }
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:909 |
A+B for Input-Output Practice (V) (C语言代码)浏览:640 |
三角形 (C++代码)记忆化搜索浏览:1317 |
校门外的树 (C语言代码)浏览:733 |
简单的a+b (C语言代码)浏览:661 |
1009题解浏览:802 |
sizeof的大作用 (C语言代码)浏览:1591 |
简单的a+b (C语言代码)浏览:574 |
C二级辅导-求偶数和 (C语言代码)浏览:707 |
排序算法(选择,插入,冒泡)浏览:876 |