-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllCityThreshold.java
More file actions
45 lines (37 loc) · 1.14 KB
/
AllCityThreshold.java
File metadata and controls
45 lines (37 loc) · 1.14 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
package graph;
import java.util.Arrays;
public class AllCityThreshold {
public int findTheCity(int n, int[][] edges, int distanceThreshold) {
int[][] distance = new int[n][n];
for (int i = 0; i < n; i++) {
Arrays.fill(distance[i], 1000000000);
distance[i][i] = 0;
}
for (int[] edge : edges) {
distance[edge[0]][edge[1]] = edge[2];
distance[edge[1]][edge[0]] = edge[2];
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
distance[i][j] = Math.min(distance[i][j], distance[i][k] + distance[k][j]);
}
}
}
int ans = -1;
int mini = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
int count = 0;
for (int j = 0; j < n; j++) {
if (i != j && distance[i][j] <= distanceThreshold) {
count++;
}
}
if (count <= mini) {
mini = count;
ans = i;
}
}
return ans;
}
}