-
Notifications
You must be signed in to change notification settings - Fork 481
/
1334.cpp
32 lines (29 loc) · 874 Bytes
/
1334.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
class Solution
{
public:
int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold)
{
int d[n][n];
memset(d, 0x3f, sizeof d);
for (int i = 0; i < n; i++) d[i][i] = 0;
for (auto& it : edges) d[it[0]][it[1]] = d[it[1]][it[0]] = it[2];
for (int k = 0; k < n; k++)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
int smallest = INT_MAX, res = 0;
for (int i = 0; i < n; i++)
{
int cnt = 0;
for (int j = 0; j < n; ++j) if (d[i][j] <= distanceThreshold) ++cnt;
if (cnt <= smallest) res = i, smallest = cnt;
}
return res;
}
};