Skip to content

Commit b981512

Browse files
authored
Merge pull request #1735 from AlgorithmWithGod/LiiNi-coder
[20251224] BOJ / G4 / 플로이드 / 이인희
2 parents f1aab44 + f983a47 commit b981512

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static final int INF = 1_000_000_000;
7+
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
int N = Integer.parseInt(br.readLine());
11+
int M = Integer.parseInt(br.readLine());
12+
int[][] dist = new int[N + 1][N + 1];
13+
for(int i = 1; i <= N; i++){
14+
for(int j = 1; j <= N; j++){
15+
if(i == j){
16+
dist[i][j] = 0;
17+
} else {
18+
dist[i][j] = INF;
19+
}
20+
}
21+
}
22+
23+
for(int i = 0; i < M; i++){
24+
StringTokenizer st = new StringTokenizer(br.readLine());
25+
int s = Integer.parseInt(st.nextToken());
26+
int e = Integer.parseInt(st.nextToken());
27+
int w = Integer.parseInt(st.nextToken());
28+
dist[s][e] = Math.min(dist[s][e], w);
29+
}
30+
//플루이드워셜
31+
for(int k = 1; k <= N; k++){
32+
for(int i = 1; i <= N; i++){
33+
for(int j = 1; j <= N; j++){
34+
if(dist[i][j] > dist[i][k] + dist[k][j]){
35+
dist[i][j] = dist[i][k] + dist[k][j];
36+
}
37+
}
38+
}
39+
}
40+
41+
StringBuilder sb = new StringBuilder();
42+
for(int i = 1; i <= N; i++){
43+
for(int j = 1; j <= N; j++){
44+
if(dist[i][j] == INF){
45+
sb.append(0).append(" ");
46+
} else {
47+
sb.append(dist[i][j]).append(" ");
48+
}
49+
}
50+
sb.append("\n");
51+
}
52+
System.out.println(sb.toString());
53+
}
54+
}
55+
56+
```

0 commit comments

Comments
 (0)