generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.cpp
50 lines (43 loc) · 1.31 KB
/
solution.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
#include <limits>
#include <vector>
class Solution {
public:
int findTheCity(
int n, std::vector<std::vector<int>>& edges,
int distanceThreshold) {
std::vector<std::vector<int>> distances(
n, std::vector<int>(n, std::numeric_limits<int>::max()));
for (int i{n - 1}; i >= 0; --i) {
distances[i][i] = 0;
}
for (const auto& edge : edges) {
distances[edge[0]][edge[1]] = edge[2];
distances[edge[1]][edge[0]] = edge[2];
}
for (int i{n - 1}; i >= 0; --i) {
for (int j{n - 1}; j >= 0; --j) {
if (distances[i][j] == std::numeric_limits<int>::max()) continue;
for (int k{n - 1}; k >= 0; --k) {
if (distances[j][k] == std::numeric_limits<int>::max()) continue;
if (distances[i][j] + distances[j][k] < distances[i][k]) {
distances[i][k] = distances[i][j] + distances[j][k];
distances[k][i] = distances[i][j] + distances[j][k];
}
}
}
}
int theCity{n};
int theCount{std::numeric_limits<int>::max()};
for (int i{n - 1}; i >= 0; --i) {
int count{0};
for (int j{n - 1}; j >= 0; --j) {
if (distances[i][j] <= distanceThreshold) ++count;
}
if (count < theCount) {
theCity = i;
theCount = count;
}
}
return theCity;
}
};