-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_11404.java
More file actions
67 lines (55 loc) · 1.75 KB
/
BOJ_11404.java
File metadata and controls
67 lines (55 loc) · 1.75 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class BOJ_11404 {
public static int n, m, a, b, c;
public static long[][] dis; // 최단거리 저장
public static StringBuilder sb = new StringBuilder();
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()); // 버스 개수
dis = new long[n+1][n+1]; // 1~N 까지
for(long[] a : dis) {
Arrays.fill(a, Integer.MAX_VALUE);
}
StringTokenizer st;
for(int i=0;i<m;i++) {
st = new StringTokenizer(br.readLine(), " ");
// 간선 정보
int a = Integer.parseInt(st.nextToken()); // start
int b = Integer.parseInt(st.nextToken()); // end
int c = Integer.parseInt(st.nextToken()); // 비용 (cost)
dis[a][b] = Math.min(dis[a][b], c);
}
floyd();
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) {
// 시작 도시 = 도착 도시 or 갈 수 없는 도시 (초기화 값 그대로인 곳)
if(i == j || dis[i][j] >= Integer.MAX_VALUE) {
sb.append("0 ");
} else {
sb.append(dis[i][j] + " ");
}
}
sb.append("\n");
}
System.out.println(sb);
}
// 플로이드 알고리즘
public static void floyd() {
// i ~ j 까지의 최단거리 : i ~ k + k ~ j (각 최단 거리의 합) : 두 가지 비교하여 min 저장
// 모든 노드들을 한번씩 거쳐가는 k를 기준으로 함
for(int k=1;k<=n;k++) {
// 출발하는 노드
for(int i=1;i<=n;i++) {
// 도착하는 노드
for(int j=1;j<=n;j++) {
dis[i][j] = Math.min(dis[i][j], dis[i][k] + dis[k][j]);
}
}
}
}
}