-
Notifications
You must be signed in to change notification settings - Fork 2
/
Solution.java
51 lines (41 loc) Β· 1.58 KB
/
Solution.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
package Graph.swea1263;
import java.io.*;
import java.util.*;
public class Solution {
final static int INF = 1001;
static int T, N;
static int[][] D;
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Graph/swea1263/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
T = Integer.parseInt(br.readLine());
for (int t = 1; t <= T; t++) {
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
D = new int[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
D[i][j] = Integer.parseInt(st.nextToken());
if (i != j && D[i][j] == 0) D[i][j] = INF;
}
}
for (int k = 0; k < N; k++) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
// if (i == j || i == k) continue;
D[i][j] = Math.min(D[i][j], D[i][k] + D[k][j]);
}
}
}
int cc_min = Integer.MAX_VALUE;
for (int i = 0; i < N; i++) {
int cc = 0;
for (int j = 0; j < N; j++) cc += D[i][j];
cc_min = Math.min(cc_min, cc);
}
sb.append("#").append(t).append(" ").append(cc_min).append("\n");
}
System.out.println(sb.toString());
}
}