-
Notifications
You must be signed in to change notification settings - Fork 0
/
swexpert_1263.java
66 lines (59 loc) · 1.77 KB
/
swexpert_1263.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.ssafy.algo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class swexpert_1263 {
static int N;
static int map[][];
static final int MAX_INT = 987654321;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(br.readLine());
for(int tc = 1; tc <= T; ++tc) {
// data input
String[] s = br.readLine().split(" ");
int idx = 0;
N = Integer.parseInt(s[0]);
map = new int[N][N];
for(int i = 0; i < N; ++i) {
for(int j = 0; j< N; ++j) {
map[i][j] = Integer.parseInt(s[++idx]);
if(map[i][j] == 0)
map[i][j] = MAX_INT;
}
}
floyd(); // 플로이드 알고리즘
int result = MAX_INT;
for(int i = 0; i < N; ++i) {
int curSum = 0;
for(int j = 0; j < N; ++j)
if(map[i][j] < MAX_INT)
curSum += map[i][j];
result = Math.min(curSum, result);
}
// solve
bw.write("#" + tc + " " + result + "\n");
}
// flush data
bw.flush();
bw.close();
br.close();
}
private static void floyd() { // 풀로이드 알고리즘
for (int k = 0; k < N; k++) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i == j) {
continue;
}
if (map[i][j] > map[i][k] + map[k][j]) {
map[i][j] = map[i][k] + map[k][j];
}
}
}
}
}
}