-
Notifications
You must be signed in to change notification settings - Fork 1
/
p11404.java
52 lines (48 loc) · 1.62 KB
/
p11404.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
46
47
48
49
50
51
52
/*
플로이드
인접행렬 + 플로이드 와샬
*/
import java.io.*;
import java.util.*;
public class p11404 {
static final int INF = 1000000000;
static int n, m;
static int[][] dist;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
m = Integer.parseInt(br.readLine());
dist = new int[n + 1][n + 1];
for (int i = 0; i <= n ; i++) {
for (int j = 0; j <= n; j++) {
dist[i][j] = i == j ? 0 : INF;
}
}
StringTokenizer st;
for (int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
int u = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
int cost = Integer.parseInt(st.nextToken());
dist[u][v] = Math.min(dist[u][v], cost);
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (dist[i][j] > dist[i][k] + dist[k][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (dist[i][j] >= INF) sb.append("0").append(" ");
else sb.append(dist[i][j]).append(" ");
}
sb.append("\n");
}
System.out.println(sb.toString());
}
}