-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance.cpp
67 lines (58 loc) · 1.72 KB
/
1334. Find the 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
63
64
65
66
67
// Attempt - 1
typedef pair<int,int> pi;
class Solution {
public:
void help(vector<pi>* adj,int node,int& n,int& distanceThreshold,vector<int>& dis)
{
priority_queue<pi,vector<pi>,greater<pi>> pq;
dis[node]=0;
pq.push({0,node});
while(!pq.empty())
{
int node=pq.top().second;
int d=pq.top().first;
pq.pop();
// if(d>distanceThreshold)
// break;
for(auto& it:adj[node])
{
int tempDistance=it.second+d;
if(dis[it.first]>tempDistance)
{
pq.push({tempDistance,it.first});
dis[it.first]=tempDistance;
}
}
}
}
int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold){
vector<pi> adj[n];
for(auto& it:edges)
{
adj[it[0]].push_back({it[1],it[2]});
adj[it[1]].push_back({it[0],it[2]});
}
vector<pi> ans;
for(int i=0;i<n;i++)
{
vector<int> dis(n,1e9);
help(adj,i,n,distanceThreshold,dis);
int cnt=0;
for(auto& it:dis)
{
if(it<=distanceThreshold)
cnt++;
}
if(cnt!=0)
ans.push_back({cnt,i});
}
sort(ans.begin(),ans.end(),[](pi& a,pi& b){
if(a.first==b.first)
return a.second>b.second;
return a.first<b.first;
});
if(ans[0].first>distanceThreshold)
return -1;
return ans[0].second;
}
};