|
148 | 148 | // }
|
149 | 149 |
|
150 | 150 | /* Floyd Warshall */
|
| 151 | + |
| 152 | +// class Solution { |
| 153 | +// public int findTheCity(int n, int[][] edges, int distanceThreshold) { |
| 154 | +// int[][] dp = new int[n][n]; |
| 155 | +// |
| 156 | +// // Initialize dp |
| 157 | +// for (int i = 0; i < n; i++) { |
| 158 | +// Arrays.fill(dp[i], Integer.MAX_VALUE); |
| 159 | +// dp[i][i] = 0; |
| 160 | +// } |
| 161 | +// |
| 162 | +// for (int[] edge : edges) { |
| 163 | +// // Fill dp with from to edge grid; dp[from][to] = weight |
| 164 | +// dp[edge[0]][edge[1]] = edge[2]; |
| 165 | +// dp[edge[1]][edge[0]] = edge[2]; |
| 166 | +// } |
| 167 | +// |
| 168 | +// // Find all shortest path |
| 169 | +// for (int detour = 0; detour < n; detour++) { |
| 170 | +// for (int from = 0; from < n; from++) { |
| 171 | +// for (int to = 0; to < n; to++) { |
| 172 | +// // Update edge path if detour city is shorter than direct |
| 173 | +// if (dp[from][to] > dp[from][detour] + dp[detour][to]) |
| 174 | +// dp[from][to] = dp[from][detour] + dp[detour][to]; |
| 175 | +// } |
| 176 | +// } |
| 177 | +// } |
| 178 | +// |
| 179 | +// int maxVisits = n + 1; |
| 180 | +// int cityWithLesserNeighbors = -1; |
| 181 | +// for(int from = 0; from < n; from++) { |
| 182 | +// // Get all neighboring cities with less than distanceThreshold edge |
| 183 | +// int neighborCitiesWithinLimit = 0; |
| 184 | +// for(int to = 0; to < n; to++) { |
| 185 | +// if(dp[from][to] <= distanceThreshold) |
| 186 | +// neighborCitiesWithinLimit++; |
| 187 | +// } |
| 188 | +// if(neighborCitiesWithinLimit <= maxVisits){ |
| 189 | +// cityWithLesserNeighbors = from; |
| 190 | +// maxVisits = Math.min(maxVisits, neighborCitiesWithinLimit); |
| 191 | +// } |
| 192 | +// } |
| 193 | +// |
| 194 | +// return cityWithLesserNeighbors; |
| 195 | +// } |
| 196 | +// } |
| 197 | + |
| 198 | + |
151 | 199 | class Solution {
|
152 | 200 | public int findTheCity(int n, int[][] edges, int distanceThreshold) {
|
153 | 201 | float[][] dp = new float[n][n];
|
154 | | - for(int i = 0; i < n; i++){ |
155 | | - Arrays.fill(dp[i],Integer.MAX_VALUE); |
156 | | - /* If int[][] Arrays.fill(dp[i],12345); */ |
| 202 | + |
| 203 | + // Initialize dp |
| 204 | + for (int i = 0; i < n; i++) { |
| 205 | + Arrays.fill(dp[i], Integer.MAX_VALUE); |
157 | 206 | dp[i][i] = 0;
|
158 | 207 | }
|
159 | | - for(int[] edge : edges){ |
| 208 | + |
| 209 | + for (int[] edge : edges) { |
| 210 | + // Fill dp with from to edge grid; dp[from][to] = weight |
160 | 211 | dp[edge[0]][edge[1]] = edge[2];
|
161 | 212 | dp[edge[1]][edge[0]] = edge[2];
|
162 | 213 | }
|
163 | 214 |
|
164 | | - for(int k = 0; k < n; k++){ |
165 | | - for(int i = 0; i < n; i++) { |
166 | | - for(int j = 0; j < n; j++) { |
167 | | - dp[i][j] = Math.min(dp[i][j],dp[i][k] + dp[k][j]); |
| 215 | + // Find all shortest path |
| 216 | + for (int detour = 0; detour < n; detour++) { |
| 217 | + for (int from = 0; from < n; from++) { |
| 218 | + for (int to = 0; to < n; to++) { |
| 219 | + // Update edge path if detour city is shorter than direct |
| 220 | + if (dp[from][to] > dp[from][detour] + dp[detour][to]) |
| 221 | + dp[from][to] = dp[from][detour] + dp[detour][to]; |
168 | 222 | }
|
169 | 223 | }
|
170 | 224 | }
|
171 | | - int maxVisits = n+1; |
172 | | - int result = -1; |
173 | 225 |
|
174 | | - for(int i = 0; i < n; i++) { |
175 | | - int visit = 0; |
176 | | - for(int j = 0; j < n; j++) { |
177 | | - if(dp[i][j] <= distanceThreshold) visit++; |
| 226 | + int maxVisits = n + 1; |
| 227 | + int cityWithLesserNeighbors = -1; |
| 228 | + for(int from = 0; from < n; from++) { |
| 229 | + // Get all neighboring cities with less than distanceThreshold edge |
| 230 | + int neighborCitiesWithinLimit = 0; |
| 231 | + for(int to = 0; to < n; to++) { |
| 232 | + if(dp[from][to] <= distanceThreshold) |
| 233 | + neighborCitiesWithinLimit++; |
178 | 234 | }
|
179 | | - if(visit <= maxVisits){ |
180 | | - result = i; |
181 | | - maxVisits = Math.min(maxVisits,visit); |
| 235 | + if(neighborCitiesWithinLimit <= maxVisits){ |
| 236 | + cityWithLesserNeighbors = from; |
| 237 | + maxVisits = Math.min(maxVisits,neighborCitiesWithinLimit); |
182 | 238 | }
|
183 | 239 | }
|
184 | | - return result; |
| 240 | + |
| 241 | + return cityWithLesserNeighbors; |
185 | 242 | }
|
186 | 243 | }
|
0 commit comments