-
Notifications
You must be signed in to change notification settings - Fork 0
/
City With the Smallest Number of Neighbors at a Threshold Distance.cpp
62 lines (53 loc) · 1.67 KB
/
City With the Smallest Number of Neighbors at a Threshold Distance.cpp
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
/*
Problem Link: https://practice.geeksforgeeks.org/problems/city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/0
*/
class Solution {
public:
int findCity(int n, int m, vector<vector<int>>& edges, int distanceThreshold) {
// using floyd warshall algo (we can also use dijikstra algo as well)
vector<vector<int>> dist(n, vector<int>(n, 1e9)); // n*n
for(auto it: edges){
dist[it[0]][it[1]]= it[2];
dist[it[1]][it[0]]= it[2];
}
for(int i=0; i<n; i++) dist[i][i]= 0;
for(int k=0; k<n; k++){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(dist[i][k] != 1e9 && dist[k][j] != 1e9){
dist[i][j]= min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
}
int cntCity= n;
int currCity= -1;
for(int city=0; city<n; city++){
int cnt=0;
for(int adjCity=0; adjCity<n; adjCity++){
if(dist[city][adjCity] <= distanceThreshold) cnt++;
}
if(cnt <= cntCity){
cntCity= cnt;
currCity= city;
}
}
return currCity;
}
};
// ---------------------------------------------------
// we can also initialize dist using this
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
if(i==j)
dist[i][j] = 0;
}
}
// (m would be used in this case)
for(int i=0;i<m;++i){
int u = edges[i][0];
int v = edges[i][1];
int cost = edges[i][2];
dist[u][v] = cost;
dist[v][u] = cost;
}