Este exemplo compartilha o código específico para a implementação Java do algoritmo Floyd para encontrar o caminho mais curto para sua referência. O conteúdo específico é o seguinte
importar java.io.fileInputStream; importar java.io.filenotfoundException; importar java.util.scanner; classe pública testMainio { / ** * @param args * @throws filenotfoundException * / public static void main (string [] args) lança fileNotfoundException {testMainio test_print = new testMainio (); int [] [] g = test_print.intputGragh ("d: //users//test.txt", 6); int [] [] dis = test_print.floyd (g, 6); test_print.printg (dis, 6); } public void printg (int [] [] g, int n) {for (int i = 0; i <n; i ++) {for (int j = 0; j <n; j ++) {System.out.println (i+"->"+j+""+g [i] [j]); }}} public int [] [] intgragh (caminho da string, int num) lança fileNotfoundException {int [] [] g = new int [num] [num]; for (int i = 0; i <num; i ++) {for (int j = 0; j <num; j ++) {g [i] [j] = 999; }} Scanner in = new Scanner (new FileInputStream (caminho)); while (in.hasNext ()) {int i = in.NextInt (); int j = in.nextInt (); int peso = in.nextInt (); G [i] [j] = peso; } retornar g; } public int [] [] floyd (int [] [] g, int n) {int [] [] dis = new int [n] [n]; for (int q = 0; q <n; q ++) {for (int w = 0; w <n; w ++) {dis [q] [w] = g [q] [w]; }} para (int k = 0; k <n; k ++) {for (int i = 0; i <n; i ++) {for (int j = 0; j <n; j ++) {if (dis [i] [j]>> [i] [k]+dism [k] }}} retornar dis; }}}}O exposto acima é todo o conteúdo deste artigo. Espero que seja útil para o aprendizado de todos e espero que todos apoiem mais o wulin.com.