-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path택배.java
More file actions
58 lines (50 loc) · 1.85 KB
/
택배.java
File metadata and controls
58 lines (50 loc) · 1.85 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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
static int N, M;
static int[][] time, map;
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
StringBuilder sb = new StringBuilder();
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
time = new int[N][N];
map = new int[N][N];
for (int i = 0; i < N; i++) {
Arrays.fill(time[i], 10000);
time[i][i] = 0;
}
int start, end, distance;
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
start = Integer.parseInt(st.nextToken()) - 1;
end = Integer.parseInt(st.nextToken()) - 1;
distance = Integer.parseInt(st.nextToken());
time[start][end] = distance;
time[end][start] = distance;
map[start][end] = end;
map[end][start] = start;
}
for (int k = 0; k < N; k++) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (time[i][k] + time[k][j] < time[i][j]){
map[i][j] = map[i][k];
time[i][j] = time[i][k] + time[k][j];
}
}
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N ; j++) {
if(i == j)sb.append("-").append(" ");
else sb.append(map[i][j] + 1).append(" ");
}
sb.append("\n");
}
System.out.println(sb);
}
}