Last active
August 27, 2020 18:18
-
-
Save bvolpato/b442fc0a333a2c1144e36872af1ed018 to your computer and use it in GitHub Desktop.
Algorithm - Floyd-Warshall
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Scanner; | |
/** | |
* Floyd-Warshall Java Implementation | |
*/ | |
public class FloydWarshall { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
// n x n | |
int n = sc.nextInt(); | |
// source | |
int s = sc.nextInt(); | |
// target | |
int t = sc.nextInt(); | |
int[][] D = new int[n][n]; | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j < n; j++) { | |
D[i][j] = sc.nextInt(); | |
} | |
} | |
// Floyd-Warshall Algorithm | |
for (int k = 0; k < n; k++) { | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j < n; j++) { | |
if (D[i][j] > D[i][k] + D[k][j]) { | |
D[i][j] = D[i][k] + D[k][j]; | |
} | |
} | |
} | |
} | |
System.out.println(D[s][t]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment