Skip to content

Commit 0196813

Browse files
authored
Merge pull request #378 from AlgorithmWithGod/Seol-JY
[20250621] BOJ / G4 / 플로이드 / 설진영
2 parents c9c9e1d + a5fdcc2 commit 0196813

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
```java
2+
import java.io.*;
3+
import java.util.StringTokenizer;
4+
5+
public class Main {
6+
static final int INF = 987654321;
7+
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
11+
12+
int n = Integer.parseInt(br.readLine());
13+
int m = Integer.parseInt(br.readLine());
14+
15+
int[][] dist = new int[n + 1][n + 1];
16+
17+
for (int i = 1; i <= n; i++) {
18+
for (int j = 1; j <= n; j++) {
19+
if (i == j) {
20+
dist[i][j] = 0;
21+
} else {
22+
dist[i][j] = INF;
23+
}
24+
}
25+
}
26+
27+
for (int i = 0; i < m; i++) {
28+
StringTokenizer st = new StringTokenizer(br.readLine());
29+
int a = Integer.parseInt(st.nextToken());
30+
int b = Integer.parseInt(st.nextToken());
31+
int c = Integer.parseInt(st.nextToken());
32+
33+
dist[a][b] = Math.min(dist[a][b], c);
34+
}
35+
36+
for (int k = 1; k <= n; k++) {
37+
for (int i = 1; i <= n; i++) {
38+
for (int j = 1; j <= n; j++) {
39+
if (dist[i][k] != INF && dist[k][j] != INF) {
40+
dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j]);
41+
}
42+
}
43+
}
44+
}
45+
46+
for (int i = 1; i <= n; i++) {
47+
for (int j = 1; j <= n; j++) {
48+
if (dist[i][j] == INF) {
49+
bw.write("0 ");
50+
} else {
51+
bw.write(dist[i][j] + " ");
52+
}
53+
}
54+
bw.newLine();
55+
}
56+
57+
bw.flush();
58+
bw.close();
59+
br.close();
60+
}
61+
}
62+
```

0 commit comments

Comments
 (0)