百度百科里面的floyd算法java的代码,总是无法运行。请问是代码有问题吗,如何编译啊?
发布网友
发布时间:2022-04-14 17:36
我来回答
共4个回答
热心网友
时间:2022-04-14 19:06
不能编译运行的说法是错误,但是结果是否正确,我就不知道了,我不懂这个算法
public class FLOYD {
int[][] length = null;// 任意两点之间路径长度
int[][][] path = null;// 任意两点之间的路径
public FLOYD(int[][] G) {
int MAX = 100;
int row = G.length;// 图G的行数
int[][] spot = new int[row][row];// 定义任意两点之间经过的点
int[] onePath = new int[row];// 记录一条路径
length = new int[row][row];
path = new int[row][row][];
for (int i = 0; i < row; i++)
// 处理图两点之间的路径
for (int j = 0; j < row; j++) {
if (G[i][j] == 0)
G[i][j] = MAX;// 没有路径的两个点之间的路径为默认最大
if (i == j)
G[i][j] = 0;// 本身的路径长度为0
}
for (int i = 0; i < row; i++)
// 初始化为任意两点之间没有路径
for (int j = 0; j < row; j++)
spot[i][j] = -1;
for (int i = 0; i < row; i++)
// 假设任意两点之间的没有路径
onePath[i] = -1;
for (int v = 0; v < row; ++v)
for (int w = 0; w < row; ++w)
length[v][w] = G[v][w];
for (int u = 0; u < row; ++u)
for (int v = 0; v < row; ++v)
for (int w = 0; w < row; ++w)
if (length[v][w] > length[v][u] + length[u][w]) {
length[v][w] = length[v][u] + length[u][w];// 如果存在更短路径则取更短路径
spot[v][w] = u;// 把经过的点加入
}
for (int i = 0; i < row; i++) {// 求出所有的路径
int[] point = new int[1];
for (int j = 0; j < row; j++) {
point[0] = 0;
onePath[point[0]++] = i;
outputPath(spot, i, j, onePath, point);
path[i][j] = new int[point[0]];
for (int s = 0; s < point[0]; s++)
path[i][j][s] = onePath[s];
}
}
}
void outputPath(int[][] spot, int i, int j, int[] onePath, int[] point) {// 输出i//
// 到j//
// 的路径的实际代码,point[]记录一条路径的长度
if (i == j)
return;
if (spot[i][j] == -1)
onePath[point[0]++] = j;
// System.out.print(" "+j+" ");
else {
outputPath(spot, i, spot[i][j], onePath, point);
outputPath(spot, spot[i][j], j, onePath, point);
}
}
public static void main(String[] args) {
int data[][] = {
{ 0, 27, 44, 17, 11, 27, 42, 0, 0, 0, 20, 25, 21, 21, 18, 27, 0 },// x1
{ 27, 0, 31, 27, 49, 0, 0, 0, 0, 0, 0, 0, 52, 21, 41, 0, 0 },// 1
{ 44, 31, 0, 19, 0, 27, 32, 0, 0, 0, 47, 0, 0, 0, 32, 0, 0 },// 2
{ 17, 27, 19, 0, 14, 0, 0, 0, 0, 0, 30, 0, 0, 0, 31, 0, 0 },// 3
{ 11, 49, 0, 14, 0, 13, 20, 0, 0, 28, 15, 0, 0, 0, 15, 25, 30 },// 4
{ 27, 0, 27, 0, 13, 0, 9, 21, 0, 26, 26, 0, 0, 0, 28, 29, 0 },// 5
{ 42, 0, 32, 0, 20, 9, 0, 13, 0, 32, 0, 0, 0, 0, 0, 33, 0 },// 6
{ 0, 0, 0, 0, 0, 21, 13, 0, 19, 0, 0, 0, 0, 0, 0, 0, 0 },// 7
{ 0, 0, 0, 0, 0, 0, 0, 19, 0, 11, 20, 0, 0, 0, 0, 33, 21 },// 8
{ 0, 0, 0, 0, 28, 26, 32, 0, 11, 0, 10, 20, 0, 0, 29, 14, 13 },// 9
{ 20, 0, 47, 30, 15, 26, 0, 0, 20, 10, 0, 18, 0, 0, 14, 9, 20 },// 10
{ 25, 0, 0, 0, 0, 0, 0, 0, 0, 20, 18, 0, 23, 0, 0, 14, 0 },// 11
{ 21, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 27, 22, 0, 0 },// 12
{ 21, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 27, 0, 0, 0, 0 },// 13
{ 18, 41, 32, 31, 15, 28, 0, 0, 0, 29, 14, 0, 22, 0, 0, 11, 0 },// 14
{ 27, 0, 0, 0, 25, 29, 33, 0, 33, 14, 9, 14, 0, 0, 11, 0, 9 },// 15
{ 0, 0, 0, 0, 30, 0, 0, 0, 21, 13, 20, 0, 0, 0, 0, 9, 0 } // 16
};
for (int i = 0; i < data.length; i++)
for (int j = i; j < data.length; j++)
if (data[i][j] != data[j][i])
return;
FLOYD test = new FLOYD(data);
for (int i = 0; i < data.length; i++)
for (int j = i; j < data[i].length; j++) {
System.out.println();
System.out.print("From " + i + " to " + j + " path is: ");
for (int k = 0; k < test.path[i][j].length; k++)
System.out.print(test.path[i][j][k] + " ");
System.out.println();
System.out.println("From " + i + " to " + j + " length :"
+ test.length[i][j]);
}
}
}
热心网友
时间:2022-04-14 20:24
java要运行首先要安装jdk,配置环境变量。eclipse只是一个编译工具,你也可以用记事本来编辑代码,文件后缀名保存为.java,然后用命令将java文件编译成.class文件,运行.class文件就行了
从你给的地址里复制出来的java代码,运行正常,一下书运行输出结果:
From 0 to 0 path is: 0 From 0 to 0 length :0
From 0 to 1 path is: 0 1 From 0 to 1 length :27
From 0 to 2 path is: 0 3 2 From 0 to 2 length :36
From 0 to 3 path is: 0 3 From 0 to 3 length :17
From 0 to 4 path is: 0 4 From 0 to 4 length :11
From 0 to 5 path is: 0 4 5 From 0 to 5 length :24
From 0 to 6 path is: 0 4 6 From 0 to 6 length :31
From 0 to 7 path is: 0 4 6 7 From 0 to 7 length :44
From 0 to 8 path is: 0 10 8 From 0 to 8 length :40
From 0 to 9 path is: 0 10 9 From 0 to 9 length :30
From 0 to 10 path is: 0 10 From 0 to 10 length :20
From 0 to 11 path is: 0 11 From 0 to 11 length :25
From 0 to 12 path is: 0 12 From 0 to 12 length :21
From 0 to 13 path is: 0 13 From 0 to 13 length :21
From 0 to 14 path is: 0 14 From 0 to 14 length :18
From 0 to 15 path is: 0 15 From 0 to 15 length :27
From 0 to 16 path is: 0 15 16 From 0 to 16 length :36
From 1 to 1 path is: 1 From 1 to 1 length :0
From 1 to 2 path is: 1 2 From 1 to 2 length :31
From 1 to 3 path is: 1 3 From 1 to 3 length :27
From 1 to 4 path is: 1 0 4 From 1 to 4 length :38
From 1 to 5 path is: 1 0 4 5 From 1 to 5 length :51
From 1 to 6 path is: 1 0 4 6 From 1 to 6 length :58
From 1 to 7 path is: 1 0 4 6 7 From 1 to 7 length :71
From 1 to 8 path is: 1 0 10 8 From 1 to 8 length :67
From 1 to 9 path is: 1 0 10 9 From 1 to 9 length :57
From 1 to 10 path is: 1 0 10 From 1 to 10 length :47
From 1 to 11 path is: 1 0 11 From 1 to 11 length :52
From 1 to 12 path is: 1 0 12 From 1 to 12 length :48
From 1 to 13 path is: 1 13 From 1 to 13 length :21
From 1 to 14 path is: 1 14 From 1 to 14 length :41
From 1 to 15 path is: 1 14 15 From 1 to 15 length :52
From 1 to 16 path is: 1 14 15 16 From 1 to 16 length :61
From 2 to 2 path is: 2 From 2 to 2 length :0
From 2 to 3 path is: 2 3 From 2 to 3 length :19
From 2 to 4 path is: 2 3 4 From 2 to 4 length :33
From 2 to 5 path is: 2 5 From 2 to 5 length :27
From 2 to 6 path is: 2 6 From 2 to 6 length :32
From 2 to 7 path is: 2 6 7 From 2 to 7 length :45
From 2 to 8 path is: 2 6 7 8 From 2 to 8 length :64
From 2 to 9 path is: 2 5 9 From 2 to 9 length :53
From 2 to 10 path is: 2 14 10 From 2 to 10 length :46
From 2 to 11 path is: 2 14 15 11 From 2 to 11 length :57
From 2 to 12 path is: 2 14 12 From 2 to 12 length :54
From 2 to 13 path is: 2 1 13 From 2 to 13 length :52
From 2 to 14 path is: 2 14 From 2 to 14 length :32
From 2 to 15 path is: 2 14 15 From 2 to 15 length :43
From 2 to 16 path is: 2 14 15 16 From 2 to 16 length :52
From 3 to 3 path is: 3 From 3 to 3 length :0
From 3 to 4 path is: 3 4 From 3 to 4 length :14
From 3 to 5 path is: 3 4 5 From 3 to 5 length :27
From 3 to 6 path is: 3 4 6 From 3 to 6 length :34
From 3 to 7 path is: 3 4 6 7 From 3 to 7 length :47
From 3 to 8 path is: 3 4 10 8 From 3 to 8 length :49
From 3 to 9 path is: 3 4 10 9 From 3 to 9 length :39
From 3 to 10 path is: 3 4 10 From 3 to 10 length :29
From 3 to 11 path is: 3 0 11 From 3 to 11 length :42
From 3 to 12 path is: 3 0 12 From 3 to 12 length :38
From 3 to 13 path is: 3 0 13 From 3 to 13 length :38
From 3 to 14 path is: 3 4 14 From 3 to 14 length :29
From 3 to 15 path is: 3 4 10 15 From 3 to 15 length :38
From 3 to 16 path is: 3 4 16 From 3 to 16 length :44
From 4 to 4 path is: 4 From 4 to 4 length :0
From 4 to 5 path is: 4 5 From 4 to 5 length :13
From 4 to 6 path is: 4 6 From 4 to 6 length :20
From 4 to 7 path is: 4 6 7 From 4 to 7 length :33
From 4 to 8 path is: 4 10 8 From 4 to 8 length :35
From 4 to 9 path is: 4 10 9 From 4 to 9 length :25
From 4 to 10 path is: 4 10 From 4 to 10 length :15
From 4 to 11 path is: 4 10 11 From 4 to 11 length :33
From 4 to 12 path is: 4 0 12 From 4 to 12 length :32
From 4 to 13 path is: 4 0 13 From 4 to 13 length :32
From 4 to 14 path is: 4 14 From 4 to 14 length :15
From 4 to 15 path is: 4 10 15 From 4 to 15 length :24
From 4 to 16 path is: 4 16 From 4 to 16 length :30
From 5 to 5 path is: 5 From 5 to 5 length :0
From 5 to 6 path is: 5 6 From 5 to 6 length :9
From 5 to 7 path is: 5 7 From 5 to 7 length :21
From 5 to 8 path is: 5 9 8 From 5 to 8 length :37
From 5 to 9 path is: 5 9 From 5 to 9 length :26
From 5 to 10 path is: 5 10 From 5 to 10 length :26
From 5 to 11 path is: 5 15 11 From 5 to 11 length :43
From 5 to 12 path is: 5 4 0 12 From 5 to 12 length :45
From 5 to 13 path is: 5 4 0 13 From 5 to 13 length :45
From 5 to 14 path is: 5 14 From 5 to 14 length :28
From 5 to 15 path is: 5 15 From 5 to 15 length :29
From 5 to 16 path is: 5 15 16 From 5 to 16 length :38
From 6 to 6 path is: 6 From 6 to 6 length :0
From 6 to 7 path is: 6 7 From 6 to 7 length :13
From 6 to 8 path is: 6 7 8 From 6 to 8 length :32
From 6 to 9 path is: 6 9 From 6 to 9 length :32
From 6 to 10 path is: 6 4 10 From 6 to 10 length :35
From 6 to 11 path is: 6 15 11 From 6 to 11 length :47
From 6 to 12 path is: 6 4 0 12 From 6 to 12 length :52
From 6 to 13 path is: 6 4 0 13 From 6 to 13 length :52
From 6 to 14 path is: 6 4 14 From 6 to 14 length :35
From 6 to 15 path is: 6 15 From 6 to 15 length :33
From 6 to 16 path is: 6 15 16 From 6 to 16 length :42
From 7 to 7 path is: 7 From 7 to 7 length :0
From 7 to 8 path is: 7 8 From 7 to 8 length :19
From 7 to 9 path is: 7 8 9 From 7 to 9 length :30
From 7 to 10 path is: 7 8 10 From 7 to 10 length :39
From 7 to 11 path is: 7 8 9 11 From 7 to 11 length :50
From 7 to 12 path is: 7 6 4 0 12 From 7 to 12 length :65
From 7 to 13 path is: 7 6 4 0 13 From 7 to 13 length :65
From 7 to 14 path is: 7 6 4 14 From 7 to 14 length :48
From 7 to 15 path is: 7 8 9 15 From 7 to 15 length :44
From 7 to 16 path is: 7 8 16 From 7 to 16 length :40
From 8 to 8 path is: 8 From 8 to 8 length :0
From 8 to 9 path is: 8 9 From 8 to 9 length :11
From 8 to 10 path is: 8 10 From 8 to 10 length :20
From 8 to 11 path is: 8 9 11 From 8 to 11 length :31
From 8 to 12 path is: 8 9 11 12 From 8 to 12 length :54
From 8 to 13 path is: 8 10 0 13 From 8 to 13 length :61
From 8 to 14 path is: 8 10 14 From 8 to 14 length :34
From 8 to 15 path is: 8 9 15 From 8 to 15 length :25
From 8 to 16 path is: 8 16 From 8 to 16 length :21
From 9 to 9 path is: 9 From 9 to 9 length :0
From 9 to 10 path is: 9 10 From 9 to 10 length :10
From 9 to 11 path is: 9 11 From 9 to 11 length :20
From 9 to 12 path is: 9 11 12 From 9 to 12 length :43
From 9 to 13 path is: 9 10 0 13 From 9 to 13 length :51
From 9 to 14 path is: 9 10 14 From 9 to 14 length :24
From 9 to 15 path is: 9 15 From 9 to 15 length :14
From 9 to 16 path is: 9 16 From 9 to 16 length :13
From 10 to 10 path is: 10 From 10 to 10 length :0
From 10 to 11 path is: 10 11 From 10 to 11 length :18
From 10 to 12 path is: 10 14 12 From 10 to 12 length :36
From 10 to 13 path is: 10 0 13 From 10 to 13 length :41
From 10 to 14 path is: 10 14 From 10 to 14 length :14
From 10 to 15 path is: 10 15 From 10 to 15 length :9
From 10 to 16 path is: 10 15 16 From 10 to 16 length :18
From 11 to 11 path is: 11 From 11 to 11 length :0
From 11 to 12 path is: 11 12 From 11 to 12 length :23
From 11 to 13 path is: 11 0 13 From 11 to 13 length :46
From 11 to 14 path is: 11 15 14 From 11 to 14 length :25
From 11 to 15 path is: 11 15 From 11 to 15 length :14
From 11 to 16 path is: 11 15 16 From 11 to 16 length :23
From 12 to 12 path is: 12 From 12 to 12 length :0
From 12 to 13 path is: 12 13 From 12 to 13 length :27
From 12 to 14 path is: 12 14 From 12 to 14 length :22
From 12 to 15 path is: 12 14 15 From 12 to 15 length :33
From 12 to 16 path is: 12 14 15 16 From 12 to 16 length :42
From 13 to 13 path is: 13 From 13 to 13 length :0
From 13 to 14 path is: 13 0 14 From 13 to 14 length :39
From 13 to 15 path is: 13 0 15 From 13 to 15 length :48
From 13 to 16 path is: 13 0 15 16 From 13 to 16 length :57
From 14 to 14 path is: 14 From 14 to 14 length :0
From 14 to 15 path is: 14 15 From 14 to 15 length :11
From 14 to 16 path is: 14 15 16 From 14 to 16 length :20
From 15 to 15 path is: 15 From 15 to 15 length :0
From 15 to 16 path is: 15 16 From 15 to 16 length :9
From 16 to 16 path is: 16 From 16 to 16 length :0追问请问把整端代码复制到一个txt(只用一个txt?),起名为floyd, javac floyd.java就可以了吗。我javac 后没问题,运行有问题(Exception in thread "main" java.lang.UnsupportedClassVersionError)。
追答检查环境变量,实在不行的话就用eclipse吧,在eclipse里随便创建一个java工程,在工程里创建一个类把代码全部复制进去(注意保留类里面的package行,如果没有就不管了),确认没有红叉报错,直接运行就行了。
热心网友
时间:2022-04-14 21:58
首先你要看下 你粘贴进去报错没有,包名啊类名是否一致 既然高java 我想你肯定装jdk了吧 跟编译环境没有太大关系
热心网友
时间:2022-04-14 23:50
编译存在的问题,您要仔细检查一下