-
Notifications
You must be signed in to change notification settings - Fork 0
/
FloydWarshall.java
37 lines (37 loc) · 1023 Bytes
/
FloydWarshall.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.io.*;
import java.lang.*;
import java.util.Scanner;
import java.io.*;
//All pair shortest Path algorithm
class FlyodWarshall{
public static void main(String[] args){
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int[][] arr=new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i!=j)arr[i][j]=Integer.MAX_VALUE;
}
}
int m=s.nextInt();
for(int i=0;i<m;i++){
int a=s.nextInt();
int b=s.nextInt();
int w=s.nextInt();
arr[a][b]=w;
arr[b][a]=w;
}
for(int k=0;k<n;k++)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(arr[i][k]!=Integer.MAX_VALUE && arr[k][j]!=Integer.MAX_VALUE && arr[i][j]>arr[i][k]+arr[k][j]){
arr[i][j]=arr[i][k]+arr[k][j];
}
}
}
}
}
}