-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution_1263_2.java
84 lines (70 loc) · 1.85 KB
/
Solution_1263_2.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Solution_1263_2 {
static int n=0;
static int[][] map;
static int[] rst;
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int INF = Integer.MAX_VALUE;
int T = Integer.parseInt(bf.readLine());
for (int tc = 1; tc <= T; tc++) {
StringTokenizer st = new StringTokenizer(bf.readLine(), " ");
n = Integer.parseInt(st.nextToken());
map = new int[n+1][n+1];
rst = new int[n+1];
//int[][] dist = new int[n][n];
for (int i = 1; i <= n; i++) {
//Arrays.fill(dist[i], INF);
for (int j = 1; j <= n; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
if (map[i][j] == 1) {
//dist[i][j] = 1;
}
}
} // ÀÔ·Â ¹Þ±â.
int min = Integer.MAX_VALUE;
int ans = 0;
for (int i = 1; i <= n; i++) {
rst[i] = bfs(i);
if (rst[i] < min) {
min = rst[i];
}
}
System.out.println("#"+tc+" "+min);
} // end of tc
} // end of main
static int bfs(int start) {
boolean[] visited = new boolean[n + 1];
Queue<pair> q = new LinkedList<pair>();
visited[start] = true;
q.add(new pair(start, 0));
int score = 0;
while (!q.isEmpty()) {
int now = q.peek().now;
int weight = q.peek().weight;
score += weight;
q.poll();
for (int i = 1; i <= n; i++) {
if (map[now][i]==1 && !visited[i]) {
visited[i] = true;
q.add(new pair(i, weight + 1));
}
}
}
return score;
}
public static class pair {
int now;
int weight;
public pair(int now, int weight) {
super();
this.now = now;
this.weight = weight;
}
}
}