-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1334.FindTheCityWithTheSmallestNumberOfNeighboursAtAThresholdDistance.cpp
More file actions
68 lines (58 loc) · 1.62 KB
/
1334.FindTheCityWithTheSmallestNumberOfNeighboursAtAThresholdDistance.cpp
File metadata and controls
68 lines (58 loc) · 1.62 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
56
57
58
59
60
61
62
63
64
65
66
67
68
class Solution {
public:
int m_n, m_distanceThreshold;
int m_data[100][100];
void floydWarshallAlgorithmWithThres(const vector<vector<int>>& edges) {
// Initialize data.
for (int i = 0; i < sizeof(m_data) / sizeof(*m_data); i++) {
for (int j = 0; j < sizeof(*m_data) / sizeof(**m_data); j++)
m_data[i][j] = INT_MAX;
m_data[i][i] = 0;
}
// Add edge paths.
for (const vector<int>& edge : edges) {
const int
from = edge[0],
to = edge[1],
distance = edge[2];
if (distance <= m_distanceThreshold) {
m_data[from][to] = distance;
m_data[to][from] = distance;
}
}
// Minimize routes.
constexpr int maxThreshold = INT_MAX / 2;
for (int i = 0; i < m_n; i++)
for (int j = 0; j < m_n; j++)
for (int k = 0; k < m_n; k++)
if (m_data[i][k] <= (INT_MAX - m_data[j][i]) &&
m_data[j][k] > (m_data[i][k] + m_data[j][i]))
// Minimize route.
m_data[j][k] = m_data[i][k] + m_data[j][i];
}
int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {
// Speed thingies.
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// Setup calculation variables.
m_n = n;
m_distanceThreshold = distanceThreshold;
// Use the Floyd Warshall Algorithm here.
floydWarshallAlgorithmWithThres(edges);
int minCount = n, cityIndex = -1;
for (int i = 0; i < n; i++) {
// Get count of cities reachable.
int count = -1;
for (int j = 0; j < n; j++)
if (m_data[i][j] <= m_distanceThreshold)
count++;
// Check if better.
if (minCount >= count) {
minCount = count;
cityIndex = i;
}
}
return cityIndex;
}
};