-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
45 lines (37 loc) Β· 1.38 KB
/
Main.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
38
39
40
41
42
43
44
45
package Graph.P11404;
import java.io.*;
import java.util.*;
public class Main {
static int n, m;
static int[][] D;
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Graph/P11404/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
m = Integer.parseInt(br.readLine());
D = new int[n+1][n+1];
while (m-- > 0) {
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
if (D[a][b] > 0) D[a][b] = Math.min(D[a][b], c);
else D[a][b] = c;
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j || D[i][k] == 0 || D[k][j] == 0) continue;
if (D[i][j] == 0) D[i][j] = D[i][k] + D[k][j];
else D[i][j] = Math.min(D[i][j], D[i][k] + D[k][j]);
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(D[i][j] + " ");
}
System.out.println();
}
}
}