-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode 1334.cpp
More file actions
55 lines (47 loc) · 1.55 KB
/
Leetcode 1334.cpp
File metadata and controls
55 lines (47 loc) · 1.55 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
46
47
48
49
50
51
52
53
54
55
class Solution {
public:
int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {
vector<vector<int>> dist(n, vector<int>(n, INT_MAX ));
vector<pair<int, int>> adj[n];
for (auto it : edges) {
adj[it[0]].push_back({ it[1], it[2] });
adj[it[1]].push_back({ it[0], it[2] });
}
for (int i = 0; i < n; i++) {
vector<int> visited(n, false);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({ 0, i });
dist[i][i] = 0;
while (!pq.empty()) {
int node = pq.top().second;
int dis = pq.top().first;
pq.pop();
if (visited[node]) continue;
visited[node] = true;
for (auto it : adj[node]) {
int adjNode = it.first;
int wt = it.second;
if (dis + wt < dist[i][adjNode]) {
dist[i][adjNode] = dis + wt;
pq.push({ dist[i][adjNode], adjNode });
}
}
}
}
int city = -1;
int count = n;
for (int i = 0; i < n; i++) {
int c = 0;
for (int j = 0; j < n; j++) {
if (dist[i][j] <= distanceThreshold) {
c++;
}
}
if (c <= count) {
count = c;
city = i;
}
}
return city;
}
};