-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1334.findCityWithSmallestNeighbours.floydwarshall.java
45 lines (43 loc) · 1.32 KB
/
1334.findCityWithSmallestNeighbours.floydwarshall.java
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
class Solution {
public int findTheCity(int n, int[][] edges, int distanceThreshold) {
// just do floyd warshall
// then count destination city from every city that meets threshold.
int mat[][]=new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==j)mat[i][j]=0;
else mat[i][j]=(int)1e9;
}
}
for(int[] edge:edges){
int src=edge[0];
int des=edge[1];
int wt=edge[2];
mat[src][des]=wt;
mat[des][src]=wt;
}
for(int k=0;k<n;k++){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(mat[i][k]+mat[k][j]< mat[i][j]){
mat[i][j]=mat[i][k]+mat[k][j];
}
}
}
}
int city=0,min=(int)1e9;
for(int i=0;i<n;i++){
int count=0;
for(int j=0;j<n;j++){
if(mat[i][j]<=distanceThreshold)count++;
}
// count represent max cities we can reach in threshold from city-i.
if(count<=min){
// if it is less then current value then update the answer
min=count;
city=i;
}
}
return city;
}
}