-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcity-with-the-smallest-number-of-neighbors-at-a-threshold-distance.java
66 lines (56 loc) · 1.64 KB
/
city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.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
class Node {
int v, w;
Node(int v, int w) {
this.v = v;
this.w = w;
}
}
class Solution {
int findCity(int n, int m, int[][] edges,int distanceThreshold)
{
List<List<Node>> adj = new ArrayList<>();
for (int i = 0; i < n; i++) {
adj.add(new ArrayList<>());
}
// Constructing adjacency list
for (int[] edge : edges) {
adj.get(edge[0]).add(new Node(edge[1], edge[2]));
adj.get(edge[1]).add(new Node(edge[0], edge[2])); // Considering bidirectional edges
}
int minCities = Integer.MAX_VALUE;
int result = -1;
//running dijkstras for reach city
for (int i = 0; i < n; i++) {
int[] dis = new int[n];
Arrays.fill(dis, Integer.MAX_VALUE);
dijkstra(adj, i, dis);
int reachableCities = 0;
for (int j = 0; j < n; j++) {
if (dis[j] <= distanceThreshold) {
reachableCities++;
}
}
//tracking min
if (reachableCities <= minCities) {
minCities = reachableCities;
result = i;
}
}
return result;
}
// Dijkstra's Algorithm
void dijkstra(List<List<Node>> adj, int start, int[] dis) {
PriorityQueue<Node> pq = new PriorityQueue<>((x, y) -> x.w - y.w);
dis[start] = 0;
pq.add(new Node(start, 0));
while (!pq.isEmpty()) {
Node currNode = pq.poll();
for (Node nei : adj.get(currNode.v)) {
if (dis[nei.v] > dis[currNode.v] + nei.w) {
dis[nei.v] = dis[currNode.v] + nei.w;
pq.add(new Node(nei.v, dis[nei.v]));
}
}
}
}
}